Skip to content

Commit 5b8d373

Browse files
authored
Fix Shelly irrigation zone ID retrieval with Sleepy devices (home-assistant#155514)
1 parent 4e3664b commit 5b8d373

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

homeassistant/components/shelly/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def get_rpc_sub_device_name(
417417
"""Get name based on device and channel name."""
418418
if key in device.config and key != "em:0":
419419
# workaround for Pro 3EM, we don't want to get name for em:0
420-
if (zone_id := get_irrigation_zone_id(device.config, key)) is not None:
420+
if (zone_id := get_irrigation_zone_id(device, key)) is not None:
421421
# workaround for Irrigation controller, name stored in "service:0"
422422
if zone_name := device.config["service:0"]["zones"][zone_id]["name"]:
423423
return cast(str, zone_name)
@@ -792,9 +792,13 @@ async def get_rpc_scripts_event_types(
792792
return script_events
793793

794794

795-
def get_irrigation_zone_id(config: dict[str, Any], key: str) -> int | None:
795+
def get_irrigation_zone_id(device: RpcDevice, key: str) -> int | None:
796796
"""Return the zone id if the component is an irrigation zone."""
797-
if key in config and (zone := get_rpc_role_by_key(config, key)).startswith("zone"):
797+
if (
798+
device.initialized
799+
and key in device.config
800+
and (zone := get_rpc_role_by_key(device.config, key)).startswith("zone")
801+
):
798802
return int(zone[4:])
799803
return None
800804

@@ -837,7 +841,7 @@ def get_rpc_device_info(
837841
if (
838842
(
839843
component not in (*All_LIGHT_TYPES, "cover", "em1", "switch")
840-
and get_irrigation_zone_id(device.config, key) is None
844+
and get_irrigation_zone_id(device, key) is None
841845
)
842846
or idx is None
843847
or len(get_rpc_key_instances(device.status, component, all_lights=True)) < 2

tests/components/shelly/test_sensor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Tests for Shelly sensor platform."""
22

33
from copy import deepcopy
4-
from unittest.mock import Mock
4+
from unittest.mock import Mock, PropertyMock
55

66
from aioshelly.const import MODEL_BLU_GATEWAY_G3
7+
from aioshelly.exceptions import NotInitialized
78
from freezegun.api import FrozenDateTimeFactory
89
import pytest
910
from syrupy.assertion import SnapshotAssertion
@@ -52,6 +53,7 @@
5253
register_device,
5354
register_entity,
5455
)
56+
from .conftest import MOCK_CONFIG, MOCK_SHELLY_RPC, MOCK_STATUS_RPC
5557

5658
from tests.common import (
5759
async_fire_time_changed,
@@ -632,6 +634,9 @@ async def test_rpc_restored_sleeping_sensor(
632634
extra_data = {"native_value": "21.0", "native_unit_of_measurement": "°C"}
633635

634636
mock_restore_cache_with_extra_data(hass, ((State(entity_id, ""), extra_data),))
637+
type(mock_rpc_device).shelly = PropertyMock(side_effect=NotInitialized())
638+
type(mock_rpc_device).config = PropertyMock(side_effect=NotInitialized())
639+
type(mock_rpc_device).status = PropertyMock(side_effect=NotInitialized())
635640
monkeypatch.setattr(mock_rpc_device, "initialized", False)
636641

637642
await hass.config_entries.async_setup(entry.entry_id)
@@ -641,6 +646,9 @@ async def test_rpc_restored_sleeping_sensor(
641646
assert state.state == "21.0"
642647

643648
# Make device online
649+
type(mock_rpc_device).shelly = PropertyMock(return_value=MOCK_SHELLY_RPC)
650+
type(mock_rpc_device).config = PropertyMock(return_value=MOCK_CONFIG)
651+
type(mock_rpc_device).status = PropertyMock(return_value=MOCK_STATUS_RPC)
644652
monkeypatch.setattr(mock_rpc_device, "initialized", True)
645653
mock_rpc_device.mock_online()
646654
await hass.async_block_till_done(wait_background_tasks=True)

0 commit comments

Comments
 (0)