mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Call typed methods in Shelly RPC selects (#194466)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
56015e4721
commit
fc1ecbcaf2
@@ -1,9 +1,11 @@
|
||||
"""Select for Shelly."""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, Final, cast, override
|
||||
from typing import TYPE_CHECKING, Any, Final, cast, override
|
||||
|
||||
from aioshelly.const import RPC_GENERATIONS
|
||||
from aioshelly.rpc_device import RpcDevice
|
||||
|
||||
from homeassistant.components.select import (
|
||||
DOMAIN as SELECT_DOMAIN,
|
||||
@@ -37,7 +39,7 @@ PARALLEL_UPDATES = 0
|
||||
class RpcSelectDescription(RpcEntityDescription, SelectEntityDescription):
|
||||
"""Class to describe a RPC select entity."""
|
||||
|
||||
method: str
|
||||
method: Callable[[RpcDevice, int, str], Awaitable[Any]]
|
||||
|
||||
|
||||
class RpcSelect(ShellyRpcAttributeEntity, SelectEntity):
|
||||
@@ -72,15 +74,10 @@ class RpcSelect(ShellyRpcAttributeEntity, SelectEntity):
|
||||
@override
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the value."""
|
||||
method = getattr(self.coordinator.device, self.entity_description.method)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert method is not None
|
||||
|
||||
if self.reversed_option_map:
|
||||
await method(self._id, self.reversed_option_map[option])
|
||||
else:
|
||||
await method(self._id, option)
|
||||
option = self.reversed_option_map[option]
|
||||
|
||||
await self.entity_description.method(self.coordinator.device, self._id, option)
|
||||
|
||||
|
||||
class RpcCuryModeSelect(RpcSelect):
|
||||
@@ -141,17 +138,6 @@ class RpcLedStripPaletteSelect(RpcSelect):
|
||||
|
||||
return self.attribute_value
|
||||
|
||||
@rpc_call
|
||||
@override
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the value."""
|
||||
method = getattr(self.coordinator.device, self.entity_description.method)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert method is not None
|
||||
|
||||
await method(self._id, {self.entity_description.sub_key: option})
|
||||
|
||||
|
||||
class RpcUiLedModeSelect(RpcSelect):
|
||||
"""Represent a RPC select entity for the plug/power strip LED mode."""
|
||||
@@ -162,17 +148,6 @@ class RpcUiLedModeSelect(RpcSelect):
|
||||
"""Return the selected entity option to represent the entity state."""
|
||||
return cast(str, self.coordinator.device.config[self.key]["leds"]["mode"])
|
||||
|
||||
@rpc_call
|
||||
@override
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the value."""
|
||||
method = getattr(self.coordinator.device, self.entity_description.method)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
assert method is not None
|
||||
|
||||
await method({"leds": {"mode": option}})
|
||||
|
||||
|
||||
RPC_SELECT_ENTITIES: Final = (
|
||||
RpcSelectDescription(
|
||||
@@ -189,7 +164,7 @@ RPC_SELECT_ENTITIES: Final = (
|
||||
"reception",
|
||||
"workplace",
|
||||
],
|
||||
method="cury_set_mode",
|
||||
method=lambda device, id_, option: device.cury_set_mode(id_, option),
|
||||
entity_class=RpcCuryModeSelect,
|
||||
),
|
||||
RpcSelectDescription(
|
||||
@@ -199,7 +174,7 @@ RPC_SELECT_ENTITIES: Final = (
|
||||
removal_condition=lambda config, _status, key: (
|
||||
not is_view_for_platform(config, key, SELECT_DOMAIN)
|
||||
),
|
||||
method="enum_set",
|
||||
method=lambda device, id_, option: device.enum_set(id_, option),
|
||||
role=ROLE_GENERIC,
|
||||
),
|
||||
RpcSelectDescription(
|
||||
@@ -207,7 +182,9 @@ RPC_SELECT_ENTITIES: Final = (
|
||||
components=("ledstrip",),
|
||||
sub_key="palette",
|
||||
translation_key="effect_palette",
|
||||
method="ledstrip_set",
|
||||
method=lambda device, id_, option: device.ledstrip_set(
|
||||
id_, {"palette": option}
|
||||
),
|
||||
supported=lambda _: True,
|
||||
entity_class=RpcLedStripPaletteSelect,
|
||||
),
|
||||
@@ -217,7 +194,9 @@ RPC_SELECT_ENTITIES: Final = (
|
||||
translation_key="led_mode",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
options=["power", "switch", "off"],
|
||||
method="plugs_ui_setconfig",
|
||||
method=lambda device, _id, option: device.plugs_ui_setconfig(
|
||||
{"leds": {"mode": option}}
|
||||
),
|
||||
supported=lambda _: True,
|
||||
entity_class=RpcUiLedModeSelect,
|
||||
),
|
||||
@@ -227,7 +206,9 @@ RPC_SELECT_ENTITIES: Final = (
|
||||
translation_key="led_mode",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
options=["power", "switch", "off"],
|
||||
method="powerstrip_ui_setconfig",
|
||||
method=lambda device, _id, option: device.powerstrip_ui_setconfig(
|
||||
{"leds": {"mode": option}}
|
||||
),
|
||||
supported=lambda _: True,
|
||||
entity_class=RpcUiLedModeSelect,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user