1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-14 18:14:35 +01:00

Fix Shelly virtual component unit retrieval (#173183)

This commit is contained in:
Shay Levy
2026-06-07 00:34:18 +03:00
committed by Franck Nijhof
parent e123b29258
commit a15d80daa2
2 changed files with 28 additions and 2 deletions
+2 -2
View File
@@ -663,9 +663,9 @@ def is_view_for_platform(config: dict[str, Any], key: str, platform: str) -> boo
def get_virtual_component_unit(config: dict[str, Any]) -> str | None:
"""Return the unit of a virtual component.
If the unit is not set, the device sends an empty string
If the unit is not set, the device sends an empty string or the key may be absent.
"""
unit = config["meta"]["ui"]["unit"]
unit = config["meta"]["ui"].get("unit")
return DEVICE_UNIT_MAP.get(unit, unit) if unit else None
+26
View File
@@ -1390,6 +1390,32 @@ async def test_rpc_device_virtual_number_sensor(
assert state.state == "56.7"
async def test_rpc_device_virtual_number_sensor_no_unit(
hass: HomeAssistant,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test a virtual number sensor with no unit key in meta.ui."""
config = deepcopy(mock_rpc_device.config)
config["number:203"] = {
"name": "Virtual number sensor",
"min": 0,
"max": 100,
"meta": {"ui": {"step": 0.1, "view": "label"}},
}
monkeypatch.setattr(mock_rpc_device, "config", config)
status = deepcopy(mock_rpc_device.status)
status["number:203"] = {"value": 34.5}
monkeypatch.setattr(mock_rpc_device, "status", status)
await init_integration(hass, 3)
assert (state := hass.states.get("sensor.test_name_virtual_number_sensor"))
assert state.state == "34.5"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
@pytest.mark.usefixtures("disable_async_remove_shelly_rpc_entities")
async def test_rpc_remove_number_virtual_sensor_when_mode_field(
hass: HomeAssistant,