ps - add reload core config service (#2350)
This commit is contained in:
1 parent
9ce9b8debb
commit
a70f922a71
6 files changed
+156
-54
No files matched your search
@@ -2,13 +2,18 @@
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import yaml
|
||||
|
||||
import homeassistant.core as ha
|
||||
from homeassistant import config
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE)
|
||||
import homeassistant.components as comps
|
||||
from homeassistant.helpers import entity
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, mock_service
|
||||
|
||||
|
||||
class TestComponentsCore(unittest.TestCase):
|
||||
@@ -31,47 +36,40 @@ class TestComponentsCore(unittest.TestCase):
|
||||
self.assertTrue(comps.is_on(self.hass, 'light.Bowl'))
|
||||
self.assertFalse(comps.is_on(self.hass, 'light.Ceiling'))
|
||||
self.assertTrue(comps.is_on(self.hass))
|
||||
self.assertFalse(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)
|
||||
comps.turn_on(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(0, len(calls))
|
||||
|
||||
def test_turn_on(self):
|
||||
"""Test turn_on method."""
|
||||
runs = []
|
||||
self.hass.services.register(
|
||||
'light', SERVICE_TURN_ON, lambda x: runs.append(1))
|
||||
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)
|
||||
comps.turn_on(self.hass, 'light.Ceiling')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
def test_turn_off(self):
|
||||
"""Test turn_off method."""
|
||||
runs = []
|
||||
self.hass.services.register(
|
||||
'light', SERVICE_TURN_OFF, lambda x: runs.append(1))
|
||||
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TURN_OFF)
|
||||
comps.turn_off(self.hass, 'light.Bowl')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
def test_toggle(self):
|
||||
"""Test toggle method."""
|
||||
runs = []
|
||||
self.hass.services.register(
|
||||
'light', SERVICE_TOGGLE, lambda x: runs.append(1))
|
||||
|
||||
calls = mock_service(self.hass, 'light', SERVICE_TOGGLE)
|
||||
comps.toggle(self.hass, 'light.Bowl')
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(runs))
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
@patch('homeassistant.core.ServiceRegistry.call')
|
||||
def test_turn_on_to_not_block_for_domains_without_service(self, mock_call):
|
||||
"""Test if turn_on is blocking domain with no service."""
|
||||
self.hass.services.register('light', SERVICE_TURN_ON, lambda x: x)
|
||||
mock_service(self.hass, 'light', SERVICE_TURN_ON)
|
||||
|
||||
# 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
|
||||
@@ -89,3 +87,62 @@ class TestComponentsCore(unittest.TestCase):
|
||||
self.assertEqual(
|
||||
('sensor', 'turn_on', {'entity_id': ['sensor.bla']}, False),
|
||||
mock_call.call_args_list[1][0])
|
||||
|
||||
def test_reload_core_conf(self):
|
||||
"""Test reload core conf service."""
|
||||
ent = entity.Entity()
|
||||
ent.entity_id = 'test.entity'
|
||||
ent.hass = self.hass
|
||||
ent.update_ha_state()
|
||||
|
||||
state = self.hass.states.get('test.entity')
|
||||
assert state is not None
|
||||
assert state.state == 'unknown'
|
||||
assert state.attributes == {}
|
||||
|
||||
with TemporaryDirectory() as conf_dir:
|
||||
self.hass.config.config_dir = conf_dir
|
||||
conf_yaml = self.hass.config.path(config.YAML_CONFIG_FILE)
|
||||
|
||||
with open(conf_yaml, 'a') as fp:
|
||||
fp.write(yaml.dump({
|
||||
ha.DOMAIN: {
|
||||
'latitude': 10,
|
||||
'longitude': 20,
|
||||
'customize': {
|
||||
'test.Entity': {
|
||||
'hello': 'world'
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
comps.reload_core_config(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert 10 == self.hass.config.latitude
|
||||
assert 20 == self.hass.config.longitude
|
||||
|
||||
ent.update_ha_state()
|
||||
|
||||
state = self.hass.states.get('test.entity')
|
||||
assert state is not None
|
||||
assert state.state == 'unknown'
|
||||
assert state.attributes.get('hello') == 'world'
|
||||
|
||||
@patch('homeassistant.components._LOGGER.error')
|
||||
@patch('homeassistant.bootstrap.process_ha_core_config')
|
||||
def test_reload_core_with_wrong_conf(self, mock_process, mock_error):
|
||||
"""Test reload core conf service."""
|
||||
with TemporaryDirectory() as conf_dir:
|
||||
self.hass.config.config_dir = conf_dir
|
||||
conf_yaml = self.hass.config.path(config.YAML_CONFIG_FILE)
|
||||
|
||||
with open(conf_yaml, 'a') as fp:
|
||||
fp.write(yaml.dump(['invalid', 'config']))
|
||||
|
||||
comps.reload_core_config(self.hass)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
assert mock_error.called
|
||||
assert mock_process.called is False
|
||||
@@ -21,8 +21,7 @@ class TestHelpersEntity(unittest.TestCase):
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
entity.Entity.overwrite_attribute(self.entity.entity_id,
|
||||
[ATTR_HIDDEN], [None])
|
||||
entity.set_customize({})
|
||||
|
||||
def test_default_hidden_not_in_attributes(self):
|
||||
"""Test that the default hidden property is set to False."""
|
||||
@@ -32,8 +31,7 @@ class TestHelpersEntity(unittest.TestCase):
|
||||
|
||||
def test_overwriting_hidden_property_to_true(self):
|
||||
"""Test we can overwrite hidden property to True."""
|
||||
entity.Entity.overwrite_attribute(self.entity.entity_id,
|
||||
[ATTR_HIDDEN], [True])
|
||||
entity.set_customize({self.entity.entity_id: {ATTR_HIDDEN: True}})
|
||||
self.entity.update_ha_state()
|
||||
|
||||
state = self.hass.states.get(self.entity.entity_id)
|
||||
@@ -43,3 +41,30 @@ class TestHelpersEntity(unittest.TestCase):
|
||||
"""Test split_entity_id."""
|
||||
self.assertEqual(['domain', 'object_id'],
|
||||
entity.split_entity_id('domain.object_id'))
|
||||
|
||||
def test_generate_entity_id_requires_hass_or_ids(self):
|
||||
"""Ensure we require at least hass or current ids."""
|
||||
fmt = 'test.{}'
|
||||
with self.assertRaises(ValueError):
|
||||
entity.generate_entity_id(fmt, 'hello world')
|
||||
|
||||
def test_generate_entity_id_given_hass(self):
|
||||
"""Test generating an entity id given hass object."""
|
||||
fmt = 'test.{}'
|
||||
self.assertEqual(
|
||||
'test.overwrite_hidden_true_2',
|
||||
entity.generate_entity_id(fmt, 'overwrite hidden true',
|
||||
hass=self.hass))
|
||||
|
||||
def test_generate_entity_id_given_keys(self):
|
||||
"""Test generating an entity id given current ids."""
|
||||
fmt = 'test.{}'
|
||||
self.assertEqual(
|
||||
'test.overwrite_hidden_true_2',
|
||||
entity.generate_entity_id(
|
||||
fmt, 'overwrite hidden true',
|
||||
current_ids=['test.overwrite_hidden_true']))
|
||||
self.assertEqual(
|
||||
'test.overwrite_hidden_true',
|
||||
entity.generate_entity_id(fmt, 'overwrite hidden true',
|
||||
current_ids=['test.another_entity']))
|
||||
Reference in new issue
Block a user