Skip to content

Commit

Permalink
Merge pull request #98 from jontofront/refactor
Browse files Browse the repository at this point in the history
Update ruff version and improve ecoNET number component descriptions
  • Loading branch information
jontofront authored Dec 4, 2024
2 parents a3fd6c1 + ef8297f commit 3b85253
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"cSpell.words": [
"aiohttp",
"clientsession",
"codespell",
"colorlog",
"econet",
"ecoster",
"hass",
Expand Down
67 changes: 36 additions & 31 deletions custom_components/econet300/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@

@dataclass
class EconetNumberEntityDescription(NumberEntityDescription):
"""Describes Econet number entity."""
"""Describes ecoNET number entity."""


class EconetNumber(EconetEntity, NumberEntity):
"""Describes Econet binary sensor entity."""
"""Describes ecoNET number sensor entity."""

entity_description: EconetNumberEntityDescription

Expand All @@ -49,49 +49,58 @@ def __init__(
self.entity_description = entity_description
self.api = api
super().__init__(coordinator)
_LOGGER.debug(
"EconetNumberEntity initialized with unique_id: %s, entity_description: %s",
self.unique_id,
self.entity_description,
)

def _sync_state(self, value):
"""Sync the state of the Econet number entity."""
_LOGGER.debug("EconetNumber _sync_state: %s", value)
self._attr_native_value = value["value"]
"""Sync the state of the ecoNET number entity."""
_LOGGER.debug("ecoNETNumber _sync_state: %s", value)
self._attr_native_value = value.get("value")
map_key = NUMBER_MAP.get(self.entity_description.key)

if map_key is not None:
self._attr_native_min_value = value["min"]
self._attr_native_max_value = value["max"]
if map_key:
self._set_value_limits(value)
else:
_LOGGER.error(
"EconetNumber _sync_state map_key %s not found in NUMBER_MAP",
"ecoNETNumber _sync_state: map_key %s not found in NUMBER_MAP",
self.entity_description.key,
)

# Ensure the state is updated in Home Assistant.
self.async_write_ha_state()
# Create an asynchronous task for setting the limits.
self.hass.async_create_task(self.async_set_limits_values())

def _set_value_limits(self, value):
"""Set native min and max values for the entity."""
self._attr_native_min_value = value.get("min")
self._attr_native_max_value = value.get("max")
_LOGGER.debug(
"ecoNETNumber _set_value_limits: min=%s, max=%s",
self._attr_native_min_value,
self._attr_native_max_value,
)

async def async_set_limits_values(self):
"""Async Sync number limits."""
limits = await self.api.get_param_limits(self.entity_description.key)
_LOGGER.debug("Number limits retrieved: %s", limits)
if limits is None:
number_limits = await self.api.get_param_limits(self.entity_description.key)
_LOGGER.debug("Number limits retrieved: %s", number_limits)

if not number_limits:
_LOGGER.warning(
"Cannot add number entity: %s, numeric limits for this entity is None",
self.entity_description.key,
)
else:
self._attr_native_min_value = limits.min
self._attr_native_max_value = limits.max
_LOGGER.debug("Apply number limits: %s", self)
self.async_write_ha_state()
return

# Directly set min and max values based on fetched limits.
self._attr_native_min_value = number_limits.min
self._attr_native_max_value = number_limits.max
_LOGGER.debug("Apply number limits: %s", self)
self.async_write_ha_state()

async def async_set_native_value(self, value: float) -> None:
"""Update the current value."""
_LOGGER.debug("Set value: %s", value)

# Skip processing if the value is unchanged.
if value == self._attr_native_value:
return

Expand Down Expand Up @@ -130,11 +139,11 @@ def apply_limits(desc: EconetNumberEntityDescription, limits: Limits):
_LOGGER.debug("Apply limits: %s", desc)


def create_number_entity_description(key: int) -> EconetNumberEntityDescription:
"""Create Econet300 mixer sensor entity based on supplied key."""
def create_number_entity_description(key: str) -> EconetNumberEntityDescription:
"""Create ecoNET300 number entity description."""
map_key = NUMBER_MAP.get(str(key), str(key))
_LOGGER.debug("Create number: %s", map_key)
entity_description = EconetNumberEntityDescription(
_LOGGER.debug("Creating number entity for key: %s", map_key)
return EconetNumberEntityDescription(
key=key,
translation_key=camel_to_snake(map_key),
icon=ENTITY_ICON.get(map_key),
Expand All @@ -145,8 +154,6 @@ def create_number_entity_description(key: int) -> EconetNumberEntityDescription:
max_value=ENTITY_MAX_VALUE.get(map_key),
native_step=ENTITY_STEP.get(map_key, 1),
)
_LOGGER.debug("Created number entity description: %s", entity_description)
return entity_description


async def async_setup_entry(
Expand All @@ -163,8 +170,6 @@ async def async_setup_entry(

for key in NUMBER_MAP:
number_limits = await api.get_param_limits(key)
_LOGGER.debug("Number limits retrieved: %s", number_limits)

if number_limits is None:
_LOGGER.warning(
"Cannot add number entity: %s, numeric limits for this entity is None",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
colorlog==6.8.2
homeassistant>=2024.9.2
ruff==0.6.5
ruff==0.8.1
codespell==2.3.0
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
colorlog==6.7.0
homeassistant>=2024.3.0
ruff==0.6.8
ruff==0.8.1
codespell==2.3.0

0 comments on commit 3b85253

Please sign in to comment.