Skip to content

Commit c8c2413

Browse files
authored
Fix Shelly sleeping sensor with channel name (home-assistant#156708)
Signed-off-by: David Rapan <[email protected]>
1 parent 291331f commit c8c2413

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

homeassistant/components/shelly/sensor.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,8 +1919,23 @@ def __init__(
19191919
super().__init__(coordinator, key, attribute, description, entry)
19201920
self.restored_data: SensorExtraStoredData | None = None
19211921

1922-
if hasattr(self, "_attr_name"):
1923-
delattr(self, "_attr_name")
1922+
if coordinator.device.initialized:
1923+
if hasattr(self, "_attr_name"):
1924+
delattr(self, "_attr_name")
1925+
1926+
translation_placeholders, translation_key = (
1927+
get_entity_translation_attributes(
1928+
get_rpc_channel_name(coordinator.device, key),
1929+
description.translation_key,
1930+
description.device_class,
1931+
self._default_to_device_class_name(),
1932+
)
1933+
)
1934+
1935+
if translation_placeholders:
1936+
self._attr_translation_placeholders = translation_placeholders
1937+
if translation_key:
1938+
self._attr_translation_key = translation_key
19241939

19251940
async def async_added_to_hass(self) -> None:
19261941
"""Handle entity which will be added."""

tests/components/shelly/test_sensor.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,31 +580,55 @@ async def test_rpc_polling_sensor(
580580
async def test_rpc_sleeping_sensor(
581581
hass: HomeAssistant,
582582
mock_rpc_device: Mock,
583-
device_registry: DeviceRegistry,
584583
monkeypatch: pytest.MonkeyPatch,
585584
) -> None:
586585
"""Test RPC online sleeping sensor."""
587586
entity_id = f"{SENSOR_DOMAIN}.test_name_temperature"
588587
monkeypatch.setattr(mock_rpc_device, "connected", False)
589588
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
590-
entry = await init_integration(hass, 2, sleep_period=1000)
589+
await init_integration(hass, 2, sleep_period=1000)
591590

592591
# Sensor should be created when device is online
593592
assert hass.states.get(entity_id) is None
594593

595-
register_entity(
596-
hass,
597-
SENSOR_DOMAIN,
598-
"test_name_temperature",
599-
"temperature:0-temperature_0",
600-
entry,
594+
# Make device online
595+
mock_rpc_device.mock_online()
596+
await hass.async_block_till_done(wait_background_tasks=True)
597+
598+
assert (state := hass.states.get(entity_id))
599+
assert state.state == "22.9"
600+
601+
mutate_rpc_device_status(monkeypatch, mock_rpc_device, "temperature:0", "tC", 23.4)
602+
mock_rpc_device.mock_update()
603+
604+
assert (state := hass.states.get(entity_id))
605+
assert state.state == "23.4"
606+
607+
608+
async def test_rpc_sleeping_sensor_with_channel_name(
609+
hass: HomeAssistant,
610+
mock_rpc_device: Mock,
611+
monkeypatch: pytest.MonkeyPatch,
612+
) -> None:
613+
"""Test RPC online sleeping sensor with channel name."""
614+
name = "test channel name"
615+
entity_id = f"{SENSOR_DOMAIN}.test_name_test_channel_name_temperature"
616+
monkeypatch.setitem(
617+
mock_rpc_device.config, "temperature:0", {"id": 0, "name": name}
601618
)
619+
monkeypatch.setattr(mock_rpc_device, "connected", False)
620+
monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000)
621+
await init_integration(hass, 2, sleep_period=1000)
622+
623+
# Sensor should be created when device is online
624+
assert hass.states.get(entity_id) is None
602625

603626
# Make device online
604627
mock_rpc_device.mock_online()
605628
await hass.async_block_till_done(wait_background_tasks=True)
606629

607630
assert (state := hass.states.get(entity_id))
631+
assert state.attributes["friendly_name"] == f"Test name {name} temperature"
608632
assert state.state == "22.9"
609633

610634
mutate_rpc_device_status(monkeypatch, mock_rpc_device, "temperature:0", "tC", 23.4)

0 commit comments

Comments
 (0)