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:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user