Skip to content

Commit

Permalink
Some comments improved.
Browse files Browse the repository at this point in the history
Additional properties of the Xiaomi Air Purifier 2 introduced.
Some code clean-up.
  • Loading branch information
syssi committed Nov 26, 2017
1 parent 3bed026 commit e516138
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 61 deletions.
92 changes: 65 additions & 27 deletions miio/airpurifier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import enum
import math
from typing import Any, Dict, Optional
from collections import defaultdict
from .device import Device
Expand Down Expand Up @@ -36,14 +37,13 @@ def __init__(self, data: Dict[str, Any]) -> None:
Response of a Air Purifier 2:
['power': 'off', 'aqi': 141, 'humidity': 64, 'temp_dec': 236,
'mode': 'auto', 'led': 'on', 'led_b': 1, 'buzzer': 'on',
'child_lock': 'off', 'limit_hum': null, 'trans_level': null,
'bright': null, 'favorite_level': 10, 'filter1_life': 80,
'act_det': null, 'f1_hour_used': 680 ]
['power': 'on', 'aqi': 22, 'humidity': 61, 'temp_dec': 197,
'mode': 'auto', 'led': 2, 'led_b': 'off', 'buzzer': 'off',
'child_lock': 'off', 'bright': None, 'favorite_level': 10,
'filter1_life': 80, 'f1_hour_used': 678, 'use_time': 2440800,
'motor1_speed': 351, 'purify_volume': 25127]
use_time and motor1_speed is missing because a request is limitted
to 16 properties. We request 15 properties at the moment.
A request is limitted to 16 properties.
"""

self.data = data
Expand All @@ -63,6 +63,11 @@ def aqi(self) -> int:
"""Air quality index."""
return self.data["aqi"]

@property
def average_aqi(self) -> int:
"""Average of the air quality index."""
return self.data["averageaqi"]

@property
def humidity(self) -> int:
"""Current humidity."""
Expand Down Expand Up @@ -90,6 +95,11 @@ def led_brightness(self) -> Optional[LedBrightness]:
"""Brightness of the LED."""
if self.data["led_b"] is not None:
return LedBrightness(self.data["led_b"])

# This is the property name of the Air Purifier Pro
if self.data["bright"] is not None:
return LedBrightness(self.data["bright"])

return None

@property
Expand All @@ -102,11 +112,6 @@ def child_lock(self) -> bool:
"""Return True if child lock is on."""
return self.data["child_lock"] == "on"

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

@property
def favorite_level(self) -> int:
"""Return favorite level, which is used if the mode is ``favorite``."""
Expand All @@ -125,24 +130,51 @@ def filter_hours_used(self) -> int:

@property
def use_time(self) -> int:
"""How long the device has been active FIXME"""
"""How long the device has been active in seconds."""
return self.data["use_time"]

@property
def purify_volume(self) -> int:
"""The volume of purified air in cubic meter."""
return self.data["purify_volume"]

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

def __str__(self) -> str:
s = "<AirPurifierStatus power=%s, aqi=%s temperature=%s, " \
"humidity=%s%%, mode=%s, led=%s, led_brightness=%s, buzzer=%s, " \
"child_lock=%s, brightness=%s, favorite_level=%s, " \
"filter_life_remaining=%s, filter_hours_used=%s, " \
"use_time=%s, motor_speed=%s>" % \
(self.power, self.aqi, self.temperature, self.humidity, self.mode,
self.led, self.led_brightness, self.buzzer, self.child_lock,
self.brightness, self.favorite_level, self.filter_life_remaining,
self.filter_hours_used, self.use_time,
s = "<AirPurifierStatus power=%s, " \
"aqi=%s," \
"average_aqi=%s," \
"temperature=%s, " \
"humidity=%s%%," \
"mode=%s," \
"led=%s," \
"led_brightness=%s," \
"buzzer=%s, " \
"child_lock=%s," \
"favorite_level=%s," \
"filter_life_remaining=%s, " \
"filter_hours_used=%s, " \
"use_time=%s, " \
"purify_volume=%s, " \
"motor_speed=%s>" % \
(self.power,
self.aqi,
self.average_aqi,
self.temperature,
self.humidity,
self.mode,
self.led,
self.led_brightness,
self.buzzer,
self.child_lock,
self.favorite_level,
self.filter_life_remaining,
self.filter_hours_used,
self.use_time,
self.purify_volume,
self.motor_speed)
return s

Expand All @@ -153,16 +185,22 @@ class AirPurifier(Device):
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']
properties = ['power', 'aqi', 'averageaqi', 'humidity', 'temp_dec',
'mode', 'favorite_level', 'filter1_life', 'f1_hour_used',
'use_time', 'motor1_speed', 'purify_volume', 'f1_hour',
# Second request
'led', 'led_b', 'bright', 'buzzer', 'child_lock', ]

values = self.send(
"get_prop",
properties
properties[0:13]
)

values.extend(self.send(
"get_prop",
properties[14:]
))

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
Expand Down
12 changes: 6 additions & 6 deletions miio/ceil.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def brightness(self) -> int:

@property
def scene(self) -> int:
"""Current scene. FIXME what is this?"""
"""Current fixed scene (brightness & colortemp)."""
return self.data["snm"]

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

@property
Expand Down Expand Up @@ -107,12 +107,12 @@ def set_color_temperature(self, level: int):
return self.send("set_cct", [level])

def delay_off(self, seconds: int):
"""Set delay off seconds."""
"""Turn off delay in seconds."""
return self.send("delay_off", [seconds])

def set_scene(self, num: int):
"""Set scene number."""
return self.send("apply_fixed_scene", [num])
def set_scene(self, number: int):
"""Set a fixed scene. 4 fixed scenes are available (1-4)"""
return self.send("apply_fixed_scene", [number])

def smart_night_light_on(self):
"""Smart Night Light On."""
Expand Down
7 changes: 4 additions & 3 deletions miio/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def child_lock(self) -> bool:

@property
def natural_level(self) -> int:
"""Natural level. FIXME what is this?"""
"""Fan speed in natural mode."""
return self.data["natural_level"]

@property
def speed_level(self) -> int:
"""Speed level. FIXME how does this compare to speed?"""
"""Fan speed in direct mode."""
return self.data["speed_level"]

@property
Expand All @@ -105,7 +105,8 @@ def poweroff_time(self) -> int:

@property
def speed(self) -> int:
"""Current speed. FIXME how does this compare to speed_level?"""
"""FIXME What is the meaning of this value?
(cp. speed_level vs. natural_level)"""
return self.data["speed"]

@property
Expand Down
40 changes: 20 additions & 20 deletions miio/philips_eyecare.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@ def brightness(self) -> int:

@property
def reminder(self) -> bool:
"""True if reminder is on. FIXME be more descriptive"""
"""Indicates the eye fatigue notification is enabled or not."""
return self.data["notifystatus"] == "on"

@property
def ambient(self) -> bool:
"""True if ambient is on. FIXME be more descriptive."""
"""True if the ambient light (second light source) is on."""
return self.data["ambstatus"] == "on"

@property
def ambient_brightness(self) -> int:
"""Ambient brightness level."""
"""Brightness of the ambient light."""
return self.data["ambvalue"]

@property
def eyecare(self) -> bool:
"""True if eyecare is on."""
"""True if the eyecare light (first light source) is on."""
return self.data["eyecare"] == "on"

@property
def scene(self) -> int:
"""Current scene."""
"""Current fixed scene."""
return self.data["scene_num"]

@property
def smart_night_light(self) -> bool:
"""True if smart night light is on."""
"""True if the smart night light mode is on."""
return self.data["bls"] == "on"

@property
def delay_off_countdown(self) -> int:
"""Current delay off counter."""
"""Countdown until turning off in minutes."""
return self.data["dvalue"]

def __str__(self) -> str:
Expand Down Expand Up @@ -109,49 +109,49 @@ def off(self):
return self.send("set_power", ["off"])

def eyecare_on(self):
"""Eyecare on."""
"""Turn the eyecare light on."""
return self.send("set_eyecare", ["on"])

def eyecare_off(self):
"""Eyecare off."""
"""Turn the eyecare light off."""
return self.send("set_eyecare", ["off"])

def set_brightness(self, level: int):
"""Set brightness level."""
return self.send("set_bright", [level])

def set_scene(self, num: int):
"""Set eyecare user scene."""
return self.send("set_user_scene", [num])
def set_scene(self, number: int):
"""Set one of the fixed eyecare user scenes."""
return self.send("set_user_scene", [number])

def delay_off(self, minutes: int):
"""Set delay off minutes."""
"""Turn off delay in minutes."""
return self.send("delay_off", [minutes])

def smart_night_light_on(self):
"""Night Light On."""
"""Turn the smart night light mode on."""
return self.send("enable_bl", ["on"])

def smart_night_light_off(self):
"""Night Light Off."""
"""Turn the smart night light mode off."""
return self.send("enable_bl", ["off"])

def reminder_on(self):
"""Eye Fatigue Reminder On."""
"""Enable the eye fatigue reminder / notification."""
return self.send("set_notifyuser", ["on"])

def reminder_off(self):
"""Eye Fatigue Reminder Off."""
"""Disable the eye fatigue reminder / notification."""
return self.send("set_notifyuser", ["off"])

def ambient_on(self):
"""Amblient Light On."""
"""Turn the ambient light on."""
return self.send("enable_amb", ["on"])

def ambient_off(self):
"""Ambient Light Off."""
"""Turn the ambient light off."""
return self.send("enable_amb", ["off"])

def set_ambient_brightness(self, level: int):
"""Set Ambient Light brightness level."""
"""Set the brightness of the ambient light."""
return self.send("set_amb_bright", [level])
10 changes: 5 additions & 5 deletions miio/philips_eyecare_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pass_dev = click.make_pass_decorator(miio.PhilipsEyecare)


def validate_bright(ctx, param, value):
def validate_brightness(ctx, param, value):
value = int(value)
if value < 1 or value > 100:
raise click.BadParameter('Should be a positive int between 1-100.')
Expand Down Expand Up @@ -123,9 +123,9 @@ def off(dev: miio.PhilipsEyecare):


@cli.command()
@click.argument('level', callback=validate_bright, required=True,)
@click.argument('level', callback=validate_brightness, required=True,)
@pass_dev
def set_bright(dev: miio.PhilipsEyecare, level):
def set_brightness(dev: miio.PhilipsEyecare, level):
"""Set brightness level."""
click.echo("Brightness: %s" % dev.set_brightness(level))

Expand Down Expand Up @@ -189,9 +189,9 @@ def ambient_off(dev: miio.PhilipsEyecare):


@cli.command()
@click.argument('level', callback=validate_bright, required=True,)
@click.argument('level', callback=validate_brightness, required=True,)
@pass_dev
def set_amb_bright(dev: miio.PhilipsEyecare, level):
def set_ambient_brightness(dev: miio.PhilipsEyecare, level):
"""Set Ambient Light brightness level."""
click.echo("Ambient Light Brightness: %s" %
dev.set_ambient_brightness(level))
Expand Down

0 comments on commit e516138

Please sign in to comment.