mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Update python yeelight and add nightlight mode sensor (#22345)
This commit is contained in:
parent
2731777c7e
commit
6a74c403c0
@ -8,13 +8,15 @@ from homeassistant.components.discovery import SERVICE_YEELIGHT
|
|||||||
from homeassistant.const import CONF_DEVICES, CONF_NAME, CONF_SCAN_INTERVAL, \
|
from homeassistant.const import CONF_DEVICES, CONF_NAME, CONF_SCAN_INTERVAL, \
|
||||||
CONF_HOST, ATTR_ENTITY_ID
|
CONF_HOST, ATTR_ENTITY_ID
|
||||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
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 import discovery
|
||||||
from homeassistant.helpers.discovery import load_platform
|
from homeassistant.helpers.discovery import load_platform
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import dispatcher_send
|
from homeassistant.helpers.dispatcher import dispatcher_send
|
||||||
from homeassistant.helpers.event import track_time_interval
|
from homeassistant.helpers.event import track_time_interval
|
||||||
|
|
||||||
REQUIREMENTS = ['yeelight==0.4.3']
|
REQUIREMENTS = ['yeelight==0.4.4']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -40,9 +42,6 @@ ACTION_RECOVER = 'recover'
|
|||||||
ACTION_STAY = 'stay'
|
ACTION_STAY = 'stay'
|
||||||
ACTION_OFF = 'off'
|
ACTION_OFF = 'off'
|
||||||
|
|
||||||
MODE_MOONLIGHT = 'moonlight'
|
|
||||||
MODE_DAYLIGHT = 'normal'
|
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=30)
|
SCAN_INTERVAL = timedelta(seconds=30)
|
||||||
|
|
||||||
YEELIGHT_RGB_TRANSITION = 'RGBTransition'
|
YEELIGHT_RGB_TRANSITION = 'RGBTransition'
|
||||||
@ -90,11 +89,6 @@ YEELIGHT_SERVICE_SCHEMA = vol.Schema({
|
|||||||
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
|
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
})
|
})
|
||||||
|
|
||||||
NIGHTLIGHT_SUPPORTED_MODELS = [
|
|
||||||
"ceiling3",
|
|
||||||
'ceiling4'
|
|
||||||
]
|
|
||||||
|
|
||||||
UPDATE_REQUEST_PROPERTIES = [
|
UPDATE_REQUEST_PROPERTIES = [
|
||||||
"power",
|
"power",
|
||||||
"bright",
|
"bright",
|
||||||
@ -103,8 +97,7 @@ UPDATE_REQUEST_PROPERTIES = [
|
|||||||
"hue",
|
"hue",
|
||||||
"sat",
|
"sat",
|
||||||
"color_mode",
|
"color_mode",
|
||||||
"flowing",
|
"bg_power",
|
||||||
"music_on",
|
|
||||||
"nl_br",
|
"nl_br",
|
||||||
"active_mode",
|
"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, LIGHT_DOMAIN, DOMAIN, platform_config, hass_config)
|
||||||
|
load_platform(hass, BINARY_SENSOR_DOMAIN, DOMAIN, platform_config,
|
||||||
|
hass_config)
|
||||||
|
|
||||||
|
|
||||||
class YeelightDevice:
|
class YeelightDevice:
|
||||||
@ -218,7 +213,7 @@ class YeelightDevice:
|
|||||||
self._bulb_device = yeelight.Bulb(self._ipaddr,
|
self._bulb_device = yeelight.Bulb(self._ipaddr,
|
||||||
model=self._model)
|
model=self._model)
|
||||||
# force init for type
|
# force init for type
|
||||||
self._update_properties()
|
self.update()
|
||||||
|
|
||||||
except yeelight.BulbException as ex:
|
except yeelight.BulbException as ex:
|
||||||
_LOGGER.error("Failed to connect to bulb %s, %s: %s",
|
_LOGGER.error("Failed to connect to bulb %s, %s: %s",
|
||||||
@ -226,9 +221,6 @@ class YeelightDevice:
|
|||||||
|
|
||||||
return self._bulb_device
|
return self._bulb_device
|
||||||
|
|
||||||
def _update_properties(self):
|
|
||||||
self._bulb_device.get_properties(UPDATE_REQUEST_PROPERTIES)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the device if any."""
|
"""Return the name of the device if any."""
|
||||||
@ -252,6 +244,11 @@ class YeelightDevice:
|
|||||||
|
|
||||||
return self.bulb.last_properties.get('active_mode') == '1'
|
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):
|
def turn_on(self, duration=DEFAULT_TRANSITION):
|
||||||
"""Turn on device."""
|
"""Turn on device."""
|
||||||
import yeelight
|
import yeelight
|
||||||
@ -281,5 +278,5 @@ class YeelightDevice:
|
|||||||
if not self.bulb:
|
if not self.bulb:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._update_properties()
|
self._bulb_device.get_properties(UPDATE_REQUEST_PROPERTIES)
|
||||||
dispatcher_send(self._hass, DATA_UPDATED, self._ipaddr)
|
dispatcher_send(self._hass, DATA_UPDATED, self._ipaddr)
|
||||||
|
57
homeassistant/components/yeelight/binary_sensor.py
Normal file
57
homeassistant/components/yeelight/binary_sensor.py
Normal 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
|
@ -1821,7 +1821,7 @@ yahooweather==0.10
|
|||||||
yalesmartalarmclient==0.1.6
|
yalesmartalarmclient==0.1.6
|
||||||
|
|
||||||
# homeassistant.components.yeelight
|
# homeassistant.components.yeelight
|
||||||
yeelight==0.4.3
|
yeelight==0.4.4
|
||||||
|
|
||||||
# homeassistant.components.yeelightsunflower.light
|
# homeassistant.components.yeelightsunflower.light
|
||||||
yeelightsunflower==0.0.10
|
yeelightsunflower==0.0.10
|
||||||
|
Loading…
x
Reference in New Issue
Block a user