Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor platform for mobile_app."""
2 
3 from typing import Any
4 
5 from homeassistant.components.binary_sensor import BinarySensorEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_WEBHOOK_ID, STATE_ON
8 from homeassistant.core import HomeAssistant, State, callback
9 from homeassistant.helpers import entity_registry as er
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import (
14  ATTR_SENSOR_ATTRIBUTES,
15  ATTR_SENSOR_DEVICE_CLASS,
16  ATTR_SENSOR_ENTITY_CATEGORY,
17  ATTR_SENSOR_ICON,
18  ATTR_SENSOR_NAME,
19  ATTR_SENSOR_STATE,
20  ATTR_SENSOR_TYPE,
21  ATTR_SENSOR_TYPE_BINARY_SENSOR as ENTITY_TYPE,
22  ATTR_SENSOR_UNIQUE_ID,
23  DOMAIN,
24 )
25 from .entity import MobileAppEntity
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up mobile app binary sensor from a config entry."""
34  entities = []
35 
36  webhook_id = config_entry.data[CONF_WEBHOOK_ID]
37 
38  entity_registry = er.async_get(hass)
39  entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
40  for entry in entries:
41  if entry.domain != ENTITY_TYPE or entry.disabled_by:
42  continue
43  config: dict[str, Any] = {
44  ATTR_SENSOR_ATTRIBUTES: {},
45  ATTR_SENSOR_DEVICE_CLASS: entry.device_class or entry.original_device_class,
46  ATTR_SENSOR_ICON: entry.original_icon,
47  ATTR_SENSOR_NAME: entry.original_name,
48  ATTR_SENSOR_STATE: None,
49  ATTR_SENSOR_TYPE: entry.domain,
50  ATTR_SENSOR_UNIQUE_ID: entry.unique_id,
51  ATTR_SENSOR_ENTITY_CATEGORY: entry.entity_category,
52  }
53  entities.append(MobileAppBinarySensor(config, config_entry))
54 
55  async_add_entities(entities)
56 
57  @callback
58  def handle_sensor_registration(data):
59  if data[CONF_WEBHOOK_ID] != webhook_id:
60  return
61 
62  async_add_entities([MobileAppBinarySensor(data, config_entry)])
63 
65  hass,
66  f"{DOMAIN}_{ENTITY_TYPE}_register",
67  handle_sensor_registration,
68  )
69 
70 
72  """Representation of a mobile app binary sensor."""
73 
74  async def async_restore_last_state(self, last_state: State) -> None:
75  """Restore previous state."""
76  await super().async_restore_last_state(last_state)
77  self._config_config[ATTR_SENSOR_STATE] = last_state.state == STATE_ON
78  self._async_update_attr_from_config_async_update_attr_from_config()
79 
80  @callback
81  def _async_update_attr_from_config(self) -> None:
82  """Update the entity from the config."""
84  self._attr_is_on_attr_is_on = self._config_config[ATTR_SENSOR_STATE]
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