Skip to content

Commit

Permalink
Some typing hints added and the code order aligned.
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Oct 21, 2017
1 parent 67196c2 commit ed80c50
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 228 deletions.
102 changes: 51 additions & 51 deletions miio/airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,60 @@ class LedBrightness(enum.Enum):
Off = 2


class AirHumidifierStatus:
"""Container for status reports from the air humidifier."""

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

@property
def power(self) -> str:
return self.data["power"]

@property
def is_on(self) -> bool:
return self.power == "on"

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

@property
def temperature(self) -> Optional[float]:
if self.data["temp_dec"] is not None:
return self.data["temp_dec"] / 10.0
return None

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

@property
def buzzer(self) -> bool:
return self.data["buzzer"] == "on"

@property
def led(self) -> bool:
return self.data["led"] == "on"

@property
def led_brightness(self) -> Optional[LedBrightness]:
if self.data["led_b"] is not None:
return LedBrightness(self.data["led_b"])
return None

def __str__(self) -> str:
s = "<AirHumidiferStatus power=%s, mode=%s, temperature=%s, " \
"humidity=%s%%, led=%s, led_brightness=%s, buzzer=%s>" % \
(self.power, self.mode, self.temperature,
self.humidity, self.led, self.led_brightness, self.buzzer)
return s


class AirHumidifier(Device):
"""Main class representing the air humidifier."""

def status(self):
def status(self) -> AirHumidifierStatus:
"""Retrieve properties."""

properties = ['power', 'mode', 'temp_dec', 'humidity', 'buzzer',
Expand Down Expand Up @@ -73,53 +123,3 @@ def set_buzzer(self, buzzer: bool):
return self.send("set_buzzer", ["on"])
else:
return self.send("set_buzzer", ["off"])


class AirHumidifierStatus:
"""Container for status reports from the air humidifier."""

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

@property
def power(self) -> str:
return self.data["power"]

@property
def is_on(self) -> bool:
return self.power == "on"

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

@property
def temperature(self) -> Optional[float]:
if self.data["temp_dec"] is not None:
return self.data["temp_dec"] / 10.0
return None

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

@property
def buzzer(self) -> bool:
return self.data["buzzer"] == "on"

@property
def led(self) -> bool:
return self.data["led"] == "on"

@property
def led_brightness(self) -> Optional[LedBrightness]:
if self.data["led_b"] is not None:
return LedBrightness(self.data["led_b"])
return None

def __str__(self) -> str:
s = "<AirHumidiferStatus power=%s, mode=%s, temperature=%s, " \
"humidity=%s%%, led=%s, led_brightness=%s, buzzer=%s>" % \
(self.power, self.mode, self.temperature,
self.humidity, self.led, self.led_brightness, self.buzzer)
return s
130 changes: 65 additions & 65 deletions miio/airpurifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,71 +20,6 @@ class LedBrightness(enum.Enum):
Off = 2


class AirPurifier(Device):
"""Main class representing the air purifier."""

def status(self):
"""Retrieve properties."""

properties = ['power', 'aqi', 'humidity', 'temp_dec',
'mode', 'led', 'led_b', 'buzzer', 'child_lock',
'bright', 'favorite_level', 'filter1_life',
'f1_hour_used', 'use_time', 'motor1_speed']

values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return AirPurifierStatus(
defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def set_mode(self, mode: OperationMode):
"""Set mode."""
return self.send("set_mode", [mode.value])

def set_favorite_level(self, level: int):
"""Set favorite level."""

# Set the favorite level used when the mode is `favorite`,
# should be between 0 and 17.
return self.send("set_favorite_level", [level]) # 0 ... 17

def set_led_brightness(self, brightness: LedBrightness):
"""Set led brightness."""
return self.send("set_led_b", [brightness.value])

def set_led(self, led: bool):
"""Turn led on/off."""
if led:
return self.send("set_led", ['on'])
else:
return self.send("set_led", ['off'])

def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
if buzzer:
return self.send("set_buzzer", ["on"])
else:
return self.send("set_buzzer", ["off"])


class AirPurifierStatus:
"""Container for status reports from the air purifier."""

Expand Down Expand Up @@ -194,3 +129,68 @@ def __str__(self) -> str:
self.filter_hours_used, self.use_time,
self.motor_speed)
return s


class AirPurifier(Device):
"""Main class representing the air purifier."""

def status(self) -> AirPurifierStatus:
"""Retrieve properties."""

properties = ['power', 'aqi', 'humidity', 'temp_dec',
'mode', 'led', 'led_b', 'buzzer', 'child_lock',
'bright', 'favorite_level', 'filter1_life',
'f1_hour_used', 'use_time', 'motor1_speed']

values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return AirPurifierStatus(
defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def set_mode(self, mode: OperationMode):
"""Set mode."""
return self.send("set_mode", [mode.value])

def set_favorite_level(self, level: int):
"""Set favorite level."""

# Set the favorite level used when the mode is `favorite`,
# should be between 0 and 17.
return self.send("set_favorite_level", [level]) # 0 ... 17

def set_led_brightness(self, brightness: LedBrightness):
"""Set led brightness."""
return self.send("set_led_b", [brightness.value])

def set_led(self, led: bool):
"""Turn led on/off."""
if led:
return self.send("set_led", ['on'])
else:
return self.send("set_led", ['off'])

def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
if buzzer:
return self.send("set_buzzer", ["on"])
else:
return self.send("set_buzzer", ["off"])
5 changes: 1 addition & 4 deletions miio/airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def battery(self) -> int:


class AirQualityMonitor(Device):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def status(self):
def status(self) -> AirQualityMonitorStatus:
"""Return device status."""

properties = ['power', 'aqi', 'battery', 'usb_state']
Expand Down
36 changes: 18 additions & 18 deletions miio/ceil.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ class Ceil(Device):
# TODO: - Auto On/Off Not Supported
# - Adjust Scenes with Wall Switch Not Supported

def status(self) -> CeilStatus:
"""Retrieve properties."""
properties = ['power', 'bright', 'cct', 'snm', 'dv', 'bl', 'ac', ]
values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return CeilStatus(defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
return self.send("set_power", ["on"])
Expand Down Expand Up @@ -103,21 +121,3 @@ def automatic_color_temperature_on(self):
def automatic_color_temperature_off(self):
"""Automatic color temperature off."""
return self.send("enable_ac", [0])

def status(self) -> CeilStatus:
"""Retrieve properties."""
properties = ['power', 'bright', 'cct', 'snm', 'dv', 'bl', 'ac', ]
values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return CeilStatus(defaultdict(lambda: None, zip(properties, values)))
2 changes: 1 addition & 1 deletion miio/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __str__(self) -> str:
class Fan(Device):
"""Main class representing the Xiaomi Smart Fan."""

def status(self):
def status(self) -> FanStatus:
"""Retrieve properties."""
properties = ['temp_dec', 'humidity', 'angle', 'speed',
'poweroff_time', 'power', 'ac_power', 'battery',
Expand Down
19 changes: 10 additions & 9 deletions miio/plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class PlugStatus:
"""Container for status reports from the plug."""

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

Expand Down Expand Up @@ -42,15 +43,7 @@ def __str__(self) -> str:
class Plug(Device):
"""Main class representing the smart wifi socket / plug."""

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def status(self):
def status(self) -> PlugStatus:
"""Retrieve properties."""
properties = ['power', 'temperature', 'current']
values = self.send(
Expand All @@ -68,3 +61,11 @@ def status(self):

return PlugStatus(
defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])
Loading

0 comments on commit ed80c50

Please sign in to comment.