Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions homeassistant/components/shelly/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,14 +897,14 @@ def get_block_coordinator_by_device_id(
) -> ShellyBlockCoordinator | None:
"""Get a Shelly block device coordinator for the given device id."""
dev_reg = dr.async_get(hass)
entry: ShellyConfigEntry | None
if device := dev_reg.async_get(device_id):
for config_entry in device.config_entries:
entry = hass.config_entries.async_get_entry(config_entry)
if (
entry
and entry.state is ConfigEntryState.LOADED
and hasattr(entry, "runtime_data")
and isinstance(entry.runtime_data, ShellyEntryData)
Comment on lines -906 to -907

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find ANY path in which the entry would be loaded without the runtime_data being present.

and entry.domain == DOMAIN
and (coordinator := entry.runtime_data.block)
Comment thread
epenet marked this conversation as resolved.
):
return coordinator
Expand All @@ -917,14 +917,14 @@ def get_rpc_coordinator_by_device_id(
) -> ShellyRpcCoordinator | None:
"""Get a Shelly RPC device coordinator for the given device id."""
dev_reg = dr.async_get(hass)
entry: ShellyConfigEntry | None
if device := dev_reg.async_get(device_id):
for config_entry in device.config_entries:
entry = hass.config_entries.async_get_entry(config_entry)
if (
entry
and entry.state is ConfigEntryState.LOADED
and hasattr(entry, "runtime_data")
and isinstance(entry.runtime_data, ShellyEntryData)
and entry.domain == DOMAIN
and (coordinator := entry.runtime_data.rpc)
Comment thread
epenet marked this conversation as resolved.
):
return coordinator
Expand Down
90 changes: 0 additions & 90 deletions tests/components/shelly/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,93 +391,3 @@ async def test_validate_trigger_invalid_triggers(
"Invalid device automation trigger (type, subtype): ('single', 'button3')"
in caplog.text
)


async def test_rpc_no_runtime_data(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
service_calls: list[ServiceCall],
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test the device trigger for the RPC device when there is no runtime_data in the entry."""
Comment on lines -396 to -403

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR marked as draft to let code-owners describe why these tests were added...

entry = await init_integration(hass, 2)
monkeypatch.delattr(entry, "runtime_data")
device = dr.async_entries_for_config_entry(device_registry, entry.entry_id)[0]

assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
CONF_PLATFORM: "device",
CONF_DOMAIN: DOMAIN,
CONF_DEVICE_ID: device.id,
CONF_TYPE: "single_push",
CONF_SUBTYPE: "button1",
},
"action": {
"service": "test.automation",
"data_template": {"some": "test_trigger_single_push"},
},
},
]
},
)
message = {
CONF_DEVICE_ID: device.id,
ATTR_CLICK_TYPE: "single_push",
ATTR_CHANNEL: 1,
}
hass.bus.async_fire(EVENT_SHELLY_CLICK, message)
await hass.async_block_till_done()

assert len(service_calls) == 1
assert service_calls[0].data["some"] == "test_trigger_single_push"


async def test_block_no_runtime_data(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
service_calls: list[ServiceCall],
mock_block_device: Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test the device trigger for the block device when there is no runtime_data in the entry."""
entry = await init_integration(hass, 1)
monkeypatch.delattr(entry, "runtime_data")
device = dr.async_entries_for_config_entry(device_registry, entry.entry_id)[0]

assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
CONF_PLATFORM: "device",
CONF_DOMAIN: DOMAIN,
CONF_DEVICE_ID: device.id,
CONF_TYPE: "single",
CONF_SUBTYPE: "button1",
},
"action": {
"service": "test.automation",
"data_template": {"some": "test_trigger_single"},
},
},
]
},
)
message = {
CONF_DEVICE_ID: device.id,
ATTR_CLICK_TYPE: "single",
ATTR_CHANNEL: 1,
}
hass.bus.async_fire(EVENT_SHELLY_CLICK, message)
await hass.async_block_till_done()

assert len(service_calls) == 1
assert service_calls[0].data["some"] == "test_trigger_single"