Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for EZVIZ binary sensors."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8  BinarySensorEntityDescription,
9 )
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import DATA_COORDINATOR, DOMAIN
15 from .coordinator import EzvizDataUpdateCoordinator
16 from .entity import EzvizEntity
17 
18 PARALLEL_UPDATES = 1
19 
20 BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
21  "Motion_Trigger": BinarySensorEntityDescription(
22  key="Motion_Trigger",
23  device_class=BinarySensorDeviceClass.MOTION,
24  ),
25  "alarm_schedules_enabled": BinarySensorEntityDescription(
26  key="alarm_schedules_enabled",
27  translation_key="alarm_schedules_enabled",
28  ),
29  "encrypted": BinarySensorEntityDescription(
30  key="encrypted",
31  translation_key="encrypted",
32  ),
33 }
34 
35 
37  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
38 ) -> None:
39  """Set up EZVIZ sensors based on a config entry."""
40  coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
41  DATA_COORDINATOR
42  ]
43 
45  [
46  EzvizBinarySensor(coordinator, camera, binary_sensor)
47  for camera in coordinator.data
48  for binary_sensor, value in coordinator.data[camera].items()
49  if binary_sensor in BINARY_SENSOR_TYPES
50  if value is not None
51  ]
52  )
53 
54 
56  """Representation of a EZVIZ sensor."""
57 
58  def __init__(
59  self,
60  coordinator: EzvizDataUpdateCoordinator,
61  serial: str,
62  binary_sensor: str,
63  ) -> None:
64  """Initialize the sensor."""
65  super().__init__(coordinator, serial)
66  self._sensor_name_sensor_name = binary_sensor
67  self._attr_unique_id_attr_unique_id = f"{serial}_{self._camera_name}.{binary_sensor}"
68  self.entity_descriptionentity_description = BINARY_SENSOR_TYPES[binary_sensor]
69 
70  @property
71  def is_on(self) -> bool:
72  """Return the state of the sensor."""
73  return self.datadatadata[self._sensor_name_sensor_name]
None __init__(self, EzvizDataUpdateCoordinator coordinator, str serial, str binary_sensor)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)