Skip to content

Commit

Permalink
refactor: make EconetBinarySensorEntityDescription immutable and enha…
Browse files Browse the repository at this point in the history
…nce value retrieval logic in EconetEntity
  • Loading branch information
jontofront committed Dec 20, 2024
1 parent 1cd1499 commit 7ef6d3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
7 changes: 2 additions & 5 deletions custom_components/econet300/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
Expand All @@ -28,7 +27,7 @@
_LOGGER = logging.getLogger(__name__)


@dataclass
@dataclass(frozen=True, kw_only=True)
class EconetBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Econet binary sensor entity."""

Expand Down Expand Up @@ -86,9 +85,7 @@ def create_binary_entity_description(key: str) -> EconetBinarySensorEntityDescri
entity_description = EconetBinarySensorEntityDescription(
key=key,
translation_key=camel_to_snake(key),
device_class=ENTITY_BINARY_DEVICE_CLASS_MAP.get(
key, BinarySensorDeviceClass.RUNNING
),
device_class=ENTITY_BINARY_DEVICE_CLASS_MAP.get(key, None),
icon=ENTITY_ICON.get(key, None),
icon_off=ENTITY_ICON_OFF.get(key, None),
)
Expand Down
22 changes: 15 additions & 7 deletions custom_components/econet300/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,25 @@ def _handle_coordinator_update(self) -> None:
"Update EconetEntity, entity name: %s", self.entity_description.name
)

value = (
self.coordinator.data["sysParams"].get(self.entity_description.key)
or self.coordinator.data["regParams"].get(self.entity_description.key)
or self.coordinator.data.get("paramsEdits", {}).get(
self.entity_description.key
)
)
value = None
if self.entity_description.key in self.coordinator.data.get("sysParams", {}):
value = self.coordinator.data["sysParams"][self.entity_description.key]
elif self.entity_description.key in self.coordinator.data.get("regParams", {}):
value = self.coordinator.data["regParams"][self.entity_description.key]
elif self.entity_description.key in self.coordinator.data.get(
"paramsEdits", {}
):
value = self.coordinator.data["paramsEdits"][self.entity_description.key]

if value is None:
_LOGGER.debug("Value for key %s is None", self.entity_description.key)
return

_LOGGER.debug(
"Updating state for key: %s with value: %s",
self.entity_description.key,
value,
)
self._sync_state(value)

async def async_added_to_hass(self):
Expand Down

0 comments on commit 7ef6d3e

Please sign in to comment.