Skip to content

Commit

Permalink
Streamline miot mappings (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Aug 31, 2024
1 parent 423b01f commit 2ebfd5b
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions custom_components/xiaomi_miio_fan/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,8 +1876,6 @@ def supported_features(self) -> int:

"""
TODO:
- fan.turn_on works, but doesn't go to the declared percentage. This is
not an issue when using set_percentage()
- setting child lock works, but HA always reads the value as null
"""

Expand Down Expand Up @@ -1983,15 +1981,15 @@ def __init__(self, data: Dict[str, Any]) -> None:
{'did': 'power', 'siid': 2, 'piid': 1, 'code': 0, 'value': True},
{'did': 'fan_level', 'siid': 2, 'piid': 2, 'code': 0, 'value': 1},
{'did': 'oscillate', 'siid': 2, 'piid': 4, 'code': 0, 'value': False},
{'did': 'angle', 'siid': 2, 'piid': 5, 'code': 0, 'value': 120},
{'did': 'swing_mode', 'siid': 2, 'piid': 4, 'code': 0, 'value': False},
{'did': 'swing_mode_angle', 'siid': 2, 'piid': 5, 'code': 0, 'value': 120},
{'did': 'mode', 'siid': 2, 'piid': 3, 'code': 0, 'value': 1},
{'did': 'delay_off_countdown', 'siid': 3, 'piid': 1, 'code': 0, 'value': 0},
{'did': 'power_off_time', 'siid': 3, 'piid': 1, 'code': 0, 'value': 0},
{'did': 'child_lock', 'siid': 7, 'piid': 1, 'code': 0, 'value': False},
{'did': 'light', 'siid': 4, 'piid': 1, 'code': 0, 'value': True},
{'did': 'buzzer', 'siid': 5, 'piid': 1, 'code': 0, 'value': True},
{'did': 'motor_control', 'siid': 6, 'piid': 1, 'code': -4003},
{'did': 'speed', 'siid': 2, 'piid': 6, 'code': 0, 'value': 20},
{'did': 'set_move', 'siid': 6, 'piid': 1, 'code': -4003},
{'did': 'fan_speed', 'siid': 2, 'piid': 6, 'code': 0, 'value': 20},
"""
self.data = data

Expand All @@ -2009,7 +2007,7 @@ def fan_level(self) -> int:

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

@property
def light(self) -> bool:
Expand All @@ -2029,31 +2027,31 @@ def power(self) -> bool:

@property
def delay_off_countdown(self) -> int:
return self.data["delay_off_countdown"]
return self.data["power_off_time"]

@property
def oscillate(self) -> bool:
return self.data["oscillate"]
return self.data["swing_mode"]

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


class FanP33(MiotDevice):
mapping = {
# https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p33:1
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"oscillate": {"siid": 2, "piid": 4},
"angle": {"siid": 2, "piid": 5},
"swing_mode": {"siid": 2, "piid": 4},
"swing_mode_angle": {"siid": 2, "piid": 5},
"mode": {"siid": 2, "piid": 3},
"delay_off_countdown": {"siid": 3, "piid": 1},
"power_off_time": {"siid": 3, "piid": 1},
"child_lock": {"siid": 7, "piid": 1},
"light": {"siid": 4, "piid": 1},
"buzzer": {"siid": 5, "piid": 1},
"motor_control": {"siid": 6, "piid": 1},
"speed": {"siid": 2, "piid": 6},
"set_move": {"siid": 6, "piid": 1},
"fan_speed": {"siid": 2, "piid": 6},
}

def __init__(
Expand Down Expand Up @@ -2089,7 +2087,7 @@ def set_speed(self, speed: int):
if speed < 0 or speed > 100:
raise FanException("Invalid speed: %s" % speed)

return self.set_property("speed", speed)
return self.set_property("fan_speed", speed)

def set_angle(self, angle: int):
"""Set the oscillation angle."""
Expand All @@ -2099,14 +2097,14 @@ def set_angle(self, angle: int):
+ ", ".join("{0}".format(i) for i in [30, 60, 90, 120, 140])
)

return self.set_property("angle", angle)
return self.set_property("swing_mode_angle", angle)

def set_oscillate(self, oscillate: bool):
"""Set oscillate on/off."""
if oscillate:
return self.set_property("oscillate", True)
return self.set_property("swing_mode", True)
else:
return self.set_property("oscillate", False)
return self.set_property("swing_mode", False)

def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
Expand Down Expand Up @@ -2134,7 +2132,7 @@ def delay_off(self, minutes: int):
if minutes < 0 or minutes > 480:
raise FanException("Invalid value for a delayed turn off: %s" % minutes)

return self.set_property("delay_off_countdown", minutes)
return self.set_property("power_off_time", minutes)

def set_rotate(self, direction: FanMoveDirection):
"""Rotate fan 7.5 degrees horizontally to given direction."""
Expand All @@ -2147,7 +2145,7 @@ def set_rotate(self, direction: FanMoveDirection):
value = 1
elif direction == FanMoveDirection.Right:
value = 2
return self.set_property("motor_control", value)
return self.set_property("set_move", value)


class XiaomiFanP39(XiaomiFanMiot):
Expand Down Expand Up @@ -2293,7 +2291,7 @@ def fan_level(self) -> int:

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

@property
def mode(self) -> str:
Expand All @@ -2305,15 +2303,15 @@ def power(self) -> bool:

@property
def delay_off_countdown(self) -> int:
return self.data["delay_off_countdown"]
return self.data["power_off_time"]

@property
def oscillate(self) -> bool:
return self.data["oscillate"]
return self.data["swing_mode"]

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


class FanP39(MiotDevice):
Expand All @@ -2322,11 +2320,11 @@ class FanP39(MiotDevice):
"power": {"siid": 2, "piid": 1},
"fan_level": {"siid": 2, "piid": 2},
"mode": {"siid": 2, "piid": 4},
"oscillate": {"siid": 2, "piid": 5},
"angle": {"siid": 2, "piid": 6},
"delay_off_countdown": {"siid": 2, "piid": 8},
"motor_control": {"siid": 2, "piid": 10, "access": ["write"]},
"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},
"set_move": {"siid": 2, "piid": 10, "access": ["write"]},
"fan_speed": {"siid": 2, "piid": 11},
"child_lock": {"siid": 3, "piid": 1},
}

Expand Down Expand Up @@ -2379,7 +2377,7 @@ def set_speed(self, speed: int):
if speed < 0 or speed > 100:
raise FanException("Invalid speed: %s" % speed)

return self.set_property("speed", speed)
return self.set_property("fan_speed", speed)

def set_angle(self, angle: int):
"""Set the oscillation angle."""
Expand All @@ -2389,14 +2387,14 @@ def set_angle(self, angle: int):
+ ", ".join("{0}".format(i) for i in [30, 60, 90, 120, 140])
)

return self.set_property("angle", angle)
return self.set_property("swing_mode_angle", angle)

def set_oscillate(self, oscillate: bool):
"""Set oscillate on/off."""
if oscillate:
return self.set_property("oscillate", True)
return self.set_property("swing_mode", True)
else:
return self.set_property("oscillate", False)
return self.set_property("swing_mode", False)

def set_child_lock(self, lock: bool):
"""Set child lock on/off."""
Expand All @@ -2413,7 +2411,7 @@ def delay_off(self, minutes: int):
if minutes < 0 or minutes > 480:
raise FanException("Invalid value for a delayed turn off: %s" % minutes)

return self.set_property("delay_off_countdown", minutes)
return self.set_property("power_off_time", minutes)

def set_rotate(self, direction: FanMoveDirection):
"""Rotate fan 7.5 degrees horizontally to given direction."""
Expand All @@ -2426,4 +2424,4 @@ def set_rotate(self, direction: FanMoveDirection):
value = 1
elif direction == FanMoveDirection.Right:
value = 2
return self.set_property("motor_control", value)
return self.set_property("set_move", value)

0 comments on commit 2ebfd5b

Please sign in to comment.