Skip to content

Commit

Permalink
Additional attributes and services of the Xiaomi Air Purifier introdu…
Browse files Browse the repository at this point in the history
…ced (#11249)

* Attributes average_aqi and purify_volume introduced. Fixes syssi/xiaomi_airpurifier#14.
New service light.xiaomi_miio_set_child_lock_{on,off} added. Fixes syssi/xiaomi_airpurifier#13.

* Lazy loading of service descriptions.

* Merge conflict resolved.
  • Loading branch information
syssi authored and balloob committed Jan 19, 2018
1 parent 5de828d commit 03a5d4e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 69 deletions.
62 changes: 62 additions & 0 deletions homeassistant/components/fan/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,65 @@ dyson_set_night_mode:
night_mode:
description: Night mode status
example: true

xiaomi_miio_set_buzzer_on:
description: Turn the buzzer on.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'

xiaomi_miio_set_buzzer_off:
description: Turn the buzzer off.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'

xiaomi_miio_set_led_on:
description: Turn the led on.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'

xiaomi_miio_set_led_off:
description: Turn the led off.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'

xiaomi_miio_set_child_lock_on:
description: Turn the child lock on.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'

xiaomi_miio_set_child_lock_off:
description: Turn the child lock off.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'

xiaomi_miio_set_favorite_level:
description: Set the favorite level.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'
level:
description: Level, between 0 and 16.
example: 1

xiaomi_miio_set_led_brightness:
description: Set the led brightness.
fields:
entity_id:
description: Name of the air purifier entity.
example: 'fan.xiaomi_air_purifier'
brightness:
description: Brightness (0 = Bright, 1 = Dim, 2 = Off)
example: 1
50 changes: 37 additions & 13 deletions homeassistant/components/fan/xiaomi_miio.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
ATTR_LED = 'led'
ATTR_LED_BRIGHTNESS = 'led_brightness'
ATTR_MOTOR_SPEED = 'motor_speed'
ATTR_AVERAGE_AIR_QUALITY_INDEX = 'average_aqi'
ATTR_PURIFY_VOLUME = 'purify_volume'

ATTR_BRIGHTNESS = 'brightness'
ATTR_LEVEL = 'level'
Expand All @@ -53,6 +55,8 @@
SERVICE_SET_BUZZER_OFF = 'xiaomi_miio_set_buzzer_off'
SERVICE_SET_LED_ON = 'xiaomi_miio_set_led_on'
SERVICE_SET_LED_OFF = 'xiaomi_miio_set_led_off'
SERVICE_SET_CHILD_LOCK_ON = 'xiaomi_miio_set_child_lock_on'
SERVICE_SET_CHILD_LOCK_OFF = 'xiaomi_miio_set_child_lock_off'
SERVICE_SET_FAVORITE_LEVEL = 'xiaomi_miio_set_favorite_level'
SERVICE_SET_LED_BRIGHTNESS = 'xiaomi_miio_set_led_brightness'

Expand All @@ -75,6 +79,8 @@
SERVICE_SET_BUZZER_OFF: {'method': 'async_set_buzzer_off'},
SERVICE_SET_LED_ON: {'method': 'async_set_led_on'},
SERVICE_SET_LED_OFF: {'method': 'async_set_led_off'},
SERVICE_SET_CHILD_LOCK_ON: {'method': 'async_set_child_lock_on'},
SERVICE_SET_CHILD_LOCK_OFF: {'method': 'async_set_child_lock_off'},
SERVICE_SET_FAVORITE_LEVEL: {
'method': 'async_set_favorite_level',
'schema': SERVICE_SCHEMA_FAVORITE_LEVEL},
Expand Down Expand Up @@ -116,15 +122,15 @@ def async_service_handler(service):
if key != ATTR_ENTITY_ID}
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
target_air_purifiers = [air for air in hass.data[PLATFORM].values()
if air.entity_id in entity_ids]
devices = [device for device in hass.data[PLATFORM].values() if
device.entity_id in entity_ids]
else:
target_air_purifiers = hass.data[PLATFORM].values()
devices = hass.data[PLATFORM].values()

update_tasks = []
for air_purifier in target_air_purifiers:
yield from getattr(air_purifier, method['method'])(**params)
update_tasks.append(air_purifier.async_update_ha_state(True))
for device in devices:
yield from getattr(device, method['method'])(**params)
update_tasks.append(device.async_update_ha_state(True))

if update_tasks:
yield from asyncio.wait(update_tasks, loop=hass.loop)
Expand Down Expand Up @@ -157,7 +163,9 @@ def __init__(self, name, air_purifier):
ATTR_CHILD_LOCK: None,
ATTR_LED: None,
ATTR_LED_BRIGHTNESS: None,
ATTR_MOTOR_SPEED: None
ATTR_MOTOR_SPEED: None,
ATTR_AVERAGE_AIR_QUALITY_INDEX: None,
ATTR_PURIFY_VOLUME: None,
}

@property
Expand Down Expand Up @@ -244,7 +252,9 @@ def async_update(self):
ATTR_BUZZER: state.buzzer,
ATTR_CHILD_LOCK: state.child_lock,
ATTR_LED: state.led,
ATTR_MOTOR_SPEED: state.motor_speed
ATTR_MOTOR_SPEED: state.motor_speed,
ATTR_AVERAGE_AIR_QUALITY_INDEX: state.average_aqi,
ATTR_PURIFY_VOLUME: state.purify_volume,
}

if state.led_brightness:
Expand Down Expand Up @@ -284,30 +294,44 @@ def async_set_speed(self: ToggleEntity, speed: str) -> None:
def async_set_buzzer_on(self):
"""Turn the buzzer on."""
yield from self._try_command(
"Turning the buzzer of air purifier on failed.",
"Turning the buzzer of the air purifier on failed.",
self._air_purifier.set_buzzer, True)

@asyncio.coroutine
def async_set_buzzer_off(self):
"""Turn the buzzer on."""
"""Turn the buzzer off."""
yield from self._try_command(
"Turning the buzzer of air purifier off failed.",
"Turning the buzzer of the air purifier off failed.",
self._air_purifier.set_buzzer, False)

@asyncio.coroutine
def async_set_led_on(self):
"""Turn the led on."""
yield from self._try_command(
"Turning the led of air purifier off failed.",
"Turning the led of the air purifier off failed.",
self._air_purifier.set_led, True)

@asyncio.coroutine
def async_set_led_off(self):
"""Turn the led off."""
yield from self._try_command(
"Turning the led of air purifier off failed.",
"Turning the led of the air purifier off failed.",
self._air_purifier.set_led, False)

@asyncio.coroutine
def async_set_child_lock_on(self):
"""Turn the child lock on."""
yield from self._try_command(
"Turning the child lock of the air purifier on failed.",
self._air_purifier.set_child_lock, True)

@asyncio.coroutine
def async_set_child_lock_off(self):
"""Turn the child lock off."""
yield from self._try_command(
"Turning the child lock of the air purifier off failed.",
self._air_purifier.set_child_lock, False)

@asyncio.coroutine
def async_set_led_brightness(self, brightness: int=2):
"""Set the led brightness."""
Expand Down
56 changes: 0 additions & 56 deletions homeassistant/components/fan/xiaomi_miio_services.yaml

This file was deleted.

0 comments on commit 03a5d4e

Please sign in to comment.