Refactor screenlogic numbers to use subclasses (#106574)

This commit is contained in:
Kevin Worrel 2023-12-28 17:45:34 -08:00 committed by GitHub
parent a47587e3cd
commit ee2689de3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,4 @@
"""Support for a ScreenLogic number entity.""" """Support for a ScreenLogic number entity."""
import asyncio
from collections.abc import Awaitable, Callable
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
@ -29,31 +27,21 @@ _LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@dataclass(frozen=True)
class ScreenLogicNumberRequiredMixin:
"""Describes a required mixin for a ScreenLogic number entity."""
set_value_name: str
@dataclass(frozen=True) @dataclass(frozen=True)
class ScreenLogicNumberDescription( class ScreenLogicNumberDescription(
NumberEntityDescription, NumberEntityDescription,
ScreenLogicEntityDescription, ScreenLogicEntityDescription,
ScreenLogicNumberRequiredMixin,
): ):
"""Describes a ScreenLogic number entity.""" """Describes a ScreenLogic number entity."""
SUPPORTED_SCG_NUMBERS = [ SUPPORTED_SCG_NUMBERS = [
ScreenLogicNumberDescription( ScreenLogicNumberDescription(
set_value_name="async_set_scg_config",
data_root=(DEVICE.SCG, GROUP.CONFIGURATION), data_root=(DEVICE.SCG, GROUP.CONFIGURATION),
key=VALUE.POOL_SETPOINT, key=VALUE.POOL_SETPOINT,
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
), ),
ScreenLogicNumberDescription( ScreenLogicNumberDescription(
set_value_name="async_set_scg_config",
data_root=(DEVICE.SCG, GROUP.CONFIGURATION), data_root=(DEVICE.SCG, GROUP.CONFIGURATION),
key=VALUE.SPA_SETPOINT, key=VALUE.SPA_SETPOINT,
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
@ -82,13 +70,13 @@ async def async_setup_entry(
cleanup_excluded_entity(coordinator, DOMAIN, scg_number_data_path) cleanup_excluded_entity(coordinator, DOMAIN, scg_number_data_path)
continue continue
if gateway.get_data(*scg_number_data_path): 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) async_add_entities(entities)
class ScreenLogicNumber(ScreenLogicEntity, NumberEntity): class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
"""Class to represent a ScreenLogic Number entity.""" """Base class to represent a ScreenLogic Number entity."""
entity_description: ScreenLogicNumberDescription entity_description: ScreenLogicNumberDescription
@ -99,13 +87,7 @@ class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
) -> None: ) -> None:
"""Initialize a ScreenLogic number entity.""" """Initialize a ScreenLogic number entity."""
super().__init__(coordinator, entity_description) 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._attr_native_unit_of_measurement = get_ha_unit(
self.entity_data.get(ATTR.UNIT) self.entity_data.get(ATTR.UNIT)
) )
@ -127,6 +109,14 @@ class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
"""Return the current value.""" """Return the current value."""
return self.entity_data[ATTR.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: async def async_set_native_value(self, value: float) -> None:
"""Update the current value.""" """Update the current value."""
@ -134,7 +124,7 @@ class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
value = int(value) value = int(value)
try: 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: except (ScreenLogicCommunicationError, ScreenLogicError) as sle:
raise HomeAssistantError( raise HomeAssistantError(
f"Failed to set '{self._data_key}' to {value}: {sle.msg}" f"Failed to set '{self._data_key}' to {value}: {sle.msg}"