Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Sensor platform support for yeelight."""
2 
3 import logging
4 
5 from homeassistant.components.binary_sensor import BinarySensorEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.dispatcher import async_dispatcher_connect
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from .const import DATA_CONFIG_ENTRIES, DATA_DEVICE, DATA_UPDATED, DOMAIN
12 from .entity import YeelightEntity
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
18  hass: HomeAssistant,
19  config_entry: ConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up Yeelight from a config entry."""
23  device = hass.data[DOMAIN][DATA_CONFIG_ENTRIES][config_entry.entry_id][DATA_DEVICE]
24  if device.is_nightlight_supported:
25  _LOGGER.debug("Adding nightlight mode sensor for %s", device.name)
26  async_add_entities([YeelightNightlightModeSensor(device, config_entry)])
27 
28 
30  """Representation of a Yeelight nightlight mode sensor."""
31 
32  _attr_translation_key = "nightlight"
33 
34  async def async_added_to_hass(self) -> None:
35  """Handle entity which will be added."""
36  self.async_on_removeasync_on_remove(
38  self.hasshass,
39  DATA_UPDATED.format(self._device_device.host),
40  self.async_write_ha_stateasync_write_ha_state,
41  )
42  )
43  await super().async_added_to_hass()
44 
45  @property
46  def unique_id(self) -> str:
47  """Return a unique ID."""
48  return f"{self._unique_id}-nightlight_sensor"
49 
50  @property
51  def is_on(self):
52  """Return true if nightlight mode is on."""
53  return self._device_device.is_nightlight_enabled
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103