Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Verisure binary sensors."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import ATTR_LAST_TRIP_TIME, EntityCategory
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity import Entity
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.update_coordinator import CoordinatorEntity
16 from homeassistant.util import dt as dt_util
17 
18 from .const import CONF_GIID, DOMAIN
19 from .coordinator import VerisureDataUpdateCoordinator
20 
21 
23  hass: HomeAssistant,
24  entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up Verisure binary sensors based on a config entry."""
28  coordinator: VerisureDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
29 
30  sensors: list[Entity] = [VerisureEthernetStatus(coordinator)]
31 
32  sensors.extend(
33  VerisureDoorWindowSensor(coordinator, serial_number)
34  for serial_number in coordinator.data["door_window"]
35  )
36 
37  async_add_entities(sensors)
38 
39 
41  CoordinatorEntity[VerisureDataUpdateCoordinator], BinarySensorEntity
42 ):
43  """Representation of a Verisure door window sensor."""
44 
45  _attr_device_class = BinarySensorDeviceClass.OPENING
46  _attr_has_entity_name = True
47 
48  def __init__(
49  self, coordinator: VerisureDataUpdateCoordinator, serial_number: str
50  ) -> None:
51  """Initialize the Verisure door window sensor."""
52  super().__init__(coordinator)
53  self._attr_unique_id_attr_unique_id = f"{serial_number}_door_window"
54  self.serial_numberserial_number = serial_number
55 
56  @property
57  def device_info(self) -> DeviceInfo:
58  """Return device information about this entity."""
59  area = self.coordinator.data["door_window"][self.serial_numberserial_number]["area"]
60  return DeviceInfo(
61  name=area,
62  manufacturer="Verisure",
63  model="Shock Sensor Detector",
64  identifiers={(DOMAIN, self.serial_numberserial_number)},
65  via_device=(DOMAIN, self.coordinator.entry.data[CONF_GIID]),
66  configuration_url="https://mypages.verisure.com",
67  )
68 
69  @property
70  def is_on(self) -> bool:
71  """Return the state of the sensor."""
72  return (
73  self.coordinator.data["door_window"][self.serial_numberserial_number]["state"] == "OPEN"
74  )
75 
76  @property
77  def available(self) -> bool:
78  """Return True if entity is available."""
79  return (
80  super().available
81  and self.serial_numberserial_number in self.coordinator.data["door_window"]
82  )
83 
84  @property
86  """Return the state attributes of the sensor."""
87  return {
88  ATTR_LAST_TRIP_TIME: dt_util.parse_datetime(
89  self.coordinator.data["door_window"][self.serial_numberserial_number]["reportTime"]
90  )
91  }
92 
93 
95  CoordinatorEntity[VerisureDataUpdateCoordinator], BinarySensorEntity
96 ):
97  """Representation of a Verisure VBOX internet status."""
98 
99  _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
100  _attr_entity_category = EntityCategory.DIAGNOSTIC
101  _attr_has_entity_name = True
102  _attr_translation_key = "ethernet"
103 
104  @property
105  def unique_id(self) -> str:
106  """Return the unique ID for this entity."""
107  return f"{self.coordinator.entry.data[CONF_GIID]}_ethernet"
108 
109  @property
110  def device_info(self) -> DeviceInfo:
111  """Return device information about this entity."""
112  return DeviceInfo(
113  name="Verisure Alarm",
114  manufacturer="Verisure",
115  model="VBox",
116  identifiers={(DOMAIN, self.coordinator.entry.data[CONF_GIID])},
117  configuration_url="https://mypages.verisure.com",
118  )
119 
120  @property
121  def is_on(self) -> bool:
122  """Return the state of the sensor."""
123  return self.coordinator.data["broadband"]["isBroadbandConnected"]
124 
125  @property
126  def available(self) -> bool:
127  """Return True if entity is available."""
128  return super().available and self.coordinator.data["broadband"] is not None
None __init__(self, VerisureDataUpdateCoordinator coordinator, str serial_number)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)