Refactor screenlogic numbers to use subclasses (#106574)
parent
a47587e3cd
commit
ee2689de3c
|
@ -1,6 +1,4 @@
|
|||
"""Support for a ScreenLogic number entity."""
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
|
@ -29,31 +27,21 @@ _LOGGER = logging.getLogger(__name__)
|
|||
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,
|
||||
|
@ -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
|
||||
|
||||
|
@ -99,13 +87,7 @@ class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
|
|||
) -> 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)
|
||||
)
|
||||
|
@ -127,6 +109,14 @@ class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
|
|||
"""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."""
|
||||
|
||||
|
@ -134,7 +124,7 @@ class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
|
|||
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}"
|
||||
|
|
Loading…
Reference in New Issue