mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Refactor number/select to use common method in IronOS (#134173)
This commit is contained in:
parent
0d116ec6a2
commit
fc0a6c2ff3
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user