Add entity_picture_template options to Template Cover, Template Light, Template Sensor, and Template Switch (#9854)

* Re-adding cover

* Re-adding light

* Re-adding sensor

* Re-adding switch

* Re-added tests

* Fixing missing imports in rebased test

* Fixing broken tests

* Owner-requested changes

* Owner-requested changes

* Fixed exception
This commit is contained in:
Aaron Bach authored and Paulus Schoutsen committed 2017-10-30 09:28:37 -07:00
1 parent 05ece53ec2
commit 646c03eea1
9 files changed
+388 -41

No files matched your search

+40
View File
@@ -682,3 +682,43 @@ class TestTemplateCover(unittest.TestCase):
state = self.hass.states.get('cover.test_template_cover')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test icon template."""
with assert_setup_component(1, 'cover'):
assert setup.setup_component(self.hass, 'cover', {
'cover': {
'platform': 'template',
'covers': {
'test_template_cover': {
'value_template':
"{{ states.cover.test_state.state }}",
'open_cover': {
'service': 'cover.open_cover',
'entity_id': 'cover.test_state'
},
'close_cover': {
'service': 'cover.close_cover',
'entity_id': 'cover.test_state'
},
'entity_picture_template':
"{% if states.cover.test_state.state %}"
"/local/cover.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('cover.test_template_cover')
assert state.attributes.get('entity_picture') == ''
state = self.hass.states.set('cover.test_state', STATE_OPEN)
self.hass.block_till_done()
state = self.hass.states.get('cover.test_template_cover')
assert state.attributes['entity_picture'] == '/local/cover.png'
+94
View File
@@ -625,3 +625,97 @@ class TestTemplateLight:
assert state is not None
assert state.attributes.get('friendly_name') == 'Template light'
def test_icon_template(self):
"""Test icon template."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'friendly_name': 'Template light',
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
},
'icon_template':
"{% if states.light.test_state.state %}"
"mdi:check"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes.get('icon') == ''
state = self.hass.states.set('light.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1, 'light'):
assert setup.setup_component(self.hass, 'light', {
'light': {
'platform': 'template',
'lights': {
'test_template_light': {
'friendly_name': 'Template light',
'value_template': "{{ 1 == 1 }}",
'turn_on': {
'service': 'light.turn_on',
'entity_id': 'light.test_state'
},
'turn_off': {
'service': 'light.turn_off',
'entity_id': 'light.test_state'
},
'set_level': {
'service': 'light.turn_on',
'data_template': {
'entity_id': 'light.test_state',
'brightness': '{{brightness}}'
}
},
'entity_picture_template':
"{% if states.light.test_state.state %}"
"/local/light.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes.get('entity_picture') == ''
state = self.hass.states.set('light.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light')
assert state.attributes['entity_picture'] == '/local/light.png'
+30
View File
@@ -74,6 +74,36 @@ class TestTemplateSensor:
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template': "State",
'entity_picture_template':
"{% if states.sensor.test_state.state == "
"'Works' %}"
"/local/sensor.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes.get('entity_picture') == ''
self.hass.states.set('sensor.test_state', 'Works')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['entity_picture'] == '/local/sensor.png'
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0):
+39
View File
@@ -166,6 +166,45 @@ class TestTemplateSwitch:
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes['icon'] == 'mdi:check'
def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1, 'switch'):
assert setup.setup_component(self.hass, 'switch', {
'switch': {
'platform': 'template',
'switches': {
'test_template_switch': {
'value_template':
"{{ states.switch.test_state.state }}",
'turn_on': {
'service': 'switch.turn_on',
'entity_id': 'switch.test_state'
},
'turn_off': {
'service': 'switch.turn_off',
'entity_id': 'switch.test_state'
},
'entity_picture_template':
"{% if states.switch.test_state.state %}"
"/local/switch.png"
"{% endif %}"
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes.get('entity_picture') == ''
state = self.hass.states.set('switch.test_state', STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get('switch.test_template_switch')
assert state.attributes['entity_picture'] == '/local/switch.png'
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0, 'switch'):