2018-04-06 16:06:47 +00:00
|
|
|
"""Implementation of a base class for all IHC devices."""
|
2018-01-20 15:29:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
|
|
|
|
class IHCDevice(Entity):
|
2018-04-06 16:06:47 +00:00
|
|
|
"""Base class for all IHC devices.
|
2018-01-20 15:29:50 +00:00
|
|
|
|
|
|
|
All IHC devices have an associated IHC resource. IHCDevice handled the
|
|
|
|
registration of the IHC controller callback when the IHC resource changes.
|
|
|
|
Derived classes must implement the on_ihc_change method
|
|
|
|
"""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self, ihc_controller, name, ihc_id: int, info: bool, product=None
|
|
|
|
) -> None:
|
2018-01-20 15:29:50 +00:00
|
|
|
"""Initialize IHC attributes."""
|
|
|
|
self.ihc_controller = ihc_controller
|
|
|
|
self._name = name
|
|
|
|
self.ihc_id = ihc_id
|
|
|
|
self.info = info
|
|
|
|
if product:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.ihc_name = product["name"]
|
|
|
|
self.ihc_note = product["note"]
|
|
|
|
self.ihc_position = product["position"]
|
2018-01-20 15:29:50 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.ihc_name = ""
|
|
|
|
self.ihc_note = ""
|
|
|
|
self.ihc_position = ""
|
2018-01-20 15:29:50 +00:00
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-04-06 16:06:47 +00:00
|
|
|
"""Add callback for IHC changes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.ihc_controller.add_notify_event(self.ihc_id, self.on_ihc_change, True)
|
2018-01-20 15:29:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
2018-04-06 16:06:47 +00:00
|
|
|
"""No polling needed for IHC devices."""
|
2018-01-20 15:29:50 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the device name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-01-20 15:29:50 +00:00
|
|
|
"""Return the state attributes."""
|
|
|
|
if not self.info:
|
|
|
|
return {}
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"ihc_id": self.ihc_id,
|
|
|
|
"ihc_name": self.ihc_name,
|
|
|
|
"ihc_note": self.ihc_note,
|
|
|
|
"ihc_position": self.ihc_position,
|
2018-01-20 15:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def on_ihc_change(self, ihc_id, value):
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Handle IHC resource change.
|
2018-01-20 15:29:50 +00:00
|
|
|
|
|
|
|
Derived classes must overwrite this to do device specific stuff.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|