Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Implementation of a base class for all IHC devices."""
2 
3 import logging
4 
5 from ihcsdk.ihccontroller import IHCController
6 
7 from homeassistant.helpers.entity import Entity
8 
9 from .const import CONF_INFO, DOMAIN
10 
11 _LOGGER = logging.getLogger(__name__)
12 
13 
15  """Base class for all IHC devices.
16 
17  All IHC devices have an associated IHC resource. IHCEntity handled the
18  registration of the IHC controller callback when the IHC resource changes.
19  Derived classes must implement the on_ihc_change method
20  """
21 
22  _attr_should_poll = False
23 
24  def __init__(
25  self,
26  ihc_controller: IHCController,
27  controller_id: str,
28  name: str,
29  ihc_id: int,
30  product=None,
31  ) -> None:
32  """Initialize IHC attributes."""
33  self.ihc_controllerihc_controller = ihc_controller
34  self._name_name = name
35  self.ihc_idihc_id = ihc_id
36  self.controller_idcontroller_id = controller_id
37  self.device_iddevice_id = None
38  self.suggested_areasuggested_area = None
39  if product:
40  self.ihc_nameihc_name = product["name"]
41  self.ihc_noteihc_note = product["note"]
42  self.ihc_positionihc_position = product["position"]
43  self.suggested_areasuggested_area = product.get("group")
44  if "id" in product:
45  product_id = product["id"]
46  self.device_iddevice_id = f"{controller_id}_{product_id }"
47  # this will name the device the same way as the IHC visual application: Product name + position
48  self.device_namedevice_name = product["name"]
49  if self.ihc_positionihc_position:
50  self.device_namedevice_name += f" ({self.ihc_position})"
51  self.device_modeldevice_model = product["model"]
52  else:
53  self.ihc_nameihc_name = ""
54  self.ihc_noteihc_note = ""
55  self.ihc_positionihc_position = ""
56 
57  async def async_added_to_hass(self):
58  """Add callback for IHC changes."""
59  _LOGGER.debug("Adding IHC entity notify event: %s", self.ihc_idihc_id)
60  self.ihc_controllerihc_controller.add_notify_event(self.ihc_idihc_id, self.on_ihc_changeon_ihc_change, True)
61 
62  @property
63  def name(self):
64  """Return the device name."""
65  return self._name_name
66 
67  @property
68  def unique_id(self):
69  """Return a unique ID."""
70  return f"{self.controller_id}-{self.ihc_id}"
71 
72  @property
74  """Return the state attributes."""
75  if not self.hasshass.data[DOMAIN][self.controller_idcontroller_id][CONF_INFO]:
76  return {}
77  attributes = {
78  "ihc_id": self.ihc_idihc_id,
79  "ihc_name": self.ihc_nameihc_name,
80  "ihc_note": self.ihc_noteihc_note,
81  "ihc_position": self.ihc_positionihc_position,
82  }
83  if len(self.hasshass.data[DOMAIN]) > 1:
84  # We only want to show the controller id if we have more than one
85  attributes["ihc_controller"] = self.controller_idcontroller_id
86  return attributes
87 
88  def on_ihc_change(self, ihc_id, value):
89  """Handle IHC resource change.
90 
91  Derived classes must overwrite this to do device specific stuff.
92  """
93  raise NotImplementedError
None __init__(self, IHCController ihc_controller, str controller_id, str name, int ihc_id, product=None)
Definition: entity.py:31
def on_ihc_change(self, ihc_id, value)
Definition: entity.py:88