From 03a5d4e131b4967860c8c22aa6467d9fdb5972d9 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Fri, 19 Jan 2018 08:50:56 +0100 Subject: [PATCH] Additional attributes and services of the Xiaomi Air Purifier introduced (#11249) * Attributes average_aqi and purify_volume introduced. Fixes https://github.com/syssi/xiaomi_airpurifier/issues/14. New service light.xiaomi_miio_set_child_lock_{on,off} added. Fixes https://github.com/syssi/xiaomi_airpurifier/issues/13. * Lazy loading of service descriptions. * Merge conflict resolved. --- homeassistant/components/fan/services.yaml | 62 +++++++++++++++++++ homeassistant/components/fan/xiaomi_miio.py | 50 +++++++++++---- .../components/fan/xiaomi_miio_services.yaml | 56 ----------------- 3 files changed, 99 insertions(+), 69 deletions(-) delete mode 100644 homeassistant/components/fan/xiaomi_miio_services.yaml diff --git a/homeassistant/components/fan/services.yaml b/homeassistant/components/fan/services.yaml index 2a8ad453ec8ea1..a306cf7767c758 100644 --- a/homeassistant/components/fan/services.yaml +++ b/homeassistant/components/fan/services.yaml @@ -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 diff --git a/homeassistant/components/fan/xiaomi_miio.py b/homeassistant/components/fan/xiaomi_miio.py index 9f21fda408d2ea..694a68d7f8e427 100644 --- a/homeassistant/components/fan/xiaomi_miio.py +++ b/homeassistant/components/fan/xiaomi_miio.py @@ -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' @@ -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' @@ -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}, @@ -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) @@ -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 @@ -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: @@ -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.""" diff --git a/homeassistant/components/fan/xiaomi_miio_services.yaml b/homeassistant/components/fan/xiaomi_miio_services.yaml deleted file mode 100644 index 93f6318e60bc3e..00000000000000 --- a/homeassistant/components/fan/xiaomi_miio_services.yaml +++ /dev/null @@ -1,56 +0,0 @@ - -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_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'