Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for Lutron devices."""
2 
3 from pylutron import Keypad, Lutron, LutronEntity, LutronEvent
4 
5 from homeassistant.const import ATTR_IDENTIFIERS, ATTR_VIA_DEVICE
6 from homeassistant.helpers.device_registry import DeviceInfo
7 from homeassistant.helpers.entity import Entity
8 
9 from .const import DOMAIN
10 
11 
13  """Base class for Lutron entities."""
14 
15  _attr_should_poll = False
16  _attr_has_entity_name = True
17 
18  def __init__(
19  self, area_name: str, lutron_device: LutronEntity, controller: Lutron
20  ) -> None:
21  """Initialize the device."""
22  self._lutron_device_lutron_device = lutron_device
23  self._controller_controller = controller
24  self._area_name_area_name = area_name
25 
26  async def async_added_to_hass(self) -> None:
27  """Register callbacks."""
28  self._lutron_device_lutron_device.subscribe(self._update_callback_update_callback, None)
29 
30  def _request_state(self) -> None:
31  """Request the state."""
32 
33  def _update_attrs(self) -> None:
34  """Update the entity's attributes."""
35 
37  self, _device: LutronEntity, _context: None, _event: LutronEvent, _params: dict
38  ) -> None:
39  """Run when invoked by pylutron when the device state changes."""
40  self._update_attrs()
41  self.schedule_update_ha_state()
42 
43  @property
44  def unique_id(self) -> str:
45  """Return a unique ID."""
46 
47  if self._lutron_device_lutron_device.uuid is None:
48  return f"{self._controller.guid}_{self._lutron_device.legacy_uuid}"
49  return f"{self._controller.guid}_{self._lutron_device.uuid}"
50 
51  def update(self) -> None:
52  """Update the entity's state."""
53  self._request_state_request_state()
54  self._update_attrs_update_attrs()
55 
56 
58  """Representation of a Lutron device entity."""
59 
60  def __init__(
61  self, area_name: str, lutron_device: LutronEntity, controller: Lutron
62  ) -> None:
63  """Initialize the device."""
64  super().__init__(area_name, lutron_device, controller)
65  self._attr_device_info_attr_device_info = DeviceInfo(
66  identifiers={(DOMAIN, self.unique_idunique_idunique_id)},
67  manufacturer="Lutron",
68  name=lutron_device.name,
69  suggested_area=area_name,
70  via_device=(DOMAIN, controller.guid),
71  )
72 
73 
75  """Representation of a Lutron Keypad."""
76 
77  def __init__(
78  self,
79  area_name: str,
80  lutron_device: LutronEntity,
81  controller: Lutron,
82  keypad: Keypad,
83  ) -> None:
84  """Initialize the device."""
85  super().__init__(area_name, lutron_device, controller)
86  self._attr_device_info_attr_device_info = DeviceInfo(
87  identifiers={(DOMAIN, keypad.id)},
88  manufacturer="Lutron",
89  name=keypad.name,
90  )
91  if keypad.type == "MAIN_REPEATER":
92  self._attr_device_info_attr_device_info[ATTR_IDENTIFIERS].add((DOMAIN, controller.guid))
93  else:
94  self._attr_device_info_attr_device_info[ATTR_VIA_DEVICE] = (DOMAIN, controller.guid)
None _update_callback(self, LutronEntity _device, None _context, LutronEvent _event, dict _params)
Definition: entity.py:38
None __init__(self, str area_name, LutronEntity lutron_device, Lutron controller)
Definition: entity.py:20
None __init__(self, str area_name, LutronEntity lutron_device, Lutron controller)
Definition: entity.py:62
None __init__(self, str area_name, LutronEntity lutron_device, Lutron controller, Keypad keypad)
Definition: entity.py:83
bool add(self, _T matcher)
Definition: match.py:185
Callable[[], None] subscribe(HomeAssistant hass, str topic, MessageCallbackType msg_callback, int qos=DEFAULT_QOS, str encoding="utf-8")
Definition: client.py:247