Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Vera devices."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import pyvera as veraApi
9 
10 from homeassistant.const import (
11  ATTR_ARMED,
12  ATTR_BATTERY_LEVEL,
13  ATTR_LAST_TRIP_TIME,
14  ATTR_TRIPPED,
15 )
16 from homeassistant.helpers.entity import Entity
17 from homeassistant.util import slugify
18 from homeassistant.util.dt import utc_from_timestamp
19 
20 from .common import ControllerData
21 from .const import CONF_LEGACY_UNIQUE_ID, VERA_ID_FORMAT
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 class VeraEntity[_DeviceTypeT: veraApi.VeraDevice](Entity):
27  """Representation of a Vera device entity."""
28 
29  def __init__(
30  self, vera_device: _DeviceTypeT, controller_data: ControllerData
31  ) -> None:
32  """Initialize the device."""
33  self.vera_device = vera_device
34  self.controller = controller_data.controller
35 
36  self._name = self.vera_device.name
37  # Append device id to prevent name clashes in HA.
38  self.vera_id = VERA_ID_FORMAT.format(
39  slugify(vera_device.name), vera_device.vera_device_id
40  )
41 
42  if controller_data.config_entry.data.get(CONF_LEGACY_UNIQUE_ID):
43  self._unique_id = str(self.vera_device.vera_device_id)
44  else:
45  self._unique_id = f"vera_{controller_data.config_entry.unique_id}_{self.vera_device.vera_device_id}"
46 
47  async def async_added_to_hass(self) -> None:
48  """Subscribe to updates."""
49  self.controller.register(self.vera_device, self._update_callback)
50 
51  def _update_callback(self, _device: _DeviceTypeT) -> None:
52  """Update the state."""
53  self.schedule_update_ha_state(True)
54 
55  def update(self):
56  """Force a refresh from the device if the device is unavailable."""
57  refresh_needed = self.vera_device.should_poll or not self.available
58  _LOGGER.debug("%s: update called (refresh=%s)", self._name, refresh_needed)
59  if refresh_needed:
60  self.vera_device.refresh()
61 
62  @property
63  def name(self) -> str:
64  """Return the name of the device."""
65  return self._name
66 
67  @property
68  def extra_state_attributes(self) -> dict[str, Any] | None:
69  """Return the state attributes of the device."""
70  attr = {}
71 
72  if self.vera_device.has_battery:
73  attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level
74 
75  if self.vera_device.is_armable:
76  armed = self.vera_device.is_armed
77  attr[ATTR_ARMED] = "True" if armed else "False"
78 
79  if self.vera_device.is_trippable:
80  if (last_tripped := self.vera_device.last_trip) is not None:
81  utc_time = utc_from_timestamp(int(last_tripped))
82  attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
83  else:
84  attr[ATTR_LAST_TRIP_TIME] = None
85  tripped = self.vera_device.is_tripped
86  attr[ATTR_TRIPPED] = "True" if tripped else "False"
87 
88  attr["Vera Device Id"] = self.vera_device.vera_device_id
89 
90  return attr
91 
92  @property
93  def available(self):
94  """If device communications have failed return false."""
95  return not self.vera_device.comm_failure
96 
97  @property
98  def unique_id(self) -> str:
99  """Return a unique ID.
100 
101  The Vera assigns a unique and immutable ID number to each device.
102  """
103  return self._unique_id
def register(HomeAssistant hass, Heos controller)
Definition: services.py:29
None __init__(self, _DeviceTypeT vera_device, ControllerData controller_data)
Definition: entity.py:31
dict[str, Any]|None extra_state_attributes(self)
Definition: entity.py:68
None _update_callback(self, _DeviceTypeT _device)
Definition: entity.py:51