Black
This commit is contained in:
parent
da05dfe708
commit
4de97abc3a
2676 changed files with 163166 additions and 140084 deletions
|
@ -8,21 +8,35 @@ import yaml
|
|||
import homeassistant.core as ha
|
||||
from homeassistant import config
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, SERVICE_HOMEASSISTANT_RESTART,
|
||||
SERVICE_HOMEASSISTANT_STOP, SERVICE_TURN_ON, SERVICE_TURN_OFF,
|
||||
SERVICE_TOGGLE, EVENT_CORE_CONFIG_UPDATE)
|
||||
ATTR_ENTITY_ID,
|
||||
STATE_ON,
|
||||
STATE_OFF,
|
||||
SERVICE_HOMEASSISTANT_RESTART,
|
||||
SERVICE_HOMEASSISTANT_STOP,
|
||||
SERVICE_TURN_ON,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TOGGLE,
|
||||
EVENT_CORE_CONFIG_UPDATE,
|
||||
)
|
||||
import homeassistant.components as comps
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.homeassistant import (
|
||||
SERVICE_CHECK_CONFIG, SERVICE_RELOAD_CORE_CONFIG)
|
||||
SERVICE_CHECK_CONFIG,
|
||||
SERVICE_RELOAD_CORE_CONFIG,
|
||||
)
|
||||
import homeassistant.helpers.intent as intent
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_service, patch_yaml_files, mock_coro,
|
||||
async_mock_service, async_capture_events)
|
||||
get_test_home_assistant,
|
||||
mock_service,
|
||||
patch_yaml_files,
|
||||
mock_coro,
|
||||
async_mock_service,
|
||||
async_capture_events,
|
||||
)
|
||||
|
||||
|
||||
def turn_on(hass, entity_id=None, **service_data):
|
||||
|
@ -98,12 +112,11 @@ class TestComponentsCore(unittest.TestCase):
|
|||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
assert run_coroutine_threadsafe(
|
||||
async_setup_component(self.hass, 'homeassistant', {}),
|
||||
self.hass.loop
|
||||
async_setup_component(self.hass, "homeassistant", {}), self.hass.loop
|
||||
).result()
|
||||
|
||||
self.hass.states.set('light.Bowl', STATE_ON)
|
||||
self.hass.states.set('light.Ceiling', STATE_OFF)
|
||||
self.hass.states.set("light.Bowl", STATE_ON)
|
||||
self.hass.states.set("light.Ceiling", STATE_OFF)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def tearDown(self):
|
||||
|
@ -112,65 +125,63 @@ class TestComponentsCore(unittest.TestCase):
|
|||
|
||||
def test_is_on(self):
|
||||
"""Test is_on method."""
|
||||
assert comps.is_on(self.hass, 'light.Bowl')
|
||||
assert not comps.is_on(self.hass, 'light.Ceiling')
|
||||
assert comps.is_on(self.hass, "light.Bowl")
|
||||
assert not comps.is_on(self.hass, "light.Ceiling")
|
||||
assert comps.is_on(self.hass)
|
||||
assert not comps.is_on(self.hass, 'non_existing.entity')
|
||||
assert not comps.is_on(self.hass, "non_existing.entity")
|
||||
|
||||
def test_turn_on_without_entities(self):
|
||||
"""Test turn_on method without entities."""
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)
|
||||
calls = mock_service(self.hass, "light", SERVICE_TURN_ON)
|
||||
turn_on(self.hass)
|
||||
self.hass.block_till_done()
|
||||
assert 0 == len(calls)
|
||||
|
||||
def test_turn_on(self):
|
||||
"""Test turn_on method."""
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)
|
||||
turn_on(self.hass, 'light.Ceiling')
|
||||
calls = mock_service(self.hass, "light", SERVICE_TURN_ON)
|
||||
turn_on(self.hass, "light.Ceiling")
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_turn_off(self):
|
||||
"""Test turn_off method."""
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TURN_OFF)
|
||||
turn_off(self.hass, 'light.Bowl')
|
||||
calls = mock_service(self.hass, "light", SERVICE_TURN_OFF)
|
||||
turn_off(self.hass, "light.Bowl")
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_toggle(self):
|
||||
"""Test toggle method."""
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TOGGLE)
|
||||
toggle(self.hass, 'light.Bowl')
|
||||
calls = mock_service(self.hass, "light", SERVICE_TOGGLE)
|
||||
toggle(self.hass, "light.Bowl")
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
@patch('homeassistant.config.os.path.isfile', Mock(return_value=True))
|
||||
@patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
|
||||
def test_reload_core_conf(self):
|
||||
"""Test reload core conf service."""
|
||||
ent = entity.Entity()
|
||||
ent.entity_id = 'test.entity'
|
||||
ent.entity_id = "test.entity"
|
||||
ent.hass = self.hass
|
||||
ent.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('test.entity')
|
||||
state = self.hass.states.get("test.entity")
|
||||
assert state is not None
|
||||
assert state.state == 'unknown'
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes == {}
|
||||
|
||||
files = {
|
||||
config.YAML_CONFIG_FILE: yaml.dump({
|
||||
ha.DOMAIN: {
|
||||
'latitude': 10,
|
||||
'longitude': 20,
|
||||
'customize': {
|
||||
'test.Entity': {
|
||||
'hello': 'world'
|
||||
}
|
||||
config.YAML_CONFIG_FILE: yaml.dump(
|
||||
{
|
||||
ha.DOMAIN: {
|
||||
"latitude": 10,
|
||||
"longitude": 20,
|
||||
"customize": {"test.Entity": {"hello": "world"}},
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
with patch_yaml_files(files, True):
|
||||
reload_core_config(self.hass)
|
||||
|
@ -182,19 +193,17 @@ class TestComponentsCore(unittest.TestCase):
|
|||
ent.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('test.entity')
|
||||
state = self.hass.states.get("test.entity")
|
||||
assert state is not None
|
||||
assert state.state == 'unknown'
|
||||
assert state.attributes.get('hello') == 'world'
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("hello") == "world"
|
||||
|
||||
@patch('homeassistant.config.os.path.isfile', Mock(return_value=True))
|
||||
@patch('homeassistant.components.homeassistant._LOGGER.error')
|
||||
@patch('homeassistant.config.async_process_ha_core_config')
|
||||
@patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
|
||||
@patch("homeassistant.components.homeassistant._LOGGER.error")
|
||||
@patch("homeassistant.config.async_process_ha_core_config")
|
||||
def test_reload_core_with_wrong_conf(self, mock_process, mock_error):
|
||||
"""Test reload core conf service."""
|
||||
files = {
|
||||
config.YAML_CONFIG_FILE: yaml.dump(['invalid', 'config'])
|
||||
}
|
||||
files = {config.YAML_CONFIG_FILE: yaml.dump(["invalid", "config"])}
|
||||
with patch_yaml_files(files, True):
|
||||
reload_core_config(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
@ -202,18 +211,15 @@ class TestComponentsCore(unittest.TestCase):
|
|||
assert mock_error.called
|
||||
assert mock_process.called is False
|
||||
|
||||
@patch('homeassistant.core.HomeAssistant.async_stop',
|
||||
return_value=mock_coro())
|
||||
@patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro())
|
||||
def test_stop_homeassistant(self, mock_stop):
|
||||
"""Test stop service."""
|
||||
stop(self.hass)
|
||||
self.hass.block_till_done()
|
||||
assert mock_stop.called
|
||||
|
||||
@patch('homeassistant.core.HomeAssistant.async_stop',
|
||||
return_value=mock_coro())
|
||||
@patch('homeassistant.config.async_check_ha_config_file',
|
||||
return_value=mock_coro())
|
||||
@patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro())
|
||||
@patch("homeassistant.config.async_check_ha_config_file", return_value=mock_coro())
|
||||
def test_restart_homeassistant(self, mock_check, mock_restart):
|
||||
"""Test stop service."""
|
||||
restart(self.hass)
|
||||
|
@ -221,10 +227,11 @@ class TestComponentsCore(unittest.TestCase):
|
|||
assert mock_restart.called
|
||||
assert mock_check.called
|
||||
|
||||
@patch('homeassistant.core.HomeAssistant.async_stop',
|
||||
return_value=mock_coro())
|
||||
@patch('homeassistant.config.async_check_ha_config_file',
|
||||
side_effect=HomeAssistantError("Test error"))
|
||||
@patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro())
|
||||
@patch(
|
||||
"homeassistant.config.async_check_ha_config_file",
|
||||
side_effect=HomeAssistantError("Test error"),
|
||||
)
|
||||
def test_restart_homeassistant_wrong_conf(self, mock_check, mock_restart):
|
||||
"""Test stop service."""
|
||||
restart(self.hass)
|
||||
|
@ -232,10 +239,8 @@ class TestComponentsCore(unittest.TestCase):
|
|||
assert mock_check.called
|
||||
assert not mock_restart.called
|
||||
|
||||
@patch('homeassistant.core.HomeAssistant.async_stop',
|
||||
return_value=mock_coro())
|
||||
@patch('homeassistant.config.async_check_ha_config_file',
|
||||
return_value=mock_coro())
|
||||
@patch("homeassistant.core.HomeAssistant.async_stop", return_value=mock_coro())
|
||||
@patch("homeassistant.config.async_check_ha_config_file", return_value=mock_coro())
|
||||
def test_check_config(self, mock_check, mock_stop):
|
||||
"""Test stop service."""
|
||||
check_config(self.hass)
|
||||
|
@ -246,65 +251,65 @@ class TestComponentsCore(unittest.TestCase):
|
|||
|
||||
async def test_turn_on_intent(hass):
|
||||
"""Test HassTurnOn intent."""
|
||||
result = await async_setup_component(hass, 'homeassistant', {})
|
||||
result = await async_setup_component(hass, "homeassistant", {})
|
||||
assert result
|
||||
|
||||
hass.states.async_set('light.test_light', 'off')
|
||||
calls = async_mock_service(hass, 'light', SERVICE_TURN_ON)
|
||||
hass.states.async_set("light.test_light", "off")
|
||||
calls = async_mock_service(hass, "light", SERVICE_TURN_ON)
|
||||
|
||||
response = await intent.async_handle(
|
||||
hass, 'test', 'HassTurnOn', {'name': {'value': 'test light'}}
|
||||
hass, "test", "HassTurnOn", {"name": {"value": "test light"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert response.speech['plain']['speech'] == 'Turned test light on'
|
||||
assert response.speech["plain"]["speech"] == "Turned test light on"
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == 'light'
|
||||
assert call.service == 'turn_on'
|
||||
assert call.data == {'entity_id': ['light.test_light']}
|
||||
assert call.domain == "light"
|
||||
assert call.service == "turn_on"
|
||||
assert call.data == {"entity_id": ["light.test_light"]}
|
||||
|
||||
|
||||
async def test_turn_off_intent(hass):
|
||||
"""Test HassTurnOff intent."""
|
||||
result = await async_setup_component(hass, 'homeassistant', {})
|
||||
result = await async_setup_component(hass, "homeassistant", {})
|
||||
assert result
|
||||
|
||||
hass.states.async_set('light.test_light', 'on')
|
||||
calls = async_mock_service(hass, 'light', SERVICE_TURN_OFF)
|
||||
hass.states.async_set("light.test_light", "on")
|
||||
calls = async_mock_service(hass, "light", SERVICE_TURN_OFF)
|
||||
|
||||
response = await intent.async_handle(
|
||||
hass, 'test', 'HassTurnOff', {'name': {'value': 'test light'}}
|
||||
hass, "test", "HassTurnOff", {"name": {"value": "test light"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert response.speech['plain']['speech'] == 'Turned test light off'
|
||||
assert response.speech["plain"]["speech"] == "Turned test light off"
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == 'light'
|
||||
assert call.service == 'turn_off'
|
||||
assert call.data == {'entity_id': ['light.test_light']}
|
||||
assert call.domain == "light"
|
||||
assert call.service == "turn_off"
|
||||
assert call.data == {"entity_id": ["light.test_light"]}
|
||||
|
||||
|
||||
async def test_toggle_intent(hass):
|
||||
"""Test HassToggle intent."""
|
||||
result = await async_setup_component(hass, 'homeassistant', {})
|
||||
result = await async_setup_component(hass, "homeassistant", {})
|
||||
assert result
|
||||
|
||||
hass.states.async_set('light.test_light', 'off')
|
||||
calls = async_mock_service(hass, 'light', SERVICE_TOGGLE)
|
||||
hass.states.async_set("light.test_light", "off")
|
||||
calls = async_mock_service(hass, "light", SERVICE_TOGGLE)
|
||||
|
||||
response = await intent.async_handle(
|
||||
hass, 'test', 'HassToggle', {'name': {'value': 'test light'}}
|
||||
hass, "test", "HassToggle", {"name": {"value": "test light"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert response.speech['plain']['speech'] == 'Toggled test light'
|
||||
assert response.speech["plain"]["speech"] == "Toggled test light"
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == 'light'
|
||||
assert call.service == 'toggle'
|
||||
assert call.data == {'entity_id': ['light.test_light']}
|
||||
assert call.domain == "light"
|
||||
assert call.service == "toggle"
|
||||
assert call.data == {"entity_id": ["light.test_light"]}
|
||||
|
||||
|
||||
async def test_turn_on_multiple_intent(hass):
|
||||
|
@ -312,78 +317,97 @@ async def test_turn_on_multiple_intent(hass):
|
|||
|
||||
This tests that matching finds the proper entity among similar names.
|
||||
"""
|
||||
result = await async_setup_component(hass, 'homeassistant', {})
|
||||
result = await async_setup_component(hass, "homeassistant", {})
|
||||
assert result
|
||||
|
||||
hass.states.async_set('light.test_light', 'off')
|
||||
hass.states.async_set('light.test_lights_2', 'off')
|
||||
hass.states.async_set('light.test_lighter', 'off')
|
||||
calls = async_mock_service(hass, 'light', SERVICE_TURN_ON)
|
||||
hass.states.async_set("light.test_light", "off")
|
||||
hass.states.async_set("light.test_lights_2", "off")
|
||||
hass.states.async_set("light.test_lighter", "off")
|
||||
calls = async_mock_service(hass, "light", SERVICE_TURN_ON)
|
||||
|
||||
response = await intent.async_handle(
|
||||
hass, 'test', 'HassTurnOn', {'name': {'value': 'test lights'}}
|
||||
hass, "test", "HassTurnOn", {"name": {"value": "test lights"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert response.speech['plain']['speech'] == 'Turned test lights 2 on'
|
||||
assert response.speech["plain"]["speech"] == "Turned test lights 2 on"
|
||||
assert len(calls) == 1
|
||||
call = calls[0]
|
||||
assert call.domain == 'light'
|
||||
assert call.service == 'turn_on'
|
||||
assert call.data == {'entity_id': ['light.test_lights_2']}
|
||||
assert call.domain == "light"
|
||||
assert call.service == "turn_on"
|
||||
assert call.data == {"entity_id": ["light.test_lights_2"]}
|
||||
|
||||
|
||||
async def test_turn_on_to_not_block_for_domains_without_service(hass):
|
||||
"""Test if turn_on is blocking domain with no service."""
|
||||
await async_setup_component(hass, 'homeassistant', {})
|
||||
async_mock_service(hass, 'light', SERVICE_TURN_ON)
|
||||
hass.states.async_set('light.Bowl', STATE_ON)
|
||||
hass.states.async_set('light.Ceiling', STATE_OFF)
|
||||
await async_setup_component(hass, "homeassistant", {})
|
||||
async_mock_service(hass, "light", SERVICE_TURN_ON)
|
||||
hass.states.async_set("light.Bowl", STATE_ON)
|
||||
hass.states.async_set("light.Ceiling", STATE_OFF)
|
||||
|
||||
# We can't test if our service call results in services being called
|
||||
# because by mocking out the call service method, we mock out all
|
||||
# So we mimic how the service registry calls services
|
||||
service_call = ha.ServiceCall('homeassistant', 'turn_on', {
|
||||
'entity_id': ['light.test', 'sensor.bla', 'light.bla']
|
||||
})
|
||||
service = hass.services._services['homeassistant']['turn_on']
|
||||
service_call = ha.ServiceCall(
|
||||
"homeassistant",
|
||||
"turn_on",
|
||||
{"entity_id": ["light.test", "sensor.bla", "light.bla"]},
|
||||
)
|
||||
service = hass.services._services["homeassistant"]["turn_on"]
|
||||
|
||||
with patch('homeassistant.core.ServiceRegistry.async_call',
|
||||
side_effect=lambda *args: mock_coro()) as mock_call:
|
||||
with patch(
|
||||
"homeassistant.core.ServiceRegistry.async_call",
|
||||
side_effect=lambda *args: mock_coro(),
|
||||
) as mock_call:
|
||||
await service.func(service_call)
|
||||
|
||||
assert mock_call.call_count == 2
|
||||
assert mock_call.call_args_list[0][0] == (
|
||||
'light', 'turn_on', {'entity_id': ['light.bla', 'light.test']}, True)
|
||||
"light",
|
||||
"turn_on",
|
||||
{"entity_id": ["light.bla", "light.test"]},
|
||||
True,
|
||||
)
|
||||
assert mock_call.call_args_list[1][0] == (
|
||||
'sensor', 'turn_on', {'entity_id': ['sensor.bla']}, False)
|
||||
"sensor",
|
||||
"turn_on",
|
||||
{"entity_id": ["sensor.bla"]},
|
||||
False,
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_update(hass):
|
||||
"""Test being able to call entity update."""
|
||||
await async_setup_component(hass, 'homeassistant', {})
|
||||
await async_setup_component(hass, "homeassistant", {})
|
||||
|
||||
with patch('homeassistant.helpers.entity_component.async_update_entity',
|
||||
return_value=mock_coro()) as mock_update:
|
||||
await hass.services.async_call('homeassistant', 'update_entity', {
|
||||
'entity_id': ['light.kitchen']
|
||||
}, blocking=True)
|
||||
with patch(
|
||||
"homeassistant.helpers.entity_component.async_update_entity",
|
||||
return_value=mock_coro(),
|
||||
) as mock_update:
|
||||
await hass.services.async_call(
|
||||
"homeassistant",
|
||||
"update_entity",
|
||||
{"entity_id": ["light.kitchen"]},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(mock_update.mock_calls) == 1
|
||||
assert mock_update.mock_calls[0][1][1] == 'light.kitchen'
|
||||
assert mock_update.mock_calls[0][1][1] == "light.kitchen"
|
||||
|
||||
|
||||
async def test_setting_location(hass):
|
||||
"""Test setting the location."""
|
||||
await async_setup_component(hass, 'homeassistant', {})
|
||||
await async_setup_component(hass, "homeassistant", {})
|
||||
events = async_capture_events(hass, EVENT_CORE_CONFIG_UPDATE)
|
||||
# Just to make sure that we are updating values.
|
||||
assert hass.config.latitude != 30
|
||||
assert hass.config.longitude != 40
|
||||
await hass.services.async_call('homeassistant', 'set_location', {
|
||||
'latitude': 30,
|
||||
'longitude': 40,
|
||||
}, blocking=True)
|
||||
await hass.services.async_call(
|
||||
"homeassistant",
|
||||
"set_location",
|
||||
{"latitude": 30, "longitude": 40},
|
||||
blocking=True,
|
||||
)
|
||||
assert len(events) == 1
|
||||
assert hass.config.latitude == 30
|
||||
assert hass.config.longitude == 40
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue