2019-03-25 19:06:43 +01:00
|
|
|
"""Sensor platform support for yeelight."""
|
|
|
|
import logging
|
|
|
|
|
2020-04-23 21:57:07 +02:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2020-08-31 22:40:56 +08:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-25 19:06:43 +01:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-12-09 13:10:24 +01:00
|
|
|
|
2020-08-31 22:40:56 +08:00
|
|
|
from . import DATA_CONFIG_ENTRIES, DATA_DEVICE, DATA_UPDATED, DOMAIN, YeelightEntity
|
2019-03-25 19:06:43 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-08-31 22:40:56 +08:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities
|
|
|
|
) -> None:
|
|
|
|
"""Set up Yeelight from a config entry."""
|
|
|
|
device = hass.data[DOMAIN][DATA_CONFIG_ENTRIES][config_entry.entry_id][DATA_DEVICE]
|
2019-03-25 19:06:43 +01:00
|
|
|
if device.is_nightlight_supported:
|
|
|
|
_LOGGER.debug("Adding nightlight mode sensor for %s", device.name)
|
2020-10-27 21:47:11 +08:00
|
|
|
async_add_entities([YeelightNightlightModeSensor(device, config_entry)])
|
2019-03-25 19:06:43 +01:00
|
|
|
|
|
|
|
|
2020-08-31 22:40:56 +08:00
|
|
|
class YeelightNightlightModeSensor(YeelightEntity, BinarySensorEntity):
|
2019-03-25 19:06:43 +01:00
|
|
|
"""Representation of a Yeelight nightlight mode sensor."""
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
2020-04-02 09:25:33 -07:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
2020-09-03 00:42:12 +08:00
|
|
|
DATA_UPDATED.format(self._device.host),
|
2020-04-02 09:25:33 -07:00
|
|
|
self.async_write_ha_state,
|
|
|
|
)
|
2019-03-25 19:06:43 +01:00
|
|
|
)
|
2021-08-09 20:33:34 +02:00
|
|
|
await super().async_added_to_hass()
|
2019-03-25 19:06:43 +01:00
|
|
|
|
2020-11-09 14:32:21 +07:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return f"{self._unique_id}-nightlight_sensor"
|
|
|
|
|
2019-03-25 19:06:43 +01:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 21:15:31 +02:00
|
|
|
return f"{self._device.name} nightlight"
|
2019-03-25 19:06:43 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if nightlight mode is on."""
|
|
|
|
return self._device.is_nightlight_enabled
|