Fix LG webOS TV trigger validation (#135312)

* Fix LG webOS TV trigger validation

* Raise if not loaded
This commit is contained in:
Shay Levy 2025-01-13 15:37:40 +02:00 committed by GitHub
parent ec5759d3b9
commit ba9ad009e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 14 deletions

View File

@ -15,7 +15,6 @@ from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import trigger
from .const import DOMAIN
from .helpers import (
async_get_client_by_device_entry,
async_get_device_entry_by_device_id,
@ -43,8 +42,7 @@ async def async_validate_trigger_config(
device_id = config[CONF_DEVICE_ID]
try:
device = async_get_device_entry_by_device_id(hass, device_id)
if DOMAIN in hass.data:
async_get_client_by_device_entry(hass, device)
async_get_client_by_device_entry(hass, device)
except ValueError as err:
raise InvalidDeviceAutomationConfig(err) from err

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from aiowebostv import WebOsClient
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry
@ -55,16 +56,18 @@ def async_get_client_by_device_entry(
Raises ValueError if client is not found.
"""
for config_entry_id in device.config_entries:
if entry := hass.config_entries.async_get_entry(config_entry_id):
client = entry.runtime_data
break
entry = hass.config_entries.async_get_entry(config_entry_id)
if entry and entry.domain == DOMAIN:
if entry.state is ConfigEntryState.LOADED:
return entry.runtime_data
if not client:
raise ValueError(
f"Device {device.id} is not from an existing {DOMAIN} config entry"
)
raise ValueError(
f"Device {device.id} is not from a loaded {DOMAIN} config entry"
)
return client
raise ValueError(
f"Device {device.id} is not from an existing {DOMAIN} config entry"
)
async def async_get_sources(host: str, key: str) -> list[str]:

View File

@ -104,10 +104,10 @@ async def test_if_fires_on_turn_on_request(
assert service_calls[2].data["id"] == 0
async def test_failure_scenarios(
async def test_invalid_trigger_raises(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, client
) -> None:
"""Test failure scenarios."""
"""Test invalid trigger platform or device id raises."""
await setup_webostv(hass)
# Test wrong trigger platform type
@ -128,7 +128,26 @@ async def test_failure_scenarios(
},
)
entry = MockConfigEntry(domain="fake", state=ConfigEntryState.LOADED, data={})
@pytest.mark.parametrize(
("domain", "entry_state"),
[
(DOMAIN, ConfigEntryState.NOT_LOADED),
("fake", ConfigEntryState.LOADED),
],
)
async def test_invalid_entry_raises(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
client,
caplog: pytest.LogCaptureFixture,
domain: str,
entry_state: ConfigEntryState,
) -> None:
"""Test device id not loaded or from another domain raises."""
await setup_webostv(hass)
entry = MockConfigEntry(domain=domain, state=entry_state, data={})
entry.runtime_data = None
entry.add_to_hass(hass)