* Add webOS Smart TV config flow support (#53256) * Add Webostv config flow * Fix tests mocks and apply review comments * Apply review comments * Change config flow to use ssdp UDN as unique_id * Fix device info * More review comments * Fix _async_check_configured_entry * Remove turn on script * Add webOS Smart TV device triggers (#53752) * Add webOS Smart TV config flow support (#53256) * Add Webostv config flow * Fix tests mocks and apply review comments * Apply review comments * Change config flow to use ssdp UDN as unique_id * Fix device info * More review comments * Fix _async_check_configured_entry * Remove turn on script * Add webOS Smart TV device triggers (#53752) * Fix webOS Smart TV mypy and pylint errors (#62620) * Change webOS Smart TV PyPi aiopylgtv package to bscpylgtv (#62633) * Change webOS Smart TV PyPi aiopylgtv package to bscpylgtv * Update bscpylgtv to 0.2.8 (revised websockets requirment) * Change webOS Smart TV PyPi package to aiowebostv (#63759) * Change webOS Smart TV PyPi package to aiowebostv * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * webOS TV check UUID for user added device (#63817) * webOS TV check uuid when for user added device * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Add test for form abort and host update Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Rework webOS Smart TV device trigger to custom trigger platform (#63950) * Rework webOS Smart TV device trigger to custom trigger platform * Review comments and add tests * Fix webOS TV import from YAML (#63996) * Fix webOS TV import from YAML * Fix requirements * Migrate YAML entities unique id to UUID * Add backoff to migration task delay * Assert result data and unique_id * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Add codeowner Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
174 lines
5.5 KiB
Python
174 lines
5.5 KiB
Python
"""The tests for WebOS TV device triggers."""
|
|
import pytest
|
|
|
|
from homeassistant.components import automation
|
|
from homeassistant.components.device_automation.exceptions import (
|
|
InvalidDeviceAutomationConfig,
|
|
)
|
|
from homeassistant.components.webostv import DOMAIN, device_trigger
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers.device_registry import async_get as get_dev_reg
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from . import ENTITY_ID, setup_webostv
|
|
|
|
from tests.common import MockConfigEntry, async_get_device_automations
|
|
|
|
|
|
async def test_get_triggers(hass, client):
|
|
"""Test we get the expected triggers."""
|
|
await setup_webostv(hass, "fake-uuid")
|
|
|
|
device_reg = get_dev_reg(hass)
|
|
device = device_reg.async_get_device(identifiers={(DOMAIN, "fake-uuid")})
|
|
|
|
turn_on_trigger = {
|
|
"platform": "device",
|
|
"domain": DOMAIN,
|
|
"type": "webostv.turn_on",
|
|
"device_id": device.id,
|
|
}
|
|
|
|
triggers = await async_get_device_automations(hass, "trigger", device.id)
|
|
assert turn_on_trigger in triggers
|
|
|
|
|
|
async def test_if_fires_on_turn_on_request(hass, calls, client):
|
|
"""Test for turn_on and turn_off triggers firing."""
|
|
await setup_webostv(hass, "fake-uuid")
|
|
|
|
device_reg = get_dev_reg(hass)
|
|
device = device_reg.async_get_device(identifiers={(DOMAIN, "fake-uuid")})
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: [
|
|
{
|
|
"trigger": {
|
|
"platform": "device",
|
|
"domain": DOMAIN,
|
|
"device_id": device.id,
|
|
"type": "webostv.turn_on",
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": "{{ trigger.device_id }}",
|
|
"id": "{{ trigger.id }}",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"trigger": {
|
|
"platform": "webostv.turn_on",
|
|
"entity_id": ENTITY_ID,
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": ENTITY_ID,
|
|
"id": "{{ trigger.id }}",
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
)
|
|
|
|
await hass.services.async_call(
|
|
"media_player",
|
|
"turn_on",
|
|
{"entity_id": ENTITY_ID},
|
|
blocking=True,
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
assert len(calls) == 2
|
|
assert calls[0].data["some"] == device.id
|
|
assert calls[0].data["id"] == 0
|
|
assert calls[1].data["some"] == ENTITY_ID
|
|
assert calls[1].data["id"] == 0
|
|
|
|
|
|
async def test_get_triggers_for_invalid_device_id(hass, caplog):
|
|
"""Test error raised for invalid shelly device_id."""
|
|
await async_setup_component(hass, "persistent_notification", {})
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: [
|
|
{
|
|
"trigger": {
|
|
"platform": "device",
|
|
"domain": DOMAIN,
|
|
"device_id": "invalid_device_id",
|
|
"type": "webostv.turn_on",
|
|
},
|
|
"action": {
|
|
"service": "test.automation",
|
|
"data_template": {
|
|
"some": "{{ trigger.invalid_device }}",
|
|
"id": "{{ trigger.id }}",
|
|
},
|
|
},
|
|
}
|
|
]
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert (
|
|
"Invalid config for [automation]: Device invalid_device_id is not a valid webostv device"
|
|
in caplog.text
|
|
)
|
|
|
|
|
|
async def test_failure_scenarios(hass, client):
|
|
"""Test failure scenarios."""
|
|
await setup_webostv(hass, "fake-uuid")
|
|
|
|
# Test wrong trigger platform type
|
|
with pytest.raises(HomeAssistantError):
|
|
await device_trigger.async_attach_trigger(
|
|
hass, {"type": "wrong.type", "device_id": "invalid_device_id"}, None, {}
|
|
)
|
|
|
|
# Test invalid device id
|
|
with pytest.raises(InvalidDeviceAutomationConfig):
|
|
await device_trigger.async_validate_trigger_config(
|
|
hass,
|
|
{
|
|
"platform": "device",
|
|
"domain": DOMAIN,
|
|
"type": "webostv.turn_on",
|
|
"device_id": "invalid_device_id",
|
|
},
|
|
)
|
|
|
|
entry = MockConfigEntry(domain="fake", state=ConfigEntryState.LOADED, data={})
|
|
entry.add_to_hass(hass)
|
|
device_reg = get_dev_reg(hass)
|
|
|
|
device = device_reg.async_get_or_create(
|
|
config_entry_id=entry.entry_id, identifiers={("fake", "fake")}
|
|
)
|
|
|
|
config = {
|
|
"platform": "device",
|
|
"domain": DOMAIN,
|
|
"device_id": device.id,
|
|
"type": "webostv.turn_on",
|
|
}
|
|
|
|
# Test that device id from non webostv domain raises exception
|
|
with pytest.raises(InvalidDeviceAutomationConfig):
|
|
await device_trigger.async_validate_trigger_config(hass, config)
|
|
|
|
# Test no exception if device is not loaded
|
|
await hass.config_entries.async_unload(entry.entry_id)
|
|
assert await device_trigger.async_validate_trigger_config(hass, config) == config
|