Support reloading the universal platform (#39248)

This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-08-26 00:25:15 +02:00
1 parent 63ebea1706
commit 90842fcb84
18 files changed
+300 -82

No files matched your search

@@ -1,10 +1,13 @@
"""The tests for the Universal Media player platform."""
import asyncio
from copy import copy
from os import path
import unittest
from asynctest.mock import patch
from voluptuous.error import MultipleInvalid
from homeassistant import config as hass_config
import homeassistant.components.input_number as input_number
import homeassistant.components.input_select as input_select
import homeassistant.components.media_player as media_player
@@ -812,3 +815,64 @@ async def test_master_state_with_template(hass):
await hass.async_block_till_done()
hass.states.get("media_player.tv").state == STATE_OFF
async def test_reload(hass):
"""Test the state_template option."""
hass.states.async_set("input_boolean.test", STATE_OFF)
hass.states.async_set("media_player.mock1", STATE_OFF)
templ = (
'{% if states.input_boolean.test.state == "off" %}on'
"{% else %}{{ states.media_player.mock1.state }}{% endif %}"
)
await async_setup_component(
hass,
"media_player",
{
"media_player": {
"platform": "universal",
"name": "tv",
"state_template": templ,
}
},
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 3
await hass.async_start()
await hass.async_block_till_done()
hass.states.get("media_player.tv").state == STATE_ON
hass.states.async_set("input_boolean.test", STATE_ON)
await hass.async_block_till_done()
hass.states.get("media_player.tv").state == STATE_OFF
hass.states.async_set("media_player.master_bedroom_2", STATE_OFF)
hass.states.async_set(
"remote.alexander_master_bedroom",
STATE_ON,
{"activity_list": ["act1", "act2"], "current_activity": "act2"},
)
yaml_path = path.join(
_get_fixtures_base_path(), "fixtures", "universal/configuration.yaml",
)
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
"universal", universal.SERVICE_RELOAD, {}, blocking=True,
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 5
assert hass.states.get("media_player.tv") is None
assert hass.states.get("media_player.master_bed_tv").state == "on"
assert hass.states.get("media_player.master_bed_tv").attributes["source"] == "act2"
def _get_fixtures_base_path():
return path.dirname(path.dirname(path.dirname(__file__)))
+14
View File
@@ -0,0 +1,14 @@
test_domain:
- platform: test_platform
sensors:
combined_sensor_energy_usage:
friendly_name: Combined Sense Energy Usage
unit_of_measurement: kW
value_template: '{{ ((states(''sensor.energy_usage'') | float) + (states(''sensor.energy_usage_2'')
| float)) / 1000 }}'
watching_tv_in_master_bedroom:
friendly_name: Watching TV in Master Bedroom
value_template: '{% if state_attr("remote.alexander_master_bedroom","current_activity")
== "Watch TV" or state_attr("remote.alexander_master_bedroom","current_activity")
== "Watch Apple TV" %}on{% else %}off{% endif %}'
+9
View File
@@ -0,0 +1,9 @@
media_player:
- platform: universal
name: Master Bed TV
children:
- media_player.master_bedroom_2
attributes:
state: remote.alexander_master_bedroom
source_list: remote.alexander_master_bedroom|activity_list
source: remote.alexander_master_bedroom|current_activity
+63
View File
@@ -0,0 +1,63 @@
"""Tests for the reload helper."""
import logging
from os import path
from homeassistant import config
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.reload import (
async_get_platform,
async_reload_integration_platforms,
)
from tests.async_mock import Mock, patch
from tests.common import (
MockModule,
MockPlatform,
mock_entity_platform,
mock_integration,
)
_LOGGER = logging.getLogger(__name__)
DOMAIN = "test_domain"
PLATFORM = "test_platform"
async def test_reload_platform(hass):
"""Test the polling of only updated entities."""
component_setup = Mock(return_value=True)
setup_called = []
async def setup_platform(*args):
setup_called.append(args)
mock_integration(hass, MockModule(DOMAIN, setup=component_setup))
mock_integration(hass, MockModule(PLATFORM, dependencies=[DOMAIN]))
mock_platform = MockPlatform(async_setup_platform=setup_platform)
mock_entity_platform(hass, f"{DOMAIN}.{PLATFORM}", mock_platform)
component = EntityComponent(_LOGGER, DOMAIN, hass)
await component.async_setup({DOMAIN: {"platform": PLATFORM, "sensors": None}})
await hass.async_block_till_done()
assert component_setup.called
assert f"{DOMAIN}.{PLATFORM}" in hass.config.components
assert len(setup_called) == 1
platform = async_get_platform(hass, PLATFORM, DOMAIN)
assert platform.platform_name == PLATFORM
assert platform.domain == DOMAIN
yaml_path = path.join(
_get_fixtures_base_path(), "fixtures", "helpers/reload_configuration.yaml",
)
with patch.object(config, "YAML_CONFIG_FILE", yaml_path):
await async_reload_integration_platforms(hass, PLATFORM, [DOMAIN])
assert len(setup_called) == 2
def _get_fixtures_base_path():
return path.dirname(path.dirname(__file__))