mirror of
https://github.com/home-assistant/core.git
synced 2026-09-12 19:49:41 +01:00
Adjust lg_netcast to not access DeviceEntry.config_entries (#181726)
This commit is contained in:
@@ -8,15 +8,16 @@ from homeassistant.components.device_automation import (
|
||||
DEVICE_TRIGGER_BASE_SCHEMA,
|
||||
InvalidDeviceAutomationConfig,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_DEVICE_ID, CONF_PLATFORM, CONF_TYPE
|
||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
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_device_entry_by_device_id
|
||||
from .triggers.turn_on import (
|
||||
PLATFORM_TYPE as TURN_ON_PLATFORM_TYPE,
|
||||
async_get_turn_on_trigger,
|
||||
@@ -40,15 +41,14 @@ async def async_validate_trigger_config(
|
||||
if config[CONF_TYPE] == TURN_ON_PLATFORM_TYPE:
|
||||
device_id = config[CONF_DEVICE_ID]
|
||||
|
||||
try:
|
||||
device = async_get_device_entry_by_device_id(hass, device_id)
|
||||
except ValueError as err:
|
||||
raise InvalidDeviceAutomationConfig(err) from err
|
||||
|
||||
if not any(
|
||||
entry.entry_id in device.config_entries
|
||||
for entry in hass.config_entries.async_loaded_entries(DOMAIN)
|
||||
):
|
||||
device, config_entry = dr.async_get_device_and_config_entry_for_domain(
|
||||
hass, device_id, domain=DOMAIN
|
||||
)
|
||||
if device is None:
|
||||
raise InvalidDeviceAutomationConfig(
|
||||
f"Device {device_id} is not a valid {DOMAIN} device."
|
||||
)
|
||||
if config_entry is None or config_entry.state is not ConfigEntryState.LOADED:
|
||||
raise InvalidDeviceAutomationConfig(
|
||||
f"Device {device.id} is not from an existing {DOMAIN} config entry"
|
||||
)
|
||||
|
||||
@@ -161,4 +161,25 @@ async def test_failure_scenarios(
|
||||
with pytest.raises(InvalidDeviceAutomationConfig):
|
||||
await device_trigger.async_validate_trigger_config(hass, config)
|
||||
|
||||
not_loaded_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data={}, unique_id="not-loaded-unique-id"
|
||||
)
|
||||
not_loaded_entry.add_to_hass(hass)
|
||||
|
||||
not_loaded_device = device_registry.async_get_or_create(
|
||||
config_entry_id=not_loaded_entry.entry_id,
|
||||
identifiers={(DOMAIN, "not-loaded-unique-id")},
|
||||
)
|
||||
|
||||
not_loaded_config = {
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"device_id": not_loaded_device.id,
|
||||
"type": "lg_netcast.turn_on",
|
||||
}
|
||||
|
||||
# Test that a device from a not-loaded lg_netcast config entry raises exception
|
||||
with pytest.raises(InvalidDeviceAutomationConfig, match="is not from an existing"):
|
||||
await device_trigger.async_validate_trigger_config(hass, not_loaded_config)
|
||||
|
||||
# Test that only valid triggers are attached
|
||||
|
||||
Reference in New Issue
Block a user