Skip to content

Commit

Permalink
Update ihcdevice.py
Browse files Browse the repository at this point in the history
  • Loading branch information
duddi authored Mar 10, 2023
1 parent f71f9d1 commit 552fbce
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions custom_components/ihc/ihcdevice.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
"""Implementation of a base class for all IHC devices."""
import logging

from ihcsdk.ihccontroller import IHCController

from homeassistant.helpers.entity import Entity

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


class IHCDevice(Entity):
"""Base class for all IHC devices.
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
"""
"""Base class for all IHC devices."""

_attr_should_poll = False

Expand All @@ -35,23 +27,19 @@ def __init__(
self.controller_id = controller_id
self.device_id = None
self.suggested_area = None

if product:
self.ihc_name = product["name"]
self.ihc_note = product["note"]
self.ihc_position = product["position"]
self.suggested_area = product["group"] if "group" in product else None
if "id" in product:
product_id = product["id"]
self.device_id = f"{controller_id}_{product_id }"
# this will name the device the same way as the IHC visual application: Product name + position
self.device_name = product["name"]
if self.ihc_position:
self.device_name += f" ({self.ihc_position})"
self.device_model = product["model"]
self.ihc_name, self.ihc_note, self.ihc_position = (
product["name"],
product["note"],
product["position"],
)
self.suggested_area = product.get("group")
self.device_id = f"{controller_id}_{product.get('id')}"
self.device_name = f"{product['name']} ({product.get('position', '')})"
self.device_model = product["model"]
else:
self.ihc_name = ""
self.ihc_note = ""
self.ihc_position = ""
self.ihc_name = self.ihc_note = self.ihc_position = ""

async def async_added_to_hass(self):
"""Add callback for IHC changes."""
Expand All @@ -78,15 +66,11 @@ def extra_state_attributes(self):
"ihc_position": self.ihc_position,
}
if len(self.hass.data[DOMAIN]) > 1:
# We only want to show the controller id if we have more than one
attributes["ihc_controller"] = self.controller_id
return attributes

def on_ihc_change(self, ihc_id, value):
"""Handle IHC resource change.
Derived classes must overwrite this to do device specific stuff.
"""
"""Handle IHC resource change."""
raise NotImplementedError

@property
Expand All @@ -95,10 +79,7 @@ def device_info(self):
if not self.device_id:
return None
return {
"identifiers": {
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.device_id)
},
"identifiers": {(DOMAIN, self.device_id)},
"name": self.device_name,
"manufacturer": "Schneider Electric",
"suggested_area": self.suggested_area,
Expand Down

0 comments on commit 552fbce

Please sign in to comment.