From 887a7afbe73ee23d10ce14c044da0e9d364892c2 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sat, 21 Nov 2026 12:48:29 +0200 Subject: [PATCH] Store Shelly RPC entity descriptions as tuples (#193663) Co-authored-by: Claude Fable 5.2 --- .../components/shelly/binary_sensor.py | 113 ++-- homeassistant/components/shelly/button.py | 32 +- homeassistant/components/shelly/camera.py | 14 +- homeassistant/components/shelly/climate.py | 9 +- homeassistant/components/shelly/cover.py | 7 +- homeassistant/components/shelly/entity.py | 38 +- homeassistant/components/shelly/fan.py | 6 +- homeassistant/components/shelly/infrared.py | 14 +- homeassistant/components/shelly/light.py | 25 +- .../components/shelly/media_player.py | 7 +- homeassistant/components/shelly/number.py | 59 ++- homeassistant/components/shelly/select.py | 29 +- homeassistant/components/shelly/sensor.py | 501 +++++++++++------- homeassistant/components/shelly/switch.py | 117 ++-- homeassistant/components/shelly/text.py | 9 +- homeassistant/components/shelly/update.py | 24 +- homeassistant/components/shelly/valve.py | 44 +- tests/components/shelly/test_entity.py | 89 ++++ 18 files changed, 710 insertions(+), 427 deletions(-) create mode 100644 tests/components/shelly/test_entity.py diff --git a/homeassistant/components/shelly/binary_sensor.py b/homeassistant/components/shelly/binary_sensor.py index dba7c15c2bb7..562c0a50c40e 100644 --- a/homeassistant/components/shelly/binary_sensor.py +++ b/homeassistant/components/shelly/binary_sensor.py @@ -230,47 +230,52 @@ REST_SENSORS: Final = { ), } -RPC_SENSORS: Final = { - "input": RpcBinarySensorDescription( +RPC_SENSORS: Final = ( + RpcBinarySensorDescription( key="input", + components=("input",), sub_key="state", translation_key="input", device_class=BinarySensorDeviceClass.POWER, removal_condition=is_rpc_momentary_input, ), - "cloud": RpcBinarySensorDescription( + RpcBinarySensorDescription( key="cloud", + components=("cloud",), sub_key="connected", translation_key="cloud", device_class=BinarySensorDeviceClass.CONNECTIVITY, entity_registry_enabled_default=False, entity_category=EntityCategory.DIAGNOSTIC, ), - "external_power": RpcBinarySensorDescription( - key="devicepower", + RpcBinarySensorDescription( + key="external_power", + components=("devicepower",), sub_key="external", translation_key="external_power", value=lambda status, _: status["present"], device_class=BinarySensorDeviceClass.POWER, entity_category=EntityCategory.DIAGNOSTIC, ), - "cb_output": RpcBinarySensorDescription( - key="cb", + RpcBinarySensorDescription( + key="cb_output", + components=("cb",), sub_key="output", translation_key="output", device_class=BinarySensorDeviceClass.POWER, entity_category=EntityCategory.DIAGNOSTIC, ), - "cb_safety": RpcBinarySensorDescription( - key="cb", + RpcBinarySensorDescription( + key="cb_safety", + components=("cb",), sub_key="safety", translation_key="safety_switch", device_class=BinarySensorDeviceClass.LOCK, value=lambda status, _: not status, entity_category=EntityCategory.DIAGNOSTIC, ), - "overtemp": RpcBinarySensorDescription( - key="switch", + RpcBinarySensorDescription( + key="overtemp", components=("switch", "cover"), sub_key="errors", translation_key="overheating", @@ -279,8 +284,8 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, supported=lambda status: status.get("apower") is not None, ), - "overpower": RpcBinarySensorDescription( - key="switch", + RpcBinarySensorDescription( + key="overpower", components=("switch", "cover"), sub_key="errors", translation_key="overpowering", @@ -289,8 +294,8 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, supported=lambda status: status.get("apower") is not None, ), - "overvoltage": RpcBinarySensorDescription( - key="switch", + RpcBinarySensorDescription( + key="overvoltage", components=("switch", "cover"), sub_key="errors", translation_key="overvoltage", @@ -299,8 +304,8 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, supported=lambda status: status.get("apower") is not None, ), - "overcurrent": RpcBinarySensorDescription( - key="switch", + RpcBinarySensorDescription( + key="overcurrent", components=("switch", "cover"), sub_key="errors", translation_key="overcurrent", @@ -309,37 +314,42 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, supported=lambda status: status.get("apower") is not None, ), - "smoke": RpcBinarySensorDescription( + RpcBinarySensorDescription( key="smoke", + components=("smoke",), sub_key="alarm", device_class=BinarySensorDeviceClass.SMOKE, ), - "restart": RpcBinarySensorDescription( - key="sys", + RpcBinarySensorDescription( + key="restart", + components=("sys",), sub_key="restart_required", translation_key="restart_required", device_class=BinarySensorDeviceClass.PROBLEM, entity_registry_enabled_default=False, entity_category=EntityCategory.DIAGNOSTIC, ), - "boolean_generic": RpcBinarySensorDescription( - key="boolean", + RpcBinarySensorDescription( + key="boolean_generic", + components=("boolean",), sub_key="value", removal_condition=lambda config, _, key: ( not is_view_for_platform(config, key, BINARY_SENSOR_DOMAIN) ), role=ROLE_GENERIC, ), - "boolean_has_power": RpcBinarySensorDescription( - key="boolean", + RpcBinarySensorDescription( + key="boolean_has_power", + components=("boolean",), sub_key="value", device_class=BinarySensorDeviceClass.POWER, entity_category=EntityCategory.DIAGNOSTIC, role="has_power", models={MODEL_FRANKEVER_WATER_VALVE}, ), - "calibration": RpcBinarySensorDescription( - key="blutrv", + RpcBinarySensorDescription( + key="calibration", + components=("blutrv",), sub_key="errors", translation_key="calibration", device_class=BinarySensorDeviceClass.PROBLEM, @@ -347,20 +357,23 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, entity_class=RpcBluTrvBinarySensor, ), - "flood": RpcBinarySensorDescription( + RpcBinarySensorDescription( key="flood", + components=("flood",), sub_key="alarm", translation_key="flood", device_class=BinarySensorDeviceClass.MOISTURE, ), - "mute": RpcBinarySensorDescription( - key="flood", + RpcBinarySensorDescription( + key="mute", + components=("flood",), sub_key="mute", translation_key="mute", entity_category=EntityCategory.DIAGNOSTIC, ), - "flood_cable_unplugged": RpcBinarySensorDescription( - key="flood", + RpcBinarySensorDescription( + key="flood_cable_unplugged", + components=("flood",), sub_key="errors", value=lambda status, _: ( False if status is None else "cable_unplugged" in status @@ -371,26 +384,30 @@ RPC_SENSORS: Final = { supported=lambda status: status.get("alarm") is not None, models={MODEL_FLOOD_G4}, ), - "presence_num_objects": RpcBinarySensorDescription( - key="presence", + RpcBinarySensorDescription( + key="presence_num_objects", + components=("presence",), sub_key="num_objects", value=lambda status, _: bool(status), device_class=BinarySensorDeviceClass.OCCUPANCY, entity_class=RpcPresenceBinarySensor, ), - "presencezone_state": RpcBinarySensorDescription( - key="presencezone", + RpcBinarySensorDescription( + key="presencezone_state", + components=("presencezone",), sub_key="value", device_class=BinarySensorDeviceClass.OCCUPANCY, entity_class=RpcPresenceBinarySensor, ), - "occupancy": RpcBinarySensorDescription( + RpcBinarySensorDescription( key="occupancy", + components=("occupancy",), sub_key="value", device_class=BinarySensorDeviceClass.OCCUPANCY, ), - "cury_tilt": RpcBinarySensorDescription( - key="cury", + RpcBinarySensorDescription( + key="cury_tilt", + components=("cury",), sub_key="errors", translation_key="tilt", device_class=BinarySensorDeviceClass.PROBLEM, @@ -400,8 +417,9 @@ RPC_SENSORS: Final = { ), supported=lambda status: status.get("slots") is not None, ), - "cury_rotation": RpcBinarySensorDescription( - key="cury", + RpcBinarySensorDescription( + key="cury_rotation", + components=("cury",), sub_key="errors", translation_key="rotation", device_class=BinarySensorDeviceClass.PROBLEM, @@ -411,24 +429,27 @@ RPC_SENSORS: Final = { ), supported=lambda status: status.get("slots") is not None, ), - "camera_motion": RpcBinarySensorDescription( - key="camera", + RpcBinarySensorDescription( + key="camera_motion", + components=("camera",), sub_key="motion", device_class=BinarySensorDeviceClass.MOTION, ), - "camerazone_motion": RpcBinarySensorDescription( - key="camerazone", + RpcBinarySensorDescription( + key="camerazone_motion", + components=("camerazone",), sub_key="motion", device_class=BinarySensorDeviceClass.MOTION, entity_class=RpcCameraZoneBinarySensor, ), - "motion": RpcBinarySensorDescription( + RpcBinarySensorDescription( key="motion", + components=("motion",), sub_key="motion", device_class=BinarySensorDeviceClass.MOTION, removal_condition=lambda config, _, key: not config[key].get("enable", True), ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/button.py b/homeassistant/components/shelly/button.py index 98f4249b866f..b6944eb73c3f 100644 --- a/homeassistant/components/shelly/button.py +++ b/homeassistant/components/shelly/button.py @@ -376,38 +376,44 @@ class RpcSleepingSmokeMuteButton(ShellySleepingRpcAttributeEntity, ButtonEntity) return False -RPC_BUTTONS = { - "button_generic": RpcButtonDescription( - key="button", +RPC_BUTTONS = ( + RpcButtonDescription( + key="button_generic", + components=("button",), role=ROLE_GENERIC, ), - "button_open": RpcButtonDescription( - key="button", + RpcButtonDescription( + key="button_open", + components=("button",), translation_key="open", entity_registry_enabled_default=False, role="open", models={MODEL_FRANKEVER_WATER_VALVE}, ), - "button_close": RpcButtonDescription( - key="button", + RpcButtonDescription( + key="button_close", + components=("button",), translation_key="close", entity_registry_enabled_default=False, role="close", models={MODEL_FRANKEVER_WATER_VALVE}, ), - "calibrate": RpcButtonDescription( - key="blutrv", + RpcButtonDescription( + key="calibrate", + components=("blutrv",), translation_key="calibrate", entity_category=EntityCategory.CONFIG, entity_class=ShellyBluTrvButton, ), - "smoke_mute": RpcButtonDescription( - key="smoke", + RpcButtonDescription( + key="smoke_mute", + components=("smoke",), sub_key="mute", translation_key="mute_alarm", ), - "ircode": RpcButtonDescription( + RpcButtonDescription( key="ircode", + components=("ircode",), entity_class=RpcIrCodeButton, ), -} +) diff --git a/homeassistant/components/shelly/camera.py b/homeassistant/components/shelly/camera.py index 051561b57c19..beee7a011714 100644 --- a/homeassistant/components/shelly/camera.py +++ b/homeassistant/components/shelly/camera.py @@ -35,23 +35,25 @@ class RpcCameraEntityDescription(RpcEntityDescription, CameraEntityDescription): stream: int -RPC_CAMERA_ENTITIES: Final = { - "stream_0": RpcCameraEntityDescription( - key="camera", +RPC_CAMERA_ENTITIES: Final = ( + RpcCameraEntityDescription( + key="stream_0", + components=("camera",), stream=0, translation_key="stream", translation_placeholders={"stream_id": "0"}, removal_condition=lambda config, _, key: not config[key]["rtsp"]["enable"], ), - "stream_1": RpcCameraEntityDescription( - key="camera", + RpcCameraEntityDescription( + key="stream_1", + components=("camera",), stream=1, translation_key="stream", translation_placeholders={"stream_id": "1"}, entity_registry_enabled_default=False, removal_condition=lambda config, _, key: not config[key]["rtsp"]["enable"], ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/climate.py b/homeassistant/components/shelly/climate.py index 6abb6aa48921..0db8c28471f6 100644 --- a/homeassistant/components/shelly/climate.py +++ b/homeassistant/components/shelly/climate.py @@ -270,14 +270,15 @@ class RpcLinkedgoThermostatClimate(ShellyRpcAttributeEntity, ClimateEntity): ) -RPC_LINKEDGO_THERMOSTAT: dict[str, RpcClimateDescription] = { - "linkedgo_thermostat_climate": RpcClimateDescription( - key="number", +RPC_LINKEDGO_THERMOSTAT: tuple[RpcClimateDescription, ...] = ( + RpcClimateDescription( + key="linkedgo_thermostat_climate", + components=("number",), sub_key="value", role="target_temperature", models={MODEL_LINKEDGO_ST802_THERMOSTAT, MODEL_LINKEDGO_ST1820_THERMOSTAT}, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/cover.py b/homeassistant/components/shelly/cover.py index 6eb2a5f30d1e..49d2ce12f3fb 100644 --- a/homeassistant/components/shelly/cover.py +++ b/homeassistant/components/shelly/cover.py @@ -50,11 +50,12 @@ BLOCK_COVERS = { ) } -RPC_COVERS = { - "cover": RpcCoverDescription( +RPC_COVERS = ( + RpcCoverDescription( key="cover", + components=("cover",), ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 565e8ea97e46..c4768d92b3de 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -1,6 +1,6 @@ """Shelly entity helper.""" -from collections.abc import Awaitable, Callable, Coroutine, Mapping +from collections.abc import Awaitable, Callable, Coroutine, Iterable, Mapping from dataclasses import dataclass from functools import partial, wraps from typing import Any, Concatenate, cast, override @@ -146,7 +146,7 @@ def async_setup_entry_rpc( hass: HomeAssistant, config_entry: ShellyConfigEntry, async_add_entities: AddEntitiesCallback, - sensors: Mapping[str, RpcEntityDescription], + sensors: Iterable[RpcEntityDescription], sensor_class: Callable, ) -> None: """Set up RPC entities.""" @@ -181,7 +181,7 @@ def async_setup_rpc_attribute_entities( hass: HomeAssistant, config_entry: ShellyConfigEntry, async_add_entities: AddEntitiesCallback, - sensors: Mapping[str, RpcEntityDescription], + sensors: Iterable[RpcEntityDescription], sensor_class: Callable, *, skip_registered: bool = False, @@ -196,11 +196,11 @@ def async_setup_rpc_attribute_entities( assert polling_coordinator entities = [] - for sensor_id, description in sensors.items(): + for description in sensors: legacy_ids = description.legacy_ids or {} key_instances = [ - (key, legacy_ids.get(component, sensor_id)) - for component in description.components or (description.key,) + (key, legacy_ids.get(component, description.key)) + for component in description.components for key in get_rpc_key_instances(coordinator.device.status, component) ] @@ -268,7 +268,7 @@ def async_restore_rpc_attribute_entities( config_entry: ShellyConfigEntry, async_add_entities: AddEntitiesCallback, coordinator: ShellyRpcCoordinator, - sensors: Mapping[str, RpcEntityDescription], + sensors: Iterable[RpcEntityDescription], sensor_class: Callable, ) -> None: """Restore RPC attributes entities.""" @@ -278,11 +278,7 @@ def async_restore_rpc_attribute_entities( entries = er.async_entries_for_config_entry(ent_reg, config_entry.entry_id) domain = sensor_class.__module__.split(".")[-1] - descriptions = { - legacy_id: description - for description in sensors.values() - for legacy_id in (description.legacy_ids or {}).values() - } | dict(sensors) + descriptions = get_rpc_descriptions_by_attribute(sensors) for entry in entries: if entry.domain != domain: @@ -338,10 +334,9 @@ class BlockEntityDescription(EntityDescription): class RpcEntityDescription(EntityDescription): """Class to describe a RPC entity.""" + components: tuple[str, ...] sub_key: str | None = None - # Components the description applies to, defaults to the key - components: tuple[str, ...] | None = None - # Description ID per component, keeps unique IDs from before a merge + # Unique ID attribute per component, keeps unique IDs from before a merge legacy_ids: Mapping[str, str] | None = None value: Callable[[Any, Any], Any] | None = None @@ -355,6 +350,19 @@ class RpcEntityDescription(EntityDescription): models: set[str] | None = None +def get_rpc_descriptions_by_attribute( + descriptions: Iterable[RpcEntityDescription], +) -> dict[str, RpcEntityDescription]: + """Return the RPC descriptions by the attribute of their unique IDs.""" + by_attribute: dict[str, RpcEntityDescription] = {} + for description in descriptions: + for attribute in (description.key, *(description.legacy_ids or {}).values()): + if attribute in by_attribute: + raise ValueError(f"Duplicate RPC description attribute: {attribute}") + by_attribute[attribute] = description + return by_attribute + + @dataclass(frozen=True) class RestEntityDescription(EntityDescription): """Class to describe a REST entity.""" diff --git a/homeassistant/components/shelly/fan.py b/homeassistant/components/shelly/fan.py index e0ec7aebcc66..03e1fdf72e94 100644 --- a/homeassistant/components/shelly/fan.py +++ b/homeassistant/components/shelly/fan.py @@ -33,9 +33,9 @@ class RpcFanDescription(RpcEntityDescription, FanEntityDescription): """Description for a Shelly RPC fan entity.""" -RPC_FANS: Final = { - "fan": RpcFanDescription(key="fan", translation_key="fan"), -} +RPC_FANS: Final = ( + RpcFanDescription(key="fan", components=("fan",), translation_key="fan"), +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/infrared.py b/homeassistant/components/shelly/infrared.py index ae96495ca8bc..1299f0d9af41 100644 --- a/homeassistant/components/shelly/infrared.py +++ b/homeassistant/components/shelly/infrared.py @@ -80,19 +80,21 @@ class ShellyInfraredReceiver(ShellyRpcAttributeEntity, InfraredReceiverEntity): self._handle_received_signal(InfraredReceivedSignal(timings=timings)) -RPC_INFRARED_ENTITIES: Final = { - "infrared_emitter": RpcInfraredEmitterEntityDescription( - key="ir", +RPC_INFRARED_ENTITIES: Final = ( + RpcInfraredEmitterEntityDescription( + key="infrared_emitter", + components=("ir",), translation_key="infrared_emitter", entity_class=ShellyInfraredEmitter, ), - "infrared_receiver": RpcInfraredReceiverEntityDescription( - key="ir", + RpcInfraredReceiverEntityDescription( + key="infrared_receiver", + components=("ir",), translation_key="infrared_receiver", entity_class=ShellyInfraredReceiver, removal_condition=lambda config, _, key: not config[key].get("raw_receive"), ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index 6eb7927431c0..cebc90c16a7d 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -594,47 +594,54 @@ class RpcShellyRgbwLight(RpcShellyLightBase): _attr_supported_features = LightEntityFeature.TRANSITION -LIGHTS: Final = { - "switch": RpcEntityDescription( +LIGHTS: Final = ( + RpcEntityDescription( key="switch", + components=("switch",), sub_key="output", removal_condition=lambda config, _status, key: ( not is_rpc_channel_type_light(config, get_rpc_key_id(key)) ), entity_class=RpcShellySwitchAsLight, ), - "light": RpcEntityDescription( + RpcEntityDescription( key="light", + components=("light",), sub_key="output", entity_class=RpcShellyLight, ), - "cct": RpcEntityDescription( + RpcEntityDescription( key="cct", + components=("cct",), sub_key="output", entity_class=RpcShellyCctLight, ), - "rgb": RpcEntityDescription( + RpcEntityDescription( key="rgb", + components=("rgb",), sub_key="output", entity_class=RpcShellyRgbLight, ), - "rgbcct": RpcEntityDescription( + RpcEntityDescription( key="rgbcct", + components=("rgbcct",), sub_key="output", entity_class=RpcShellyRgbCctLight, ), - "rgbw": RpcEntityDescription( + RpcEntityDescription( key="rgbw", + components=("rgbw",), sub_key="output", entity_class=RpcShellyRgbwLight, ), - "ledstrip": RpcEntityDescription( + RpcEntityDescription( key="ledstrip", + components=("ledstrip",), sub_key="on", translation_key="ledstrip", entity_class=RpcShellyLedStrip, ), -} +) @callback diff --git a/homeassistant/components/shelly/media_player.py b/homeassistant/components/shelly/media_player.py index 7d2c82fdd66c..d3d850daa25d 100644 --- a/homeassistant/components/shelly/media_player.py +++ b/homeassistant/components/shelly/media_player.py @@ -56,12 +56,13 @@ class RpcMediaPlayerDescription(RpcEntityDescription, MediaPlayerEntityDescripti """Class to describe a Shelly RPC media player entity.""" -RPC_MEDIA_PLAYER_ENTITIES: Final = { - "media": RpcMediaPlayerDescription( +RPC_MEDIA_PLAYER_ENTITIES: Final = ( + RpcMediaPlayerDescription( key="media", + components=("media",), device_class=MediaPlayerDeviceClass.SPEAKER, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/number.py b/homeassistant/components/shelly/number.py index 77601cda553a..dabd49d17aaf 100644 --- a/homeassistant/components/shelly/number.py +++ b/homeassistant/components/shelly/number.py @@ -219,9 +219,10 @@ BLOCK_NUMBERS: dict[tuple[str, str], BlockNumberDescription] = { } -RPC_NUMBERS: Final = { - "external_temperature": RpcNumberDescription( - key="blutrv", +RPC_NUMBERS: Final = ( + RpcNumberDescription( + key="external_temperature", + components=("blutrv",), sub_key="current_C", translation_key="external_temperature", native_min_value=-50, @@ -234,8 +235,9 @@ RPC_NUMBERS: Final = { method="blu_trv_set_external_temperature", entity_class=RpcBluTrvExtTempNumber, ), - "number_generic": RpcNumberDescription( - key="number", + RpcNumberDescription( + key="number_generic", + components=("number",), sub_key="value", removal_condition=lambda config, _, key: ( not is_view_for_platform(config, key, NUMBER_DOMAIN) @@ -250,8 +252,9 @@ RPC_NUMBERS: Final = { method="number_set", role=ROLE_GENERIC, ), - "number_current_limit": RpcNumberDescription( - key="number", + RpcNumberDescription( + key="number_current_limit", + components=("number",), sub_key="value", translation_key="current_limit", device_class=NumberDeviceClass.CURRENT, @@ -264,8 +267,9 @@ RPC_NUMBERS: Final = { role="current_limit", models={MODEL_TOP_EV_CHARGER_EVE01}, ), - "number_position": RpcNumberDescription( - key="number", + RpcNumberDescription( + key="number_position", + components=("number",), sub_key="value", translation_key="valve_position", entity_registry_enabled_default=False, @@ -278,8 +282,9 @@ RPC_NUMBERS: Final = { role="position", models={MODEL_FRANKEVER_WATER_VALVE}, ), - "number_target_humidity": RpcNumberDescription( - key="number", + RpcNumberDescription( + key="number_target_humidity", + components=("number",), sub_key="value", translation_key="target_humidity", device_class=NumberDeviceClass.HUMIDITY, @@ -293,8 +298,9 @@ RPC_NUMBERS: Final = { role="target_humidity", models={MODEL_LINKEDGO_ST802_THERMOSTAT, MODEL_LINKEDGO_ST1820_THERMOSTAT}, ), - "number_target_temperature": RpcNumberDescription( - key="number", + RpcNumberDescription( + key="number_target_temperature", + components=("number",), sub_key="value", translation_key="target_temperature", device_class=NumberDeviceClass.TEMPERATURE, @@ -308,8 +314,9 @@ RPC_NUMBERS: Final = { role="target_temperature", models={MODEL_LINKEDGO_ST802_THERMOSTAT, MODEL_LINKEDGO_ST1820_THERMOSTAT}, ), - "valve_position": RpcNumberDescription( - key="blutrv", + RpcNumberDescription( + key="valve_position", + components=("blutrv",), sub_key="pos", translation_key="valve_position", native_min_value=0, @@ -323,8 +330,9 @@ RPC_NUMBERS: Final = { ), entity_class=RpcBluTrvNumber, ), - "left_slot_intensity": RpcNumberDescription( - key="cury", + RpcNumberDescription( + key="left_slot_intensity", + components=("cury",), sub_key="slots", translation_key="left_slot_intensity", value=lambda status, _: status["left"]["intensity"], @@ -341,8 +349,9 @@ RPC_NUMBERS: Final = { ), entity_class=RpcCuryIntensityNumber, ), - "right_slot_intensity": RpcNumberDescription( - key="cury", + RpcNumberDescription( + key="right_slot_intensity", + components=("cury",), sub_key="slots", translation_key="right_slot_intensity", value=lambda status, _: status["right"]["intensity"], @@ -359,8 +368,9 @@ RPC_NUMBERS: Final = { ), entity_class=RpcCuryIntensityNumber, ), - "ledstrip_speed": RpcNumberDescription( - key="ledstrip", + RpcNumberDescription( + key="ledstrip_speed", + components=("ledstrip",), sub_key="speed", translation_key="effect_speed", native_min_value=0, @@ -372,8 +382,9 @@ RPC_NUMBERS: Final = { supported=lambda _: True, entity_class=RpcLedStripNumber, ), - "ledstrip_intensity": RpcNumberDescription( - key="ledstrip", + RpcNumberDescription( + key="ledstrip_intensity", + components=("ledstrip",), sub_key="intensity", translation_key="effect_intensity", native_min_value=0, @@ -385,7 +396,7 @@ RPC_NUMBERS: Final = { supported=lambda _: True, entity_class=RpcLedStripNumber, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/select.py b/homeassistant/components/shelly/select.py index 38ce9a5f73a6..c9b22b6e3e89 100644 --- a/homeassistant/components/shelly/select.py +++ b/homeassistant/components/shelly/select.py @@ -174,9 +174,10 @@ class RpcUiLedModeSelect(RpcSelect): await method({"leds": {"mode": option}}) -RPC_SELECT_ENTITIES: Final = { - "cury_mode": RpcSelectDescription( - key="cury", +RPC_SELECT_ENTITIES: Final = ( + RpcSelectDescription( + key="cury_mode", + components=("cury",), sub_key="mode", translation_key="cury_mode", options=[ @@ -191,8 +192,9 @@ RPC_SELECT_ENTITIES: Final = { method="cury_set_mode", entity_class=RpcCuryModeSelect, ), - "enum_generic": RpcSelectDescription( - key="enum", + RpcSelectDescription( + key="enum_generic", + components=("enum",), sub_key="value", removal_condition=lambda config, _status, key: ( not is_view_for_platform(config, key, SELECT_DOMAIN) @@ -200,16 +202,18 @@ RPC_SELECT_ENTITIES: Final = { method="enum_set", role=ROLE_GENERIC, ), - "ledstrip_palette": RpcSelectDescription( - key="ledstrip", + RpcSelectDescription( + key="ledstrip_palette", + components=("ledstrip",), sub_key="palette", translation_key="effect_palette", method="ledstrip_set", supported=lambda _: True, entity_class=RpcLedStripPaletteSelect, ), - "plugs_ui_led_mode": RpcSelectDescription( - key="plugs_ui", + RpcSelectDescription( + key="plugs_ui_led_mode", + components=("plugs_ui",), translation_key="led_mode", entity_category=EntityCategory.CONFIG, options=["power", "switch", "off"], @@ -217,8 +221,9 @@ RPC_SELECT_ENTITIES: Final = { supported=lambda _: True, entity_class=RpcUiLedModeSelect, ), - "powerstrip_ui_led_mode": RpcSelectDescription( - key="powerstrip_ui", + RpcSelectDescription( + key="powerstrip_ui_led_mode", + components=("powerstrip_ui",), translation_key="led_mode", entity_category=EntityCategory.CONFIG, options=["power", "switch", "off"], @@ -226,7 +231,7 @@ RPC_SELECT_ENTITIES: Final = { supported=lambda _: True, entity_class=RpcUiLedModeSelect, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 33de62d05848..fe9bd3669a37 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -540,9 +540,9 @@ REST_SENSORS: Final = { } -RPC_SENSORS: Final = { - "power": RpcSensorDescription( - key="switch", +RPC_SENSORS: Final = ( + RpcSensorDescription( + key="power", components=("switch", "cover", "light", "pm1", "cct", "rgb", "rgbw", "rgbcct"), legacy_ids={ "light": "power_light", @@ -557,15 +557,17 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), - "power_em1": RpcSensorDescription( - key="em1", + RpcSensorDescription( + key="power_em1", + components=("em1",), sub_key="act_power", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), - "a_act_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="a_act_power", + components=("em",), sub_key="a_act_power", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, @@ -573,8 +575,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_act_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="b_act_power", + components=("em",), sub_key="b_act_power", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, @@ -582,8 +585,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_act_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="c_act_power", + components=("em",), sub_key="c_act_power", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, @@ -591,15 +595,17 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "total_act_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="total_act_power", + components=("em",), sub_key="total_act_power", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), - "a_aprt_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="a_aprt_power", + components=("em",), sub_key="a_aprt_power", native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, device_class=SensorDeviceClass.APPARENT_POWER, @@ -607,8 +613,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_aprt_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="b_aprt_power", + components=("em",), sub_key="b_aprt_power", native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, device_class=SensorDeviceClass.APPARENT_POWER, @@ -616,8 +623,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_aprt_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="c_aprt_power", + components=("em",), sub_key="c_aprt_power", native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, device_class=SensorDeviceClass.APPARENT_POWER, @@ -625,22 +633,24 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "aprt_power_em1": RpcSensorDescription( - key="em1", + RpcSensorDescription( + key="aprt_power_em1", + components=("em1",), sub_key="aprt_power", native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, device_class=SensorDeviceClass.APPARENT_POWER, state_class=SensorStateClass.MEASUREMENT, ), - "total_aprt_power": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="total_aprt_power", + components=("em",), sub_key="total_aprt_power", native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, device_class=SensorDeviceClass.APPARENT_POWER, state_class=SensorStateClass.MEASUREMENT, ), - "switch_pf": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="switch_pf", components=("switch", "cover", "em1"), legacy_ids={ "em1": "pf_em1", @@ -649,32 +659,35 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, ), - "a_pf": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="a_pf", + components=("em",), sub_key="a_pf", device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_pf": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="b_pf", + components=("em",), sub_key="b_pf", device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_pf": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="c_pf", + components=("em",), sub_key="c_pf", device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "voltage": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="voltage", components=("switch", "cover", "em1", "light", "pm1", "cct", "rgb", "rgbw"), legacy_ids={ "em1": "voltage_em1", @@ -692,8 +705,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, ), - "a_voltage": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="a_voltage", + components=("em",), sub_key="a_voltage", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, @@ -702,8 +716,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_voltage": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="b_voltage", + components=("em",), sub_key="b_voltage", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, @@ -712,8 +727,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_voltage": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="c_voltage", + components=("em",), sub_key="c_voltage", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, @@ -722,8 +738,8 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "current": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="current", components=("switch", "cover", "em1", "light", "pm1", "cct", "rgb", "rgbw"), legacy_ids={ "em1": "current_em1", @@ -740,8 +756,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, ), - "a_current": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="a_current", + components=("em",), sub_key="a_current", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, @@ -750,8 +767,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_current": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="b_current", + components=("em",), sub_key="b_current", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, @@ -760,8 +778,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_current": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="c_current", + components=("em",), sub_key="c_current", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, @@ -770,8 +789,9 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "n_current": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="n_current", + components=("em",), sub_key="n_current", translation_key="neutral_current", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, @@ -780,16 +800,17 @@ RPC_SENSORS: Final = { removal_condition=lambda _, status, key: status[key].get("n_current") is None, entity_registry_enabled_default=False, ), - "total_current": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="total_current", + components=("em",), sub_key="total_current", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, ), - "energy": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="energy", components=( "switch", "cover", @@ -817,8 +838,9 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), - "ret_energy": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="ret_energy", + components=("switch",), sub_key="ret_aenergy", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -829,8 +851,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL_INCREASING, removal_condition=lambda _, status, key: status[key].get("ret_aenergy") is None, ), - "consumed_energy_switch": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="consumed_energy_switch", + components=("switch",), sub_key="ret_aenergy", translation_key="energy_consumed", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -843,8 +866,9 @@ RPC_SENSORS: Final = { entity_class=RpcEnergyConsumedSensor, removal_condition=lambda _, status, key: status[key].get("ret_aenergy") is None, ), - "switch_on_time": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="switch_on_time", + components=("switch",), sub_key="counts", translation_key="total_runtime", # API docs state minutes, but the firmware reports seconds @@ -858,8 +882,9 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, removal_condition=lambda _, status, key: "on_time" not in status[key]["counts"], ), - "switch_on_count": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="switch_on_count", + components=("switch",), sub_key="counts", translation_key="switching_cycles", value=lambda status, _: status["switch_on"], @@ -870,8 +895,9 @@ RPC_SENSORS: Final = { "switch_on" not in status[key]["counts"] ), ), - "switch_on_above_thr": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="switch_on_above_thr", + components=("switch",), sub_key="counts", translation_key="runtime_above_power_threshold", # API docs state minutes, but the firmware reports seconds @@ -887,8 +913,9 @@ RPC_SENSORS: Final = { "on_above_thr" not in status[key]["counts"] ), ), - "ret_energy_pm1": RpcSensorDescription( - key="pm1", + RpcSensorDescription( + key="ret_energy_pm1", + components=("pm1",), sub_key="ret_aenergy", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -898,8 +925,9 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), - "consumed_energy_pm1": RpcSensorDescription( - key="pm1", + RpcSensorDescription( + key="consumed_energy_pm1", + components=("pm1",), sub_key="ret_aenergy", translation_key="energy_consumed", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -911,8 +939,9 @@ RPC_SENSORS: Final = { entity_registry_enabled_default=False, entity_class=RpcEnergyConsumedSensor, ), - "total_act": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="total_act", + components=("emdata",), sub_key="total_act", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -921,8 +950,9 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), - "total_act_energy": RpcSensorDescription( - key="em1data", + RpcSensorDescription( + key="total_act_energy", + components=("em1data",), sub_key="total_act_energy", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -932,8 +962,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL_INCREASING, entity_registry_enabled_default=False, ), - "a_total_act_energy": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="a_total_act_energy", + components=("emdata",), sub_key="a_total_act_energy", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -945,8 +976,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_total_act_energy": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="b_total_act_energy", + components=("emdata",), sub_key="b_total_act_energy", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -958,8 +990,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_total_act_energy": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="c_total_act_energy", + components=("emdata",), sub_key="c_total_act_energy", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -971,8 +1004,9 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "total_act_ret": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="total_act_ret", + components=("emdata",), sub_key="total_act_ret", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -982,8 +1016,9 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), - "total_act_ret_energy": RpcSensorDescription( - key="em1data", + RpcSensorDescription( + key="total_act_ret_energy", + components=("em1data",), sub_key="total_act_ret_energy", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -994,8 +1029,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL_INCREASING, entity_registry_enabled_default=False, ), - "a_total_act_ret_energy": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="a_total_act_ret_energy", + components=("emdata",), sub_key="a_total_act_ret_energy", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -1008,8 +1044,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_total_act_ret_energy": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="b_total_act_ret_energy", + components=("emdata",), sub_key="b_total_act_ret_energy", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -1022,8 +1059,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_total_act_ret_energy": RpcSensorDescription( - key="emdata", + RpcSensorDescription( + key="c_total_act_ret_energy", + components=("emdata",), sub_key="c_total_act_ret_energy", translation_key="energy_returned", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, @@ -1036,8 +1074,8 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "freq": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="freq", components=("switch", "cover", "em1", "pm1"), legacy_ids={ "em1": "freq_em1", @@ -1050,8 +1088,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, ), - "a_freq": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="a_freq", + components=("em",), sub_key="a_freq", native_unit_of_measurement=UnitOfFrequency.HERTZ, suggested_display_precision=0, @@ -1061,8 +1100,9 @@ RPC_SENSORS: Final = { emeter_phase="A", entity_class=RpcEmeterPhaseSensor, ), - "b_freq": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="b_freq", + components=("em",), sub_key="b_freq", native_unit_of_measurement=UnitOfFrequency.HERTZ, suggested_display_precision=0, @@ -1072,8 +1112,9 @@ RPC_SENSORS: Final = { emeter_phase="B", entity_class=RpcEmeterPhaseSensor, ), - "c_freq": RpcSensorDescription( - key="em", + RpcSensorDescription( + key="c_freq", + components=("em",), sub_key="c_freq", native_unit_of_measurement=UnitOfFrequency.HERTZ, suggested_display_precision=0, @@ -1083,15 +1124,16 @@ RPC_SENSORS: Final = { emeter_phase="C", entity_class=RpcEmeterPhaseSensor, ), - "illuminance": RpcSensorDescription( + RpcSensorDescription( key="illuminance", + components=("illuminance",), sub_key="lux", native_unit_of_measurement=LIGHT_LUX, device_class=SensorDeviceClass.ILLUMINANCE, state_class=SensorStateClass.MEASUREMENT, ), - "temperature": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="temperature", components=("switch", "cover", "light", "cct", "rgb", "rgbw"), legacy_ids={ "light": "temperature_light", @@ -1109,8 +1151,9 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, use_polling_coordinator=True, ), - "temperature_tc": RpcSensorDescription( - key="temperature", + RpcSensorDescription( + key="temperature_tc", + components=("temperature",), sub_key="tC", native_unit_of_measurement=UnitOfTemperature.CELSIUS, suggested_display_precision=1, @@ -1120,8 +1163,9 @@ RPC_SENSORS: Final = { DRIVER_MISSING_ERROR in (status[key].get("errors") or []) ), ), - "rssi": RpcSensorDescription( - key="wifi", + RpcSensorDescription( + key="rssi", + components=("wifi",), sub_key="rssi", native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, @@ -1131,8 +1175,9 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, use_polling_coordinator=True, ), - "uptime": RpcSensorDescription( - key="sys", + RpcSensorDescription( + key="uptime", + components=("sys",), sub_key="uptime", device_class=SensorDeviceClass.UPTIME, value=lambda status, _: utcnow() - timedelta(seconds=status), @@ -1140,16 +1185,18 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, use_polling_coordinator=True, ), - "last_seen": RpcSensorDescription( - key="sys", + RpcSensorDescription( + key="last_seen", + components=("sys",), sub_key="wakeup_period", translation_key="last_seen", device_class=SensorDeviceClass.TIMESTAMP, entity_category=EntityCategory.DIAGNOSTIC, entity_class=RpcLastSeenSensor, ), - "humidity_rh": RpcSensorDescription( - key="humidity", + RpcSensorDescription( + key="humidity_rh", + components=("humidity",), sub_key="rh", native_unit_of_measurement=UnitOfRatio.PERCENTAGE, suggested_display_precision=1, @@ -1159,8 +1206,9 @@ RPC_SENSORS: Final = { DRIVER_MISSING_ERROR in (status[key].get("errors") or []) ), ), - "battery": RpcSensorDescription( - key="devicepower", + RpcSensorDescription( + key="battery", + components=("devicepower",), sub_key="battery", native_unit_of_measurement=UnitOfRatio.PERCENTAGE, value=lambda status, _: status["percent"], @@ -1169,8 +1217,9 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, removal_condition=lambda _, status, key: status[key]["battery"] is None, ), - "voltmeter": RpcSensorDescription( + RpcSensorDescription( key="voltmeter", + components=("voltmeter",), sub_key="voltage", translation_key="voltmeter", native_unit_of_measurement=UnitOfElectricPotential.VOLT, @@ -1180,16 +1229,18 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, available=lambda status: status is not None, ), - "voltmeter_value": RpcSensorDescription( - key="voltmeter", + RpcSensorDescription( + key="voltmeter_value", + components=("voltmeter",), sub_key="xvoltage", translation_key="voltmeter_value", state_class=SensorStateClass.MEASUREMENT, removal_condition=lambda _, status, key: status[key].get("xvoltage") is None, unit=lambda config: config["xvoltage"]["unit"] or None, ), - "analoginput": RpcSensorDescription( - key="input", + RpcSensorDescription( + key="analoginput", + components=("input",), sub_key="percent", translation_key="analog", native_unit_of_measurement=UnitOfRatio.PERCENTAGE, @@ -1198,8 +1249,9 @@ RPC_SENSORS: Final = { config[key]["type"] != "analog" or config[key]["enable"] is False ), ), - "analoginput_xpercent": RpcSensorDescription( - key="input", + RpcSensorDescription( + key="analoginput_xpercent", + components=("input",), sub_key="xpercent", translation_key="analog_value", state_class=SensorStateClass.MEASUREMENT, @@ -1210,8 +1262,9 @@ RPC_SENSORS: Final = { ), unit=lambda config: config["xpercent"]["unit"] or None, ), - "pulse_counter": RpcSensorDescription( - key="input", + RpcSensorDescription( + key="pulse_counter", + components=("input",), sub_key="counts", translation_key="pulse_counter", native_unit_of_measurement="pulse", @@ -1221,8 +1274,9 @@ RPC_SENSORS: Final = { config[key]["type"] != "count" or config[key]["enable"] is False ), ), - "counter_value": RpcSensorDescription( - key="input", + RpcSensorDescription( + key="counter_value", + components=("input",), sub_key="counts", translation_key="pulse_counter_value", value=lambda status, _: status["xtotal"], @@ -1233,8 +1287,9 @@ RPC_SENSORS: Final = { ), unit=lambda config: config["xcounts"]["unit"] or None, ), - "counter_frequency": RpcSensorDescription( - key="input", + RpcSensorDescription( + key="counter_frequency", + components=("input",), sub_key="freq", translation_key="pulse_counter_frequency", native_unit_of_measurement=UnitOfFrequency.HERTZ, @@ -1243,8 +1298,9 @@ RPC_SENSORS: Final = { config[key]["type"] != "count" or config[key]["enable"] is False ), ), - "counter_frequency_value": RpcSensorDescription( - key="input", + RpcSensorDescription( + key="counter_frequency_value", + components=("input",), sub_key="xfreq", translation_key="pulse_counter_frequency_value", state_class=SensorStateClass.MEASUREMENT, @@ -1255,16 +1311,18 @@ RPC_SENSORS: Final = { ), unit=lambda config: config["xfreq"]["unit"] or None, ), - "text_generic": RpcSensorDescription( - key="text", + RpcSensorDescription( + key="text_generic", + components=("text",), sub_key="value", removal_condition=lambda config, _, key: ( not is_view_for_platform(config, key, SENSOR_DOMAIN) ), role=ROLE_GENERIC, ), - "number_generic": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_generic", + components=("number",), sub_key="value", removal_condition=lambda config, _, key: ( not is_view_for_platform(config, key, SENSOR_DOMAIN) @@ -1272,8 +1330,9 @@ RPC_SENSORS: Final = { unit=get_virtual_component_unit, role=ROLE_GENERIC, ), - "enum_generic": RpcSensorDescription( - key="enum", + RpcSensorDescription( + key="enum_generic", + components=("enum",), sub_key="value", removal_condition=lambda config, _, key: ( not is_view_for_platform(config, key, SENSOR_DOMAIN) @@ -1281,8 +1340,9 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.ENUM, role=ROLE_GENERIC, ), - "valve_position": RpcSensorDescription( - key="blutrv", + RpcSensorDescription( + key="valve_position", + components=("blutrv",), sub_key="pos", translation_key="valve_position", native_unit_of_measurement=UnitOfRatio.PERCENTAGE, @@ -1293,8 +1353,9 @@ RPC_SENSORS: Final = { ), entity_class=RpcBluTrvSensor, ), - "blutrv_battery": RpcSensorDescription( - key="blutrv", + RpcSensorDescription( + key="blutrv_battery", + components=("blutrv",), sub_key="battery", native_unit_of_measurement=UnitOfRatio.PERCENTAGE, device_class=SensorDeviceClass.BATTERY, @@ -1302,8 +1363,9 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, entity_class=RpcBluTrvSensor, ), - "blutrv_rssi": RpcSensorDescription( - key="blutrv", + RpcSensorDescription( + key="blutrv_rssi", + components=("blutrv",), sub_key="rssi", native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, @@ -1311,31 +1373,35 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, entity_class=RpcBluTrvSensor, ), - "illuminance_illumination": RpcSensorDescription( - key="illuminance", + RpcSensorDescription( + key="illuminance_illumination", + components=("illuminance",), sub_key="illumination", translation_key="illuminance_level", device_class=SensorDeviceClass.ENUM, options=["dark", "twilight", "bright"], ), - "fan_light": RpcSensorDescription( - key="fan", + RpcSensorDescription( + key="fan_light", + components=("fan",), sub_key="light", translation_key="illuminance_level", device_class=SensorDeviceClass.ENUM, value=lambda status, _: status["level"], options=["dark", "twilight", "bright"], ), - "fan_hum": RpcSensorDescription( - key="fan", + RpcSensorDescription( + key="fan_hum", + components=("fan",), sub_key="hum", translation_key="humidity_level", device_class=SensorDeviceClass.ENUM, value=lambda status, _: status["level"], options=["low", "medium", "high", "very_high"], ), - "number_average_temperature": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_average_temperature", + components=("number",), sub_key="value", translation_key="average_temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, @@ -1346,8 +1412,9 @@ RPC_SENSORS: Final = { not config.get("service:0", {}).get("weather_api", False) ), ), - "number_last_precipitation": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_last_precipitation", + components=("number",), sub_key="value", translation_key="rainfall", native_unit_of_measurement=UnitOfVolumetricFlux.MILLIMETERS_PER_DAY, @@ -1357,8 +1424,9 @@ RPC_SENSORS: Final = { not config.get("service:0", {}).get("weather_api", False) ), ), - "number_current_humidity": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_current_humidity", + components=("number",), sub_key="value", native_unit_of_measurement=UnitOfRatio.PERCENTAGE, suggested_display_precision=1, @@ -1366,8 +1434,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="current_humidity", ), - "number_current_temperature": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_current_temperature", + components=("number",), sub_key="value", native_unit_of_measurement=UnitOfTemperature.CELSIUS, suggested_display_precision=1, @@ -1375,8 +1444,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="current_temperature", ), - "number_flow_rate": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_flow_rate", + components=("number",), sub_key="value", translation_key="water_flow_rate", native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_MINUTE, @@ -1384,8 +1454,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="flow_rate", ), - "number_water_pressure": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_water_pressure", + components=("number",), sub_key="value", translation_key="water_pressure", native_unit_of_measurement=UnitOfPressure.KPA, @@ -1393,8 +1464,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="water_pressure", ), - "number_water_temperature": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_water_temperature", + components=("number",), sub_key="value", translation_key="water_temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, @@ -1403,15 +1475,17 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="water_temperature", ), - "enum_work_state": RpcSensorDescription( - key="enum", + RpcSensorDescription( + key="enum_work_state", + components=("enum",), sub_key="value", translation_key="charger_state", device_class=SensorDeviceClass.ENUM, role="work_state", ), - "number_energy_charge": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_energy_charge", + components=("number",), sub_key="value", translation_key="session_energy", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -1420,8 +1494,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL, role="energy_charge", ), - "number_time_charge": RpcSensorDescription( - key="number", + RpcSensorDescription( + key="number_time_charge", + components=("number",), sub_key="value", translation_key="session_duration", native_unit_of_measurement=UnitOfTime.MINUTES, @@ -1429,8 +1504,8 @@ RPC_SENSORS: Final = { device_class=SensorDeviceClass.DURATION, role="time_charge", ), - "presence_num_objects": RpcSensorDescription( - key="presence", + RpcSensorDescription( + key="presence_num_objects", components=("presence", "presencezone"), legacy_ids={ "presencezone": "presencezone_num_objects", @@ -1440,8 +1515,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, entity_class=RpcPresenceSensor, ), - "object_water_consumption": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_water_consumption", + components=("object",), sub_key="value", translation_key="water_consumption", value=lambda status, _: float(status["counter"]["total"]), @@ -1451,8 +1527,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL_INCREASING, role="water_consumption", ), - "object_energy_consumption": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_energy_consumption", + components=("object",), sub_key="value", value=lambda status, _: float(status["counter"]["total"]), native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -1461,8 +1538,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL_INCREASING, role="phase_info", ), - "object_total_act_energy": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_total_act_energy", + components=("object",), sub_key="value", value=lambda status, _: float(status["total_act_energy"]), native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, @@ -1471,8 +1549,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.TOTAL_INCREASING, role="phase_info", ), - "object_total_power": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_total_power", + components=("object",), sub_key="value", value=lambda status, _: float(status["total_power"]), native_unit_of_measurement=UnitOfPower.KILO_WATT, @@ -1481,8 +1560,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_a_voltage": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_a_voltage", + components=("object",), sub_key="value", translation_key="voltage_with_phase_name", translation_placeholders={"phase_name": "A"}, @@ -1493,8 +1573,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_b_voltage": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_b_voltage", + components=("object",), sub_key="value", translation_key="voltage_with_phase_name", translation_placeholders={"phase_name": "B"}, @@ -1505,8 +1586,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_c_voltage": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_c_voltage", + components=("object",), sub_key="value", translation_key="voltage_with_phase_name", translation_placeholders={"phase_name": "C"}, @@ -1517,8 +1599,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_a_current": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_a_current", + components=("object",), sub_key="value", translation_key="current_with_phase_name", translation_placeholders={"phase_name": "A"}, @@ -1529,8 +1612,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_b_current": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_b_current", + components=("object",), sub_key="value", translation_key="current_with_phase_name", translation_placeholders={"phase_name": "B"}, @@ -1541,8 +1625,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_c_current": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_c_current", + components=("object",), sub_key="value", translation_key="current_with_phase_name", translation_placeholders={"phase_name": "C"}, @@ -1553,8 +1638,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_a_power": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_a_power", + components=("object",), sub_key="value", translation_key="power_with_phase_name", translation_placeholders={"phase_name": "A"}, @@ -1565,8 +1651,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_b_power": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_b_power", + components=("object",), sub_key="value", translation_key="power_with_phase_name", translation_placeholders={"phase_name": "B"}, @@ -1577,8 +1664,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "object_phase_c_power": RpcSensorDescription( - key="object", + RpcSensorDescription( + key="object_phase_c_power", + components=("object",), sub_key="value", translation_key="power_with_phase_name", translation_placeholders={"phase_name": "C"}, @@ -1589,8 +1677,9 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="phase_info", ), - "cury_left_level": RpcSensorDescription( - key="cury", + RpcSensorDescription( + key="cury_left_level", + components=("cury",), sub_key="slots", translation_key="left_slot_level", value=lambda status, _: status["left"]["vial"]["level"], @@ -1602,8 +1691,9 @@ RPC_SENSORS: Final = { and left.get("vial", {}).get("level", -1) != -1 ), ), - "cury_left_vial": RpcSensorDescription( - key="cury", + RpcSensorDescription( + key="cury_left_vial", + components=("cury",), sub_key="slots", translation_key="left_slot_vial", value=lambda status, _: status["left"]["vial"]["name"], @@ -1613,8 +1703,9 @@ RPC_SENSORS: Final = { and left.get("vial", {}).get("level", -1) != -1 ), ), - "cury_right_level": RpcSensorDescription( - key="cury", + RpcSensorDescription( + key="cury_right_level", + components=("cury",), sub_key="slots", translation_key="right_slot_level", value=lambda status, _: status["right"]["vial"]["level"], @@ -1626,8 +1717,9 @@ RPC_SENSORS: Final = { and right.get("vial", {}).get("level", -1) != -1 ), ), - "cury_right_vial": RpcSensorDescription( - key="cury", + RpcSensorDescription( + key="cury_right_vial", + components=("cury",), sub_key="slots", translation_key="right_slot_vial", value=lambda status, _: status["right"]["vial"]["name"], @@ -1637,8 +1729,9 @@ RPC_SENSORS: Final = { and right.get("vial", {}).get("level", -1) != -1 ), ), - "storage_fs_free": RpcSensorDescription( - key="storage", + RpcSensorDescription( + key="storage_fs_free", + components=("storage",), sub_key="fs_free", translation_key="storage_free_space", state_class=SensorStateClass.MEASUREMENT, @@ -1649,8 +1742,8 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, removal_condition=lambda _, status, key: not status[key]["present"], ), - "switch_source": RpcSensorDescription( - key="switch", + RpcSensorDescription( + key="switch_source", components=("switch", "cover", "light", "cct", "rgb", "rgbw", "rgbcct", "cb"), legacy_ids={ "light": "light_source", @@ -1667,7 +1760,7 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/switch.py b/homeassistant/components/shelly/switch.py index 3e6dc7bdedd2..f90fcafaddf4 100644 --- a/homeassistant/components/shelly/switch.py +++ b/homeassistant/components/shelly/switch.py @@ -243,9 +243,10 @@ class RpcLedStripSwitch(RpcSwitch): ) -RPC_SWITCHES = { - "switch": RpcSwitchDescription( +RPC_SWITCHES = ( + RpcSwitchDescription( key="switch", + components=("switch",), sub_key="output", removal_condition=is_rpc_exclude_from_relay, is_on=lambda status: bool(status["output"]), @@ -254,8 +255,9 @@ RPC_SWITCHES = { method_params_fn=lambda id, value: (id, value), entity_class=RpcRelaySwitch, ), - "cb": RpcSwitchDescription( + RpcSwitchDescription( key="cb", + components=("cb",), sub_key="output", is_on=lambda status: bool(status["output"]), method_on="cb_set", @@ -263,8 +265,9 @@ RPC_SWITCHES = { method_params_fn=lambda id, value: (id, value), entity_class=RpcCircuitBreakerSwitch, ), - "boolean_generic": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_generic", + components=("boolean",), sub_key="value", removal_condition=lambda config, _, key: ( not is_view_for_platform(config, key, SWITCH_DOMAIN) @@ -275,8 +278,9 @@ RPC_SWITCHES = { method_params_fn=lambda id, value: (id, value), role=ROLE_GENERIC, ), - "boolean_anti_freeze": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_anti_freeze", + components=("boolean",), sub_key="value", translation_key="frost_protection", entity_registry_enabled_default=False, @@ -287,8 +291,9 @@ RPC_SWITCHES = { role="anti_freeze", models={MODEL_LINKEDGO_ST802_THERMOSTAT, MODEL_LINKEDGO_ST1820_THERMOSTAT}, ), - "boolean_child_lock": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_child_lock", + components=("boolean",), sub_key="value", translation_key="child_lock", is_on=lambda status: bool(status["value"]), @@ -298,8 +303,9 @@ RPC_SWITCHES = { role="child_lock", models={MODEL_LINKEDGO_ST1820_THERMOSTAT}, ), - "boolean_enable": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_enable", + components=("boolean",), sub_key="value", translation_key="thermostat_enabled", entity_registry_enabled_default=False, @@ -310,8 +316,9 @@ RPC_SWITCHES = { role="enable", models={MODEL_LINKEDGO_ST802_THERMOSTAT, MODEL_LINKEDGO_ST1820_THERMOSTAT}, ), - "boolean_start_charging": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_start_charging", + components=("boolean",), sub_key="value", translation_key="charging", is_on=lambda status: bool(status["value"]), @@ -321,8 +328,9 @@ RPC_SWITCHES = { role="start_charging", models={MODEL_TOP_EV_CHARGER_EVE01}, ), - "boolean_state": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_state", + components=("boolean",), sub_key="value", translation_key="valve_opened", entity_registry_enabled_default=False, @@ -333,8 +341,9 @@ RPC_SWITCHES = { role="state", models={MODEL_NEO_WATER_VALVE}, ), - "boolean_zone0": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_zone0", + components=("boolean",), sub_key="value", translation_key="zone_with_number", translation_placeholders={"zone_number": "1"}, @@ -346,8 +355,9 @@ RPC_SWITCHES = { role="zone0", models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone1": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_zone1", + components=("boolean",), sub_key="value", translation_key="zone_with_number", translation_placeholders={"zone_number": "2"}, @@ -359,8 +369,9 @@ RPC_SWITCHES = { role="zone1", models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone2": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_zone2", + components=("boolean",), sub_key="value", translation_key="zone_with_number", translation_placeholders={"zone_number": "3"}, @@ -372,8 +383,9 @@ RPC_SWITCHES = { role="zone2", models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone3": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_zone3", + components=("boolean",), sub_key="value", translation_key="zone_with_number", translation_placeholders={"zone_number": "4"}, @@ -385,8 +397,9 @@ RPC_SWITCHES = { role="zone3", models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone4": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_zone4", + components=("boolean",), sub_key="value", translation_key="zone_with_number", translation_placeholders={"zone_number": "5"}, @@ -398,8 +411,9 @@ RPC_SWITCHES = { role="zone4", models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone5": RpcSwitchDescription( - key="boolean", + RpcSwitchDescription( + key="boolean_zone5", + components=("boolean",), sub_key="value", translation_key="zone_with_number", translation_placeholders={"zone_number": "6"}, @@ -411,8 +425,9 @@ RPC_SWITCHES = { role="zone5", models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "script": RpcSwitchDescription( + RpcSwitchDescription( key="script", + components=("script",), sub_key="running", is_on=lambda status: bool(status["running"]), method_on="script_start", @@ -421,8 +436,9 @@ RPC_SWITCHES = { entity_registry_enabled_default=False, entity_category=EntityCategory.CONFIG, ), - "cury_left": RpcSwitchDescription( - key="cury", + RpcSwitchDescription( + key="cury_left", + components=("cury",), sub_key="slots", translation_key="left_slot", is_on=lambda status: bool(status["slots"]["left"]["on"]), @@ -434,8 +450,9 @@ RPC_SWITCHES = { and left.get("vial", {}).get("level", -1) != -1 ), ), - "cury_left_boost": RpcSwitchDescription( - key="cury", + RpcSwitchDescription( + key="cury_left_boost", + components=("cury",), sub_key="slots", translation_key="left_slot_boost", is_on=lambda status: status["slots"]["left"]["boost"] is not None, @@ -447,8 +464,9 @@ RPC_SWITCHES = { and left.get("vial", {}).get("level", -1) != -1 ), ), - "cury_right": RpcSwitchDescription( - key="cury", + RpcSwitchDescription( + key="cury_right", + components=("cury",), sub_key="slots", translation_key="right_slot", is_on=lambda status: bool(status["slots"]["right"]["on"]), @@ -460,8 +478,9 @@ RPC_SWITCHES = { and right.get("vial", {}).get("level", -1) != -1 ), ), - "cury_right_boost": RpcSwitchDescription( - key="cury", + RpcSwitchDescription( + key="cury_right_boost", + components=("cury",), sub_key="slots", translation_key="right_slot_boost", is_on=lambda status: status["slots"]["right"]["boost"] is not None, @@ -473,8 +492,9 @@ RPC_SWITCHES = { and right.get("vial", {}).get("level", -1) != -1 ), ), - "cury_away_mode": RpcSwitchDescription( - key="cury", + RpcSwitchDescription( + key="cury_away_mode", + components=("cury",), sub_key="away_mode", translation_key="cury_away_mode", is_on=lambda status: status["away_mode"], @@ -482,8 +502,9 @@ RPC_SWITCHES = { method_off="cury_set_away_mode", method_params_fn=lambda id, value: (id, value), ), - "fan_boost": RpcSwitchDescription( - key="fan", + RpcSwitchDescription( + key="fan_boost", + components=("fan",), sub_key="boost", translation_key="boost", is_on=lambda status: status["boost"]["active"], @@ -492,8 +513,9 @@ RPC_SWITCHES = { method_params_fn=lambda id, _: (id,), entity_class=RpcFanBoostSwitch, ), - "ledstrip_reverse": RpcSwitchDescription( - key="ledstrip", + RpcSwitchDescription( + key="ledstrip_reverse", + components=("ledstrip",), sub_key="reverse", translation_key="effect_reverse", is_on=lambda status: bool(status["reverse"]), @@ -503,8 +525,8 @@ RPC_SWITCHES = { supported=lambda _: True, entity_class=RpcLedStripSwitch, ), - "in_locked": RpcSwitchDescription( - key="switch", + RpcSwitchDescription( + key="in_locked", components=("switch", "cover"), translation_key="input_lock", entity_category=EntityCategory.CONFIG, @@ -516,8 +538,9 @@ RPC_SWITCHES = { method_params_fn=lambda id, value: (id, {"in_locked": value}), entity_class=RpcConfigSwitch, ), - "camera_privacy": RpcSwitchDescription( - key="camera", + RpcSwitchDescription( + key="camera_privacy", + components=("camera",), sub_key="privacy", translation_key="camera_privacy", is_on=lambda status: status["privacy"], @@ -526,7 +549,7 @@ RPC_SWITCHES = { method_params_fn=lambda id, value: (id, value), entity_category=EntityCategory.CONFIG, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/text.py b/homeassistant/components/shelly/text.py index 74c4b84c0901..e7326142e302 100644 --- a/homeassistant/components/shelly/text.py +++ b/homeassistant/components/shelly/text.py @@ -37,16 +37,17 @@ class RpcTextDescription(RpcEntityDescription, TextEntityDescription): """Class to describe a RPC text entity.""" -RPC_TEXT_ENTITIES: Final = { - "text_generic": RpcTextDescription( - key="text", +RPC_TEXT_ENTITIES: Final = ( + RpcTextDescription( + key="text_generic", + components=("text",), sub_key="value", removal_condition=lambda config, _status, key: ( not is_view_for_platform(config, key, TEXT_DOMAIN) ), role=ROLE_GENERIC, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/update.py b/homeassistant/components/shelly/update.py index 4aeb3e2bdd85..efbdaa4aaaf1 100644 --- a/homeassistant/components/shelly/update.py +++ b/homeassistant/components/shelly/update.py @@ -315,17 +315,19 @@ class RpcBluTrvUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity): ) -RPC_UPDATES: Final = { - "fwupdate": RpcUpdateDescription( - key="sys", +RPC_UPDATES: Final = ( + RpcUpdateDescription( + key="fwupdate", + components=("sys",), sub_key="available_updates", latest_version=lambda status: status.get("stable", {"version": ""})["version"], beta=False, device_class=UpdateDeviceClass.FIRMWARE, entity_category=EntityCategory.CONFIG, ), - "fwupdate_beta": RpcUpdateDescription( - key="sys", + RpcUpdateDescription( + key="fwupdate_beta", + components=("sys",), sub_key="available_updates", translation_key="beta_firmware", latest_version=lambda status: status.get("beta", {"version": ""})["version"], @@ -334,8 +336,9 @@ RPC_UPDATES: Final = { entity_category=EntityCategory.CONFIG, entity_registry_enabled_default=False, ), - "loraupdate": RpcUpdateDescription( - key="lora", + RpcUpdateDescription( + key="loraupdate", + components=("lora",), translation_key="lora_firmware", # Right after firmware update, lora status can be None latest_version=lambda status: status or {}, @@ -344,14 +347,15 @@ RPC_UPDATES: Final = { entity_category=EntityCategory.CONFIG, entity_class=RpcLoraAddOnUpdateEntity, ), - "blutrv_fwupdate": RpcBluTrvUpdateDescription( - key="blutrv", + RpcBluTrvUpdateDescription( + key="blutrv_fwupdate", + components=("blutrv",), sub_key="fw_ver", device_class=UpdateDeviceClass.FIRMWARE, entity_category=EntityCategory.CONFIG, entity_class=RpcBluTrvUpdateEntity, ), -} +) async def async_setup_entry( diff --git a/homeassistant/components/shelly/valve.py b/homeassistant/components/shelly/valve.py index f61017e4b957..f22dfd1f6a82 100644 --- a/homeassistant/components/shelly/valve.py +++ b/homeassistant/components/shelly/valve.py @@ -119,64 +119,72 @@ class RpcShellySimpleWaterValve(RpcShellyBaseWaterValve): await self.coordinator.device.boolean_set(self._id, False) -RPC_VALVES: dict[str, RpcValveDescription] = { - "water_valve": RpcValveDescription( - key="number", +RPC_VALVES: tuple[RpcValveDescription, ...] = ( + RpcValveDescription( + key="water_valve", + components=("number",), sub_key="value", role="position", entity_class=RpcShellyWaterValve, models={MODEL_FRANKEVER_WATER_VALVE}, ), - "neo_water_valve": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="neo_water_valve", + components=("boolean",), sub_key="value", role="state", entity_class=RpcShellySimpleWaterValve, models={MODEL_NEO_WATER_VALVE}, ), - "boolean_zone0": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="boolean_zone0", + components=("boolean",), sub_key="value", role="zone0", entity_class=RpcShellySimpleWaterValve, models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone1": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="boolean_zone1", + components=("boolean",), sub_key="value", role="zone1", entity_class=RpcShellySimpleWaterValve, models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone2": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="boolean_zone2", + components=("boolean",), sub_key="value", role="zone2", entity_class=RpcShellySimpleWaterValve, models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone3": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="boolean_zone3", + components=("boolean",), sub_key="value", role="zone3", entity_class=RpcShellySimpleWaterValve, models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone4": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="boolean_zone4", + components=("boolean",), sub_key="value", role="zone4", entity_class=RpcShellySimpleWaterValve, models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), - "boolean_zone5": RpcValveDescription( - key="boolean", + RpcValveDescription( + key="boolean_zone5", + components=("boolean",), sub_key="value", role="zone5", entity_class=RpcShellySimpleWaterValve, models={MODEL_FRANKEVER_IRRIGATION_CONTROLLER}, ), -} +) async def async_setup_entry( diff --git a/tests/components/shelly/test_entity.py b/tests/components/shelly/test_entity.py new file mode 100644 index 000000000000..2b7369879afb --- /dev/null +++ b/tests/components/shelly/test_entity.py @@ -0,0 +1,89 @@ +"""Tests for Shelly entity helpers.""" + +import pytest + +from homeassistant.components.shelly.binary_sensor import ( + RPC_SENSORS as RPC_BINARY_SENSORS, +) +from homeassistant.components.shelly.button import RPC_BUTTONS +from homeassistant.components.shelly.camera import RPC_CAMERA_ENTITIES +from homeassistant.components.shelly.climate import RPC_LINKEDGO_THERMOSTAT +from homeassistant.components.shelly.cover import RPC_COVERS +from homeassistant.components.shelly.entity import ( + RpcEntityDescription, + get_rpc_descriptions_by_attribute, +) +from homeassistant.components.shelly.fan import RPC_FANS +from homeassistant.components.shelly.infrared import RPC_INFRARED_ENTITIES +from homeassistant.components.shelly.light import LIGHTS +from homeassistant.components.shelly.media_player import RPC_MEDIA_PLAYER_ENTITIES +from homeassistant.components.shelly.number import RPC_NUMBERS +from homeassistant.components.shelly.select import RPC_SELECT_ENTITIES +from homeassistant.components.shelly.sensor import RPC_SENSORS +from homeassistant.components.shelly.switch import RPC_SWITCHES +from homeassistant.components.shelly.text import RPC_TEXT_ENTITIES +from homeassistant.components.shelly.update import RPC_UPDATES +from homeassistant.components.shelly.valve import RPC_VALVES + + +@pytest.mark.parametrize( + "descriptions", + [ + pytest.param(RPC_BINARY_SENSORS, id="binary_sensor"), + pytest.param(RPC_BUTTONS, id="button"), + pytest.param(RPC_CAMERA_ENTITIES, id="camera"), + pytest.param(RPC_LINKEDGO_THERMOSTAT, id="climate"), + pytest.param(RPC_COVERS, id="cover"), + pytest.param(RPC_FANS, id="fan"), + pytest.param(RPC_INFRARED_ENTITIES, id="infrared"), + pytest.param(LIGHTS, id="light"), + pytest.param(RPC_MEDIA_PLAYER_ENTITIES, id="media_player"), + pytest.param(RPC_NUMBERS, id="number"), + pytest.param(RPC_SELECT_ENTITIES, id="select"), + pytest.param(RPC_SENSORS, id="sensor"), + pytest.param(RPC_SWITCHES, id="switch"), + pytest.param(RPC_TEXT_ENTITIES, id="text"), + pytest.param(RPC_UPDATES, id="update"), + pytest.param(RPC_VALVES, id="valve"), + ], +) +def test_rpc_description_attributes_are_unique( + descriptions: tuple[RpcEntityDescription, ...], +) -> None: + """Test the keys and legacy IDs of a platform's RPC descriptions are unique.""" + by_attribute = get_rpc_descriptions_by_attribute(descriptions) + + assert all( + by_attribute[description.key] is description for description in descriptions + ) + + +@pytest.mark.parametrize( + "descriptions", + [ + pytest.param( + ( + RpcEntityDescription(key="power", components=("switch",)), + RpcEntityDescription(key="power", components=("light",)), + ), + id="key", + ), + pytest.param( + ( + RpcEntityDescription(key="power", components=("switch",)), + RpcEntityDescription( + key="power_pm1", + components=("pm1", "light"), + legacy_ids={"light": "power"}, + ), + ), + id="legacy_id", + ), + ], +) +def test_rpc_description_duplicate_attribute( + descriptions: tuple[RpcEntityDescription, ...], +) -> None: + """Test a key or legacy ID used by two RPC descriptions is rejected.""" + with pytest.raises(ValueError, match="Duplicate RPC description attribute: power"): + get_rpc_descriptions_by_attribute(descriptions)