wrapped up unit testing

This commit is contained in:
Brian Cribbs
2017-05-18 16:59:18 -04:00
parent 876f270c57
commit 43645d28da
2 changed files with 16 additions and 22 deletions

View File

@@ -101,7 +101,7 @@ class LightTemplate(Light):
self._level_template = level_template
self._state = False
self._brightness = 0
self._brightness = None
self._entities = entity_ids
if self._template is not None:
@@ -112,8 +112,6 @@ class LightTemplate(Light):
@property
def brightness(self):
"""Return the brightness of the light."""
if self._level_template is None:
return None
return self._brightness
@property
@@ -133,20 +131,17 @@ class LightTemplate(Light):
def async_added_to_hass(self):
"""Register callbacks."""
state = yield from async_get_last_state(self.hass, self.entity_id)
_LOGGER.error("Previous state thawed " + str(state))
if state:
self._state = state.state == STATE_ON
@callback
def template_light_state_listener(entity, old_state, new_state):
"""Handle target device state changes."""
_LOGGER.error("state listener callback")
self.hass.async_add_job(self.async_update_ha_state(True))
@callback
def template_light_startup(event):
"""Update template on startup."""
_LOGGER.error("startup callback")
async_track_state_change(
self.hass, self._entities, template_light_state_listener)
@@ -158,17 +153,14 @@ class LightTemplate(Light):
@asyncio.coroutine
def async_turn_on(self, **kwargs):
"""Turn the light on."""
_LOGGER.error(str(kwargs))
_LOGGER.error(str(self._level_script))
self._state = True
if ATTR_BRIGHTNESS in kwargs and self._level_script:
self._brightness = kwargs[ATTR_BRIGHTNESS]
self.hass.async_add_job(self._level_script.async_run(
{"brightness": kwargs[ATTR_BRIGHTNESS]}))
self._brightness = kwargs[ATTR_BRIGHTNESS]
else:
self.hass.async_add_job(self._on_script.async_run())
self._state = True
@asyncio.coroutine
def async_turn_off(self, **kwargs):
"""Turn the light off."""
@@ -192,7 +184,8 @@ class LightTemplate(Light):
self._state = None
if self._level_template is not None:
self._brightness = self._level_template.async_render()
brightness = self._level_template.async_render()
self._brightness = brightness
except TemplateError as ex:
_LOGGER.error(ex)

View File

@@ -427,12 +427,13 @@ class TestTemplateLight:
assert len(self.calls) == 1
def test_level_action_no_template(self):
"""Test setting brightness"""
"""Test setting brightness with optimistic template."""
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': '{{1 == 1}}',
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
@@ -444,11 +445,10 @@ class TestTemplateLight:
'set_level': {
'service': 'test.automation',
'data_template': {
'entity_id': 'light.test_state',
'entity_id': 'test.test_state',
'brightness': '{{brightness}}'
}
},
'level_template': '{{states.light.test_state.attributes.brightness}}'
}
}
}
@@ -462,23 +462,24 @@ class TestTemplateLight:
core.light.turn_on(
self.hass, 'light.test_template_light', **{ATTR_BRIGHTNESS: 124})
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert len(self.calls) == 1
state = self.hass.states.get('light.test_template_light')
assert self.calls[0].data['brightness'] == '124'
# need to add assertions about optimistic state
# assert state.attributes.get('brightness') == 124
state = self.hass.states.get('light.test_template_light')
assert state != None
assert state.attributes.get('brightness') == 124
def test_level_template(self):
"""Test the setting of the state with off."""
"""Test the template for the level."""
with assert_setup_component(1):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'value_template': "{{ 1 == 2 }}",
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
@@ -506,8 +507,8 @@ class TestTemplateLight:
state = self.hass.states.get('light.test_template_light')
assert state != None
_LOGGER.error(str(state))
assert state.attributes.get('brightness') == 42
assert state.attributes.get('brightness') == '42'
@asyncio.coroutine
def test_restore_state(hass):