Update python yeelight and add nightlight mode sensor (#22345)

This commit is contained in:
zewelor 2019-03-25 19:06:43 +01:00 committed by Sebastian Muszynski
parent 2731777c7e
commit 6a74c403c0
3 changed files with 71 additions and 17 deletions

View File

@ -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)

View File

@ -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

View File

@ -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