Fix Shelly enum sensors (#154814)

This commit is contained in:
Maciej Bieniek
2025-10-19 12:51:18 +02:00
committed by GitHub
parent 9f74471d22
commit 05277aa708
4 changed files with 18 additions and 24 deletions
@@ -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
+10 -15
View File
@@ -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(
@@ -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,
})
# ---
+7 -7
View File
@@ -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)