Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dmaker.fan.p9 and dmaker.fan.p10 #819

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Supported devices
- Xiaomi Philips Zhirui Smart LED Bulb E14 Candle Lamp
- Xiaomi Philips Zhirui Bedroom Smart Lamp
- Xiaomi Universal IR Remote Controller (Chuangmi IR)
- Xiaomi Mi Smart Pedestal Fan V2, V3, SA1, ZA1, ZA3, ZA4, P5
- Xiaomi Mi Smart Pedestal Fan V2, V3, SA1, ZA1, ZA3, ZA4, P5, P9, P10
- Xiaomi Mi Air Humidifier V1, CA1, CA4, CB1, MJJSQ, JSQ001
- Xiaomi Mi Water Purifier (Basic support: Turn on & off)
- Xiaomi PM2.5 Air Quality Monitor V1, B1, S1
Expand Down
7 changes: 7 additions & 0 deletions docs/api/miio.airconditioningcompanionMCN.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
miio.airconditioningcompanionMCN module
=======================================

.. automodule:: miio.airconditioningcompanionMCN
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion miio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from miio.cooker import Cooker
from miio.device import Device
from miio.exceptions import DeviceError, DeviceException
from miio.fan import Fan, FanP5, FanSA1, FanV2, FanZA1, FanZA4
from miio.fan import Fan, FanMiot, FanP5, FanP9, FanP10, FanSA1, FanV2, FanZA1, FanZA4
from miio.gateway import Gateway
from miio.heater import Heater
from miio.philips_bulb import PhilipsBulb, PhilipsWhiteBulb
Expand Down
301 changes: 301 additions & 0 deletions miio/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .click_common import EnumType, command, format_output
from .device import Device
from .exceptions import DeviceException
from .miot_device import MiotDevice

_LOGGER = logging.getLogger(__name__)

Expand All @@ -17,6 +18,8 @@
MODEL_FAN_ZA3 = "zhimi.fan.za3"
MODEL_FAN_ZA4 = "zhimi.fan.za4"
MODEL_FAN_P5 = "dmaker.fan.p5"
MODEL_FAN_P9 = "dmaker.fan.p9"
MODEL_FAN_P10 = "dmaker.fan.p10"

AVAILABLE_PROPERTIES_COMMON = [
"angle",
Expand Down Expand Up @@ -63,6 +66,37 @@
MODEL_FAN_P5: AVAILABLE_PROPERTIES_P5,
}

MIOT_MAPPING = {
MODEL_FAN_P9: {
# Source https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p9:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"child_lock": {"siid": 3, "piid": 1},
"fan_speed": {"siid": 2, "piid": 11},
"swing_mode": {"siid": 2, "piid": 5},
"swing_mode_angle": {"siid": 2, "piid": 6},
"power_off_time": {"siid": 2, "piid": 8},
"buzzer": {"siid": 2, "piid": 7},
"light": {"siid": 2, "piid": 9},
"mode": {"siid": 2, "piid": 4},
"set_move": {"siid": 2, "piid": 10},
},
MODEL_FAN_P10: {
# Source https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p10:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"child_lock": {"siid": 3, "piid": 1},
"fan_speed": {"siid": 2, "piid": 10},
"swing_mode": {"siid": 2, "piid": 4},
"swing_mode_angle": {"siid": 2, "piid": 5},
"power_off_time": {"siid": 2, "piid": 6},
"buzzer": {"siid": 2, "piid": 8},
"light": {"siid": 2, "piid": 7},
"mode": {"siid": 2, "piid": 3},
"set_move": {"siid": 2, "piid": 9},
},
}


class FanException(DeviceException):
pass
Expand All @@ -73,6 +107,11 @@ class OperationMode(enum.Enum):
Nature = "nature"


class OperationModeMiot(enum.Enum):
Normal = 0
Nature = 1


class LedBrightness(enum.Enum):
Bright = 0
Dim = 1
Expand Down Expand Up @@ -367,6 +406,91 @@ def __json__(self):
return self.data


class FanStatusMiot:
"""Container for status reports from the Xiaomi Mi Smart Pedestal Fan DMaker P9/P10."""

def __init__(self, data: Dict[str, Any]) -> None:
self.data = data

@property
def power(self) -> str:
"""Power state."""
return "on" if self.data["power"] else "off"

@property
def is_on(self) -> bool:
"""True if device is currently on."""
return self.data["power"]

@property
def mode(self) -> OperationModeMiot:
"""Operation mode."""
return OperationModeMiot(self.data["mode"])

@property
def speed(self) -> int:
"""Speed of the motor."""
return self.data["fan_speed"]

@property
def oscillate(self) -> bool:
"""True if oscillation is enabled."""
return self.data["swing_mode"]

@property
def angle(self) -> int:
"""Oscillation angle."""
return self.data["swing_mode_angle"]

@property
def delay_off_countdown(self) -> int:
"""Countdown until turning off in seconds."""
return self.data["power_off_time"]

@property
def led(self) -> bool:
"""True if LED is turned on, if available."""
return self.data["light"]

@property
def buzzer(self) -> bool:
"""True if buzzer is turned on."""
return self.data["buzzer"]

@property
def child_lock(self) -> bool:
"""True if child lock is on."""
return self.data["child_lock"]

def __repr__(self) -> str:
s = (
"<FanStatus power=%s, "
"mode=%s, "
"speed=%s, "
"oscillate=%s, "
"angle=%s, "
"led=%s, "
"buzzer=%s, "
"child_lock=%s, "
"delay_off_countdown=%s>"
% (
self.power,
self.mode,
self.speed,
self.oscillate,
self.angle,
self.led,
self.buzzer,
self.child_lock,
self.delay_off_countdown,
)
)
return s

def __json__(self):
return self.data


class Fan(Device):
"""Main class representing the Xiaomi Mi Smart Pedestal Fan."""

Expand Down Expand Up @@ -767,3 +891,180 @@ def delay_off(self, minutes: int):
def set_rotate(self, direction: MoveDirection):
"""Rotate the fan by -5/+5 degrees left/right."""
return self.send("m_roll", [direction.value])


class FanMiot(MiotDevice):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
model: str = MODEL_FAN_P10,
) -> None:
super().__init__(MIOT_MAPPING[model], ip, token, start_id, debug, lazy_discover)

if model in MIOT_MAPPING:
self.model = model
else:
self.model = MODEL_FAN_P10

@command(
default_output=format_output(
"",
"Power: {result.power}\n"
"Operation mode: {result.mode}\n"
"Speed: {result.speed}\n"
"Oscillate: {result.oscillate}\n"
"Angle: {result.angle}\n"
"LED: {result.led}\n"
"Buzzer: {result.buzzer}\n"
"Child lock: {result.child_lock}\n"
"Power-off time: {result.delay_off_countdown}\n",
)
)
def status(self) -> FanStatusMiot:
"""Retrieve properties."""
return FanStatusMiot(
{
prop["did"]: prop["value"] if prop["code"] == 0 else None
for prop in self.get_properties_for_mapping()
}
)

@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.set_property("power", True)

@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.set_property("power", False)

@command(
click.argument("mode", type=EnumType(OperationModeMiot)),
default_output=format_output("Setting mode to '{mode.value}'"),
)
def set_mode(self, mode: OperationModeMiot):
"""Set mode."""
return self.set_property("mode", mode.value)

@command(
click.argument("speed", type=int),
default_output=format_output("Setting speed to {speed}"),
)
def set_speed(self, speed: int):
"""Set speed."""
if speed < 0 or speed > 100:
raise FanException("Invalid speed: %s" % speed)

return self.set_property("fan_speed", speed)

@command(
click.argument("angle", type=int),
default_output=format_output("Setting angle to {angle}"),
)
def set_angle(self, angle: int):
"""Set the oscillation angle."""
if angle not in [30, 60, 90, 120, 140]:
raise FanException(
"Unsupported angle. Supported values: 30, 60, 90, 120, 140"
)

return self.set_property("swing_mode_angle", angle)

@command(
click.argument("oscillate", type=bool),
default_output=format_output(
lambda oscillate: "Turning on oscillate"
if oscillate
else "Turning off oscillate"
),
)
def set_oscillate(self, oscillate: bool):
"""Set oscillate on/off."""
if oscillate:
return self.set_property("swing_mode", True)
else:
return self.set_property("swing_mode", False)

@command(
click.argument("led", type=bool),
default_output=format_output(
lambda led: "Turning on LED" if led else "Turning off LED"
),
)
def set_led(self, led: bool):
"""Turn led on/off."""
if led:
return self.set_property("light", True)
else:
return self.set_property("light", False)

@command(
click.argument("buzzer", type=bool),
default_output=format_output(
lambda buzzer: "Turning on buzzer" if buzzer else "Turning off buzzer"
),
)
def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
if buzzer:
return self.set_property("buzzer", True)
else:
return self.set_property("buzzer", False)

@command(
click.argument("lock", type=bool),
default_output=format_output(
lambda lock: "Turning on child lock" if lock else "Turning off child lock"
),
)
def set_child_lock(self, lock: bool):
"""Set child lock on/off."""
return self.set_property("child_lock", lock)

@command(
click.argument("minutes", type=int),
default_output=format_output("Setting delayed turn off to {minutes} minutes"),
)
def delay_off(self, minutes: int):
"""Set delay off minutes."""

if minutes < 0:
raise FanException("Invalid value for a delayed turn off: %s" % minutes)

return self.set_property("power_off_time", minutes)

@command(
click.argument("direction", type=EnumType(MoveDirection)),
default_output=format_output("Rotating the fan to the {direction}"),
)
def set_rotate(self, direction: MoveDirection):
return self.set_property("set_move", [direction.value])


class FanP9(FanMiot):
swim2sun marked this conversation as resolved.
Show resolved Hide resolved
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_P9)


class FanP10(FanMiot):
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=MODEL_FAN_P10)
Loading