Refactor number/select to use common method in IronOS (#134173)

This commit is contained in:
Manu 2025-01-13 14:50:55 +01:00 committed by GitHub
parent 0d116ec6a2
commit fc0a6c2ff3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 36 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from enum import Enum
import logging import logging
from typing import cast from typing import cast
@ -151,7 +152,11 @@ class IronOSSettingsCoordinator(IronOSBaseCoordinator[SettingsDataResponse]):
return self.data or SettingsDataResponse() return self.data or SettingsDataResponse()
async def write(self, characteristic: CharSetting, value: bool) -> None: async def write(
self,
characteristic: CharSetting,
value: bool | Enum | float,
) -> None:
"""Write value to the settings characteristic.""" """Write value to the settings characteristic."""
try: try:

View File

@ -6,12 +6,7 @@ from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from enum import StrEnum from enum import StrEnum
from pynecil import ( from pynecil import CharSetting, LiveDataResponse, SettingsDataResponse
CharSetting,
CommunicationError,
LiveDataResponse,
SettingsDataResponse,
)
from homeassistant.components.number import ( from homeassistant.components.number import (
DEFAULT_MAX_VALUE, DEFAULT_MAX_VALUE,
@ -28,11 +23,10 @@ from homeassistant.const import (
UnitOfTime, UnitOfTime,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import IronOSConfigEntry from . import IronOSConfigEntry
from .const import DOMAIN, MAX_TEMP, MIN_TEMP from .const import MAX_TEMP, MIN_TEMP
from .coordinator import IronOSCoordinators from .coordinator import IronOSCoordinators
from .entity import IronOSBaseEntity from .entity import IronOSBaseEntity
@ -363,16 +357,8 @@ class IronOSNumberEntity(IronOSBaseEntity, NumberEntity):
"""Update the current value.""" """Update the current value."""
if raw_value_fn := self.entity_description.raw_value_fn: if raw_value_fn := self.entity_description.raw_value_fn:
value = raw_value_fn(value) value = raw_value_fn(value)
try:
await self.coordinator.device.write( await self.settings.write(self.entity_description.characteristic, value)
self.entity_description.characteristic, value
)
except CommunicationError as e:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="submit_setting_failed",
) from e
await self.settings.async_request_refresh()
@property @property
def native_value(self) -> float | int | None: def native_value(self) -> float | int | None:

View File

@ -5,14 +5,12 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum, StrEnum from enum import Enum, StrEnum
from typing import Any
from pynecil import ( from pynecil import (
AnimationSpeed, AnimationSpeed,
AutostartMode, AutostartMode,
BatteryType, BatteryType,
CharSetting, CharSetting,
CommunicationError,
LockingMode, LockingMode,
LogoDuration, LogoDuration,
ScreenOrientationMode, ScreenOrientationMode,
@ -25,11 +23,9 @@ from pynecil import (
from homeassistant.components.select import SelectEntity, SelectEntityDescription from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import IronOSConfigEntry from . import IronOSConfigEntry
from .const import DOMAIN
from .coordinator import IronOSCoordinators from .coordinator import IronOSCoordinators
from .entity import IronOSBaseEntity from .entity import IronOSBaseEntity
@ -42,7 +38,7 @@ class IronOSSelectEntityDescription(SelectEntityDescription):
value_fn: Callable[[SettingsDataResponse], str | None] value_fn: Callable[[SettingsDataResponse], str | None]
characteristic: CharSetting characteristic: CharSetting
raw_value_fn: Callable[[str], Any] | None = None raw_value_fn: Callable[[str], Enum]
class PinecilSelect(StrEnum): class PinecilSelect(StrEnum):
@ -193,18 +189,10 @@ class IronOSSelectEntity(IronOSBaseEntity, SelectEntity):
async def async_select_option(self, option: str) -> None: async def async_select_option(self, option: str) -> None:
"""Change the selected option.""" """Change the selected option."""
if raw_value_fn := self.entity_description.raw_value_fn: await self.settings.write(
value = raw_value_fn(option) self.entity_description.characteristic,
try: self.entity_description.raw_value_fn(option),
await self.coordinator.device.write( )
self.entity_description.characteristic, value
)
except CommunicationError as e:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="submit_setting_failed",
) from e
await self.settings.async_request_refresh()
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass.""" """Run when entity about to be added to hass."""