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 datetime import timedelta
from enum import Enum
import logging
from typing import cast
@ -151,7 +152,11 @@ class IronOSSettingsCoordinator(IronOSBaseCoordinator[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."""
try:

View File

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

View File

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