2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Configurator component."""
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=protected-access
|
2015-01-20 05:39:24 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import homeassistant.components.configurator as configurator
|
2019-12-09 17:56:21 +00:00
|
|
|
from homeassistant.const import ATTR_FRIENDLY_NAME, EVENT_TIME_CHANGED
|
2015-01-20 05:39:24 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
class TestConfigurator(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Configurator component."""
|
2015-01-20 05:39:24 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-01-20 05:39:24 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-01-20 05:39:24 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_request_least_info(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test request config with least amount of data."""
|
2015-01-20 05:39:24 +00:00
|
|
|
request_id = configurator.request_config(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, "Test Request", lambda _: None
|
|
|
|
)
|
2015-01-20 05:39:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert 1 == len(
|
|
|
|
self.hass.services.services.get(configurator.DOMAIN, [])
|
|
|
|
), "No new service registered"
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
states = self.hass.states.all()
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(states), "Expected a new state registered"
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
state = states[0]
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert configurator.STATE_CONFIGURE == state.state
|
2019-07-31 19:25:30 +00:00
|
|
|
assert request_id == state.attributes.get(configurator.ATTR_CONFIGURE_ID)
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
def test_request_all_info(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test request config with all possible info."""
|
2016-06-11 03:50:04 +00:00
|
|
|
exp_attr = {
|
|
|
|
ATTR_FRIENDLY_NAME: "Test Request",
|
2017-11-20 13:11:55 +00:00
|
|
|
configurator.ATTR_DESCRIPTION: """config description
|
|
|
|
|
|
|
|
[link name](link url)
|
|
|
|
|
|
|
|
![Description image](config image url)""",
|
2016-06-11 03:50:04 +00:00
|
|
|
configurator.ATTR_SUBMIT_CAPTION: "config submit caption",
|
|
|
|
configurator.ATTR_FIELDS: [],
|
2016-10-25 05:28:34 +00:00
|
|
|
configurator.ATTR_ENTITY_PICTURE: "config entity picture",
|
2016-06-11 03:50:04 +00:00
|
|
|
configurator.ATTR_CONFIGURE_ID: configurator.request_config(
|
2016-10-25 05:28:34 +00:00
|
|
|
self.hass,
|
|
|
|
name="Test Request",
|
|
|
|
callback=lambda _: None,
|
|
|
|
description="config description",
|
|
|
|
description_image="config image url",
|
|
|
|
submit_caption="config submit caption",
|
|
|
|
fields=None,
|
|
|
|
link_name="link name",
|
|
|
|
link_url="link url",
|
|
|
|
entity_picture="config entity picture",
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
2016-06-11 03:50:04 +00:00
|
|
|
}
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
states = self.hass.states.all()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(states)
|
2015-01-20 05:39:24 +00:00
|
|
|
state = states[0]
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert configurator.STATE_CONFIGURE == state.state
|
2017-11-20 13:11:55 +00:00
|
|
|
assert exp_attr == state.attributes
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
def test_callback_called_on_configure(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if our callback gets called when configure service called."""
|
2015-01-20 05:39:24 +00:00
|
|
|
calls = []
|
|
|
|
request_id = configurator.request_config(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, "Test Request", lambda _: calls.append(1)
|
|
|
|
)
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
self.hass.services.call(
|
2019-07-31 19:25:30 +00:00
|
|
|
configurator.DOMAIN,
|
|
|
|
configurator.SERVICE_CONFIGURE,
|
|
|
|
{configurator.ATTR_CONFIGURE_ID: request_id},
|
|
|
|
)
|
2015-01-20 05:39:24 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(calls), "Callback not called"
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
def test_state_change_on_notify_errors(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test state change on notify errors."""
|
2015-01-20 05:39:24 +00:00
|
|
|
request_id = configurator.request_config(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, "Test Request", lambda _: None
|
|
|
|
)
|
2015-01-20 05:39:24 +00:00
|
|
|
error = "Oh no bad bad bad"
|
2017-08-14 05:37:50 +00:00
|
|
|
configurator.notify_errors(self.hass, request_id, error)
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
state = self.hass.states.all()[0]
|
2018-10-24 10:10:05 +00:00
|
|
|
assert error == state.attributes.get(configurator.ATTR_ERRORS)
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
def test_notify_errors_fail_silently_on_bad_request_id(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if notify errors fails silently with a bad request id."""
|
2017-08-14 05:37:50 +00:00
|
|
|
configurator.notify_errors(self.hass, 2015, "Try this error")
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
def test_request_done_works(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if calling request done works."""
|
2015-01-20 05:39:24 +00:00
|
|
|
request_id = configurator.request_config(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, "Test Request", lambda _: None
|
|
|
|
)
|
2017-08-14 05:37:50 +00:00
|
|
|
configurator.request_done(self.hass, request_id)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(self.hass.states.all())
|
2015-01-20 05:39:24 +00:00
|
|
|
|
2015-01-20 06:40:30 +00:00
|
|
|
self.hass.bus.fire(EVENT_TIME_CHANGED)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(self.hass.states.all())
|
2015-01-20 05:39:24 +00:00
|
|
|
|
|
|
|
def test_request_done_fail_silently_on_bad_request_id(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test that request_done fails silently with a bad request id."""
|
2017-08-14 05:37:50 +00:00
|
|
|
configurator.request_done(self.hass, 2016)
|