From 6a74c403c0ee6f9252ccfa434b29bcf2761c647d Mon Sep 17 00:00:00 2001 From: zewelor Date: Mon, 25 Mar 2019 19:06:43 +0100 Subject: [PATCH] Update python yeelight and add nightlight mode sensor (#22345) --- homeassistant/components/yeelight/__init__.py | 29 +++++----- .../components/yeelight/binary_sensor.py | 57 +++++++++++++++++++ requirements_all.txt | 2 +- 3 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 homeassistant/components/yeelight/binary_sensor.py diff --git a/homeassistant/components/yeelight/__init__.py b/homeassistant/components/yeelight/__init__.py index ed4e704a6a5..8a5c1e81a93 100644 --- a/homeassistant/components/yeelight/__init__.py +++ b/homeassistant/components/yeelight/__init__.py @@ -8,13 +8,15 @@ from homeassistant.components.discovery import SERVICE_YEELIGHT from homeassistant.const import CONF_DEVICES, CONF_NAME, CONF_SCAN_INTERVAL, \ CONF_HOST, ATTR_ENTITY_ID from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN +from homeassistant.components.binary_sensor import DOMAIN as \ + BINARY_SENSOR_DOMAIN from homeassistant.helpers import discovery from homeassistant.helpers.discovery import load_platform import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.event import track_time_interval -REQUIREMENTS = ['yeelight==0.4.3'] +REQUIREMENTS = ['yeelight==0.4.4'] _LOGGER = logging.getLogger(__name__) @@ -40,9 +42,6 @@ ACTION_RECOVER = 'recover' ACTION_STAY = 'stay' ACTION_OFF = 'off' -MODE_MOONLIGHT = 'moonlight' -MODE_DAYLIGHT = 'normal' - SCAN_INTERVAL = timedelta(seconds=30) YEELIGHT_RGB_TRANSITION = 'RGBTransition' @@ -90,11 +89,6 @@ YEELIGHT_SERVICE_SCHEMA = vol.Schema({ vol.Required(ATTR_ENTITY_ID): cv.entity_ids, }) -NIGHTLIGHT_SUPPORTED_MODELS = [ - "ceiling3", - 'ceiling4' -] - UPDATE_REQUEST_PROPERTIES = [ "power", "bright", @@ -103,8 +97,7 @@ UPDATE_REQUEST_PROPERTIES = [ "hue", "sat", "color_mode", - "flowing", - "music_on", + "bg_power", "nl_br", "active_mode", ] @@ -195,6 +188,8 @@ def _setup_device(hass, hass_config, ipaddr, device_config): ) load_platform(hass, LIGHT_DOMAIN, DOMAIN, platform_config, hass_config) + load_platform(hass, BINARY_SENSOR_DOMAIN, DOMAIN, platform_config, + hass_config) class YeelightDevice: @@ -218,7 +213,7 @@ class YeelightDevice: self._bulb_device = yeelight.Bulb(self._ipaddr, model=self._model) # force init for type - self._update_properties() + self.update() except yeelight.BulbException as ex: _LOGGER.error("Failed to connect to bulb %s, %s: %s", @@ -226,9 +221,6 @@ class YeelightDevice: return self._bulb_device - def _update_properties(self): - self._bulb_device.get_properties(UPDATE_REQUEST_PROPERTIES) - @property def name(self): """Return the name of the device if any.""" @@ -252,6 +244,11 @@ class YeelightDevice: return self.bulb.last_properties.get('active_mode') == '1' + @property + def is_nightlight_supported(self) -> bool: + """Return true / false if nightlight is supported.""" + return self.bulb.get_model_specs().get('night_light', False) + def turn_on(self, duration=DEFAULT_TRANSITION): """Turn on device.""" import yeelight @@ -281,5 +278,5 @@ class YeelightDevice: if not self.bulb: return - self._update_properties() + self._bulb_device.get_properties(UPDATE_REQUEST_PROPERTIES) dispatcher_send(self._hass, DATA_UPDATED, self._ipaddr) diff --git a/homeassistant/components/yeelight/binary_sensor.py b/homeassistant/components/yeelight/binary_sensor.py new file mode 100644 index 00000000000..cf7bbc5244e --- /dev/null +++ b/homeassistant/components/yeelight/binary_sensor.py @@ -0,0 +1,57 @@ +"""Sensor platform support for yeelight.""" +import logging + +from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.core import callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.components.yeelight import DATA_YEELIGHT, DATA_UPDATED + +DEPENDENCIES = ['yeelight'] + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the Yeelight sensors.""" + if not discovery_info: + return + + device = hass.data[DATA_YEELIGHT][discovery_info['host']] + + if device.is_nightlight_supported: + _LOGGER.debug("Adding nightlight mode sensor for %s", device.name) + add_entities([YeelightNightlightModeSensor(device)]) + + +class YeelightNightlightModeSensor(BinarySensorDevice): + """Representation of a Yeelight nightlight mode sensor.""" + + def __init__(self, device): + """Initialize nightlight mode sensor.""" + self._device = device + + @callback + def _schedule_immediate_update(self, ipaddr): + if ipaddr == self._device.ipaddr: + self.async_schedule_update_ha_state() + + async def async_added_to_hass(self): + """Handle entity which will be added.""" + async_dispatcher_connect( + self.hass, DATA_UPDATED, self._schedule_immediate_update + ) + + @property + def should_poll(self): + """No polling needed.""" + return False + + @property + def name(self): + """Return the name of the sensor.""" + return "{} nightlight".format(self._device.name) + + @property + def is_on(self): + """Return true if nightlight mode is on.""" + return self._device.is_nightlight_enabled diff --git a/requirements_all.txt b/requirements_all.txt index 7fd0c906ed7..f6814690309 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1821,7 +1821,7 @@ yahooweather==0.10 yalesmartalarmclient==0.1.6 # homeassistant.components.yeelight -yeelight==0.4.3 +yeelight==0.4.4 # homeassistant.components.yeelightsunflower.light yeelightsunflower==0.0.10