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

View File

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