Skip to content

Commit

Permalink
Refactor screenlogic numbers to use subclasses (home-assistant#106574)
Browse files Browse the repository at this point in the history
  • Loading branch information
dieselrabbit authored Dec 29, 2023
1 parent a47587e commit ee2689d
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions homeassistant/components/screenlogic/number.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Support for a ScreenLogic number entity."""
import asyncio
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
import logging

Expand Down Expand Up @@ -29,31 +27,21 @@
PARALLEL_UPDATES = 1


@dataclass(frozen=True)
class ScreenLogicNumberRequiredMixin:
"""Describes a required mixin for a ScreenLogic number entity."""

set_value_name: str


@dataclass(frozen=True)
class ScreenLogicNumberDescription(
NumberEntityDescription,
ScreenLogicEntityDescription,
ScreenLogicNumberRequiredMixin,
):
"""Describes a ScreenLogic number entity."""


SUPPORTED_SCG_NUMBERS = [
ScreenLogicNumberDescription(
set_value_name="async_set_scg_config",
data_root=(DEVICE.SCG, GROUP.CONFIGURATION),
key=VALUE.POOL_SETPOINT,
entity_category=EntityCategory.CONFIG,
),
ScreenLogicNumberDescription(
set_value_name="async_set_scg_config",
data_root=(DEVICE.SCG, GROUP.CONFIGURATION),
key=VALUE.SPA_SETPOINT,
entity_category=EntityCategory.CONFIG,
Expand Down Expand Up @@ -82,13 +70,13 @@ async def async_setup_entry(
cleanup_excluded_entity(coordinator, DOMAIN, scg_number_data_path)
continue
if gateway.get_data(*scg_number_data_path):
entities.append(ScreenLogicNumber(coordinator, scg_number_description))
entities.append(ScreenLogicSCGNumber(coordinator, scg_number_description))

async_add_entities(entities)


class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
"""Class to represent a ScreenLogic Number entity."""
"""Base class to represent a ScreenLogic Number entity."""

entity_description: ScreenLogicNumberDescription

Expand All @@ -99,13 +87,7 @@ def __init__(
) -> None:
"""Initialize a ScreenLogic number entity."""
super().__init__(coordinator, entity_description)
if not asyncio.iscoroutinefunction(
func := getattr(self.gateway, entity_description.set_value_name)
):
raise TypeError(
f"set_value_name '{entity_description.set_value_name}' is not a coroutine"
)
self._set_value_func: Callable[..., Awaitable[bool]] = func

self._attr_native_unit_of_measurement = get_ha_unit(
self.entity_data.get(ATTR.UNIT)
)
Expand All @@ -127,14 +109,22 @@ def native_value(self) -> float:
"""Return the current value."""
return self.entity_data[ATTR.VALUE]

async def async_set_native_value(self, value: float) -> None:
"""Update the current value."""
raise NotImplementedError()


class ScreenLogicSCGNumber(ScreenLogicNumber):
"""Class to represent a ScreenLoigic SCG Number entity."""

async def async_set_native_value(self, value: float) -> None:
"""Update the current value."""

# Current API requires int values for the currently supported numbers.
value = int(value)

try:
await self._set_value_func(**{self._data_key: value})
await self.gateway.async_set_scg_config(**{self._data_key: value})
except (ScreenLogicCommunicationError, ScreenLogicError) as sle:
raise HomeAssistantError(
f"Failed to set '{self._data_key}' to {value}: {sle.msg}"
Expand Down

0 comments on commit ee2689d

Please sign in to comment.