From 05277aa708d39ae89d5b72c67e72818ff6a3d7b7 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Sun, 19 Oct 2025 12:51:18 +0200 Subject: [PATCH] Fix Shelly enum sensors (#154814) --- homeassistant/components/shelly/entity.py | 1 - homeassistant/components/shelly/sensor.py | 25 ++++++++----------- .../shelly/snapshots/test_sensor.ambr | 2 +- tests/components/shelly/test_sensor.py | 14 +++++------ 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index f9eb51c2ac8d..b350407f117c 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -323,7 +323,6 @@ class RpcEntityDescription(EntityDescription): use_polling_coordinator: bool = False supported: Callable = lambda _: False unit: Callable[[dict], str | None] | None = None - options_fn: Callable[[dict], list[str]] | None = None entity_class: Callable | None = None role: str | None = None models: set[str] | None = None diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index ec1abd415ae6..c4ee99faa917 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -104,7 +104,10 @@ class RpcSensor(ShellyRpcAttributeEntity, SensorEntity): super().__init__(coordinator, key, attribute, description) if self.option_map: - self._attr_options = list(self.option_map.values()) + if description.role == ROLE_GENERIC: + self._attr_options = list(self.option_map.values()) + else: + self._attr_options = list(self.option_map) @property def native_value(self) -> StateType: @@ -117,7 +120,10 @@ class RpcSensor(ShellyRpcAttributeEntity, SensorEntity): if not isinstance(attribute_value, str): return None - return self.option_map[attribute_value] + if self.entity_description.role == ROLE_GENERIC: + return self.option_map[attribute_value] + + return attribute_value class RpcEnergyConsumedSensor(RpcSensor): @@ -1442,7 +1448,6 @@ RPC_SENSORS: Final = { removal_condition=lambda config, _, key: not is_view_for_platform( config, key, SENSOR_PLATFORM ), - options_fn=lambda config: config["options"], device_class=SensorDeviceClass.ENUM, role=ROLE_GENERIC, ), @@ -1532,21 +1537,11 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, role="water_temperature", ), - "number_work_state": RpcSensorDescription( - key="number", + "enum_work_state": RpcSensorDescription( + key="enum", sub_key="value", translation_key="charger_state", device_class=SensorDeviceClass.ENUM, - options=[ - "charger_charging", - "charger_end", - "charger_fault", - "charger_free", - "charger_free_fault", - "charger_insert", - "charger_pause", - "charger_wait", - ], role="work_state", ), "number_energy_charge": RpcSensorDescription( diff --git a/tests/components/shelly/snapshots/test_sensor.ambr b/tests/components/shelly/snapshots/test_sensor.ambr index 11892f586a66..93be3c9103e7 100644 --- a/tests/components/shelly/snapshots/test_sensor.ambr +++ b/tests/components/shelly/snapshots/test_sensor.ambr @@ -399,7 +399,7 @@ 'suggested_object_id': None, 'supported_features': 0, 'translation_key': 'charger_state', - 'unique_id': '123456789ABC-number:200-number_work_state', + 'unique_id': '123456789ABC-enum:200-enum_work_state', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 640d7439eb9f..3aace4a88170 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -1693,7 +1693,7 @@ async def test_rpc_shelly_ev_sensors( ) -> None: """Test Shelly EV sensors.""" config = deepcopy(mock_rpc_device.config) - config["number:200"] = { + config["enum:200"] = { "name": "Charger state", "meta": { "ui": { @@ -1711,14 +1711,14 @@ async def test_rpc_shelly_ev_sensors( } }, "options": [ - "charger_free", - "charger_insert", - "charger_free_fault", - "charger_wait", "charger_charging", - "charger_pause", "charger_end", "charger_fault", + "charger_free", + "charger_free_fault", + "charger_insert", + "charger_pause", + "charger_wait", ], "role": "work_state", } @@ -1735,7 +1735,7 @@ async def test_rpc_shelly_ev_sensors( monkeypatch.setattr(mock_rpc_device, "config", config) status = deepcopy(mock_rpc_device.status) - status["number:200"] = {"value": "charger_charging"} + status["enum:200"] = {"value": "charger_charging"} status["number:201"] = {"value": 5.0} status["number:202"] = {"value": 60} monkeypatch.setattr(mock_rpc_device, "status", status)