From 4d9ef9e795b4bf5999b0ad6880b8d75ede5c74f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 30 Oct 2018 17:38:09 +0200 Subject: [PATCH] Import homeassistant domain instead of hardcoding it (#17985) --- .../components/websocket_api/commands.py | 4 +- homeassistant/config.py | 2 +- homeassistant/helpers/state.py | 3 +- .../climate/test_generic_thermostat.py | 43 ++++++++++--------- tests/components/test_conversation.py | 17 ++++---- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 8e1dac4af8e..771a6a57f4f 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -2,7 +2,7 @@ import voluptuous as vol from homeassistant.const import MATCH_ALL, EVENT_TIME_CHANGED -from homeassistant.core import callback +from homeassistant.core import callback, DOMAIN as HASS_DOMAIN from homeassistant.helpers import config_validation as cv from homeassistant.helpers.service import async_get_all_descriptions @@ -134,7 +134,7 @@ async def handle_call_service(hass, connection, msg): Async friendly. """ blocking = True - if (msg['domain'] == 'homeassistant' and + if (msg['domain'] == HASS_DOMAIN and msg['service'] in ['restart', 'stop']): blocking = False await hass.services.async_call( diff --git a/homeassistant/config.py b/homeassistant/config.py index 8f0690a02c0..5f7107f95ae 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -445,7 +445,7 @@ def _format_config_error(ex: vol.Invalid, domain: str, config: Dict) -> str: getattr(domain_config, '__config_file__', '?'), getattr(domain_config, '__line__', '?')) - if domain != 'homeassistant': + if domain != CONF_CORE: message += ('Please check the docs at ' 'https://home-assistant.io/components/{}/'.format(domain)) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index fa728026eeb..61dc49ce2ec 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -44,14 +44,13 @@ from homeassistant.const import ( STATE_CLOSED, STATE_HOME, STATE_LOCKED, STATE_NOT_HOME, STATE_OFF, STATE_ON, STATE_OPEN, STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN, STATE_UNLOCKED, SERVICE_SELECT_OPTION) -from homeassistant.core import State +from homeassistant.core import State, DOMAIN as HASS_DOMAIN from homeassistant.util.async_ import run_coroutine_threadsafe from .typing import HomeAssistantType _LOGGER = logging.getLogger(__name__) GROUP_DOMAIN = 'group' -HASS_DOMAIN = 'homeassistant' # Update this dict of lists when new services are added to HA. # Each item is a service with a list of required attributes. diff --git a/tests/components/climate/test_generic_thermostat.py b/tests/components/climate/test_generic_thermostat.py index 71654afd5b8..f87a5371773 100644 --- a/tests/components/climate/test_generic_thermostat.py +++ b/tests/components/climate/test_generic_thermostat.py @@ -6,7 +6,8 @@ from unittest import mock import pytz import homeassistant.core as ha -from homeassistant.core import callback, CoreState, State +from homeassistant.core import ( + callback, DOMAIN as HASS_DOMAIN, CoreState, State) from homeassistant.setup import setup_component, async_setup_component from homeassistant.const import ( SERVICE_TURN_OFF, @@ -257,7 +258,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -270,7 +271,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self.hass.block_till_done() assert 2 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -292,7 +293,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -314,7 +315,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -327,7 +328,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -363,7 +364,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -416,7 +417,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self.hass.block_till_done() assert 2 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -445,7 +446,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -458,7 +459,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -480,7 +481,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -502,7 +503,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -515,7 +516,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -592,7 +593,7 @@ class TestClimateGenericThermostatACModeMinCycle(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -618,7 +619,7 @@ class TestClimateGenericThermostatACModeMinCycle(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -692,7 +693,7 @@ class TestClimateGenericThermostatMinCycle(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -709,7 +710,7 @@ class TestClimateGenericThermostatMinCycle(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -773,7 +774,7 @@ class TestClimateGenericThermostatACKeepAlive(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -796,7 +797,7 @@ class TestClimateGenericThermostatACKeepAlive(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -863,7 +864,7 @@ class TestClimateGenericThermostatKeepAlive(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_ON == call.service assert ENT_SWITCH == call.data['entity_id'] @@ -886,7 +887,7 @@ class TestClimateGenericThermostatKeepAlive(unittest.TestCase): self.hass.block_till_done() assert 1 == len(self.calls) call = self.calls[0] - assert 'homeassistant' == call.domain + assert HASS_DOMAIN == call.domain assert SERVICE_TURN_OFF == call.service assert ENT_SWITCH == call.data['entity_id'] diff --git a/tests/components/test_conversation.py b/tests/components/test_conversation.py index 61247b5bdde..7934e016281 100644 --- a/tests/components/test_conversation.py +++ b/tests/components/test_conversation.py @@ -2,6 +2,7 @@ # pylint: disable=protected-access import pytest +from homeassistant.core import DOMAIN as HASS_DOMAIN from homeassistant.setup import async_setup_component from homeassistant.components import conversation import homeassistant.components as component @@ -152,7 +153,7 @@ async def test_turn_on_intent(hass, sentence): assert result hass.states.async_set('light.kitchen', 'off') - calls = async_mock_service(hass, 'homeassistant', 'turn_on') + calls = async_mock_service(hass, HASS_DOMAIN, 'turn_on') await hass.services.async_call( 'conversation', 'process', { @@ -162,7 +163,7 @@ async def test_turn_on_intent(hass, sentence): assert len(calls) == 1 call = calls[0] - assert call.domain == 'homeassistant' + assert call.domain == HASS_DOMAIN assert call.service == 'turn_on' assert call.data == {'entity_id': 'light.kitchen'} @@ -203,7 +204,7 @@ async def test_turn_off_intent(hass, sentence): assert result hass.states.async_set('light.kitchen', 'on') - calls = async_mock_service(hass, 'homeassistant', 'turn_off') + calls = async_mock_service(hass, HASS_DOMAIN, 'turn_off') await hass.services.async_call( 'conversation', 'process', { @@ -213,7 +214,7 @@ async def test_turn_off_intent(hass, sentence): assert len(calls) == 1 call = calls[0] - assert call.domain == 'homeassistant' + assert call.domain == HASS_DOMAIN assert call.service == 'turn_off' assert call.data == {'entity_id': 'light.kitchen'} @@ -228,7 +229,7 @@ async def test_toggle_intent(hass, sentence): assert result hass.states.async_set('light.kitchen', 'on') - calls = async_mock_service(hass, 'homeassistant', 'toggle') + calls = async_mock_service(hass, HASS_DOMAIN, 'toggle') await hass.services.async_call( 'conversation', 'process', { @@ -238,7 +239,7 @@ async def test_toggle_intent(hass, sentence): assert len(calls) == 1 call = calls[0] - assert call.domain == 'homeassistant' + assert call.domain == HASS_DOMAIN assert call.service == 'toggle' assert call.data == {'entity_id': 'light.kitchen'} @@ -253,7 +254,7 @@ async def test_http_api(hass, aiohttp_client): client = await aiohttp_client(hass.http.app) hass.states.async_set('light.kitchen', 'off') - calls = async_mock_service(hass, 'homeassistant', 'turn_on') + calls = async_mock_service(hass, HASS_DOMAIN, 'turn_on') resp = await client.post('/api/conversation/process', json={ 'text': 'Turn the kitchen on' @@ -262,7 +263,7 @@ async def test_http_api(hass, aiohttp_client): assert len(calls) == 1 call = calls[0] - assert call.domain == 'homeassistant' + assert call.domain == HASS_DOMAIN assert call.service == 'turn_on' assert call.data == {'entity_id': 'light.kitchen'}