Stephen Alderman f62fb76765
Add Config Flow to LG Netcast (#104913)
* Add Config Flow to lg_netcast

* Add YAML import to Lg Netcast ConfigFlow

Deprecates YAML config support

* Add LG Netcast Device triggers for turn_on action

* Add myself to LG Netcast codeowners

* Remove unnecessary user_input validation check.

* Move netcast discovery logic to the backend

* Use FlowResultType Enum for tests

* Mock pylgnetcast.query_device_info instead of _send_to_tv

* Refactor lg_netcast client discovery, simplify YAML import

* Simplify CONF_NAME to use friendly name

Fix: Use Friendly name for Name

* Expose model to DeviceInfo

* Add test for testing YAML import when not TV not online

* Switch to entity_name for LGTVDevice

* Add data_description to host field in user step

* Wrap try only around _get_session_id

* Send regular request for access_token to ensure it display on the TV

* Stop displaying access token when flow is aborted

* Remove config_flow only consts and minor fixups

* Simplify media_player logic & raise new migration issue

* Add async_unload_entry

* Create issues when import config flow fails, and raise only a single yaml deprecation issue type

* Remove single use trigger helpers

* Bump issue deprecation breakage version

* Lint

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-04-16 09:29:02 +02:00

89 lines
2.7 KiB
Python

"""Provides device triggers for LG Netcast."""
from __future__ import annotations
from typing import Any
import voluptuous as vol
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.device_automation.exceptions import (
InvalidDeviceAutomationConfig,
)
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.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,
)
TRIGGER_TYPES = {TURN_ON_PLATFORM_TYPE}
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
{
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
}
)
async def async_validate_trigger_config(
hass: HomeAssistant, config: ConfigType
) -> ConfigType:
"""Validate config."""
config = TRIGGER_SCHEMA(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 DOMAIN in hass.data:
for config_entry_id in device.config_entries:
if hass.data[DOMAIN].get(config_entry_id):
break
else:
raise InvalidDeviceAutomationConfig(
f"Device {device.id} is not from an existing {DOMAIN} config entry"
)
return config
async def async_get_triggers(
_hass: HomeAssistant, device_id: str
) -> list[dict[str, Any]]:
"""List device triggers for LG Netcast devices."""
return [async_get_turn_on_trigger(device_id)]
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Attach a trigger."""
if (trigger_type := config[CONF_TYPE]) == TURN_ON_PLATFORM_TYPE:
trigger_config = {
CONF_PLATFORM: trigger_type,
CONF_DEVICE_ID: config[CONF_DEVICE_ID],
}
trigger_config = await trigger.async_validate_trigger_config(
hass, trigger_config
)
return await trigger.async_attach_trigger(
hass, trigger_config, action, trigger_info
)
raise HomeAssistantError(f"Unhandled trigger type {trigger_type}")