mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +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 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:
|
||||||
|
@ -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:
|
||||||
|
@ -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."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user