Template binary sensor attributes (#22664)

* Added attribute support to template binary sensor with tests

Added attribute support to template binary sensor with tests

* fix dictionary update

fix dictionary update

* Fixed whitespace and line length issues

* Fixed indentation

* Simplify applying of attribute templates based on feedback

* Syntax and whitespace fixes

* Black formatting

* Black formatting on tests

* Check attribute_templates is not None

* Fixed test

* Added test for failure to render template

* Test fix

* Updated test

* Removed whitespace and applied Black formatting

* Fixed test assertion

* Updated test

* Code improvements folloing comments

Using chain to iterate over templates and attribute_templates
Replacing dict() with {}
Rmoving unused constant

* Applied Black formatting

* Fixed removed code

* Default attribute_templates to empty dict

* Black formatting

* Fixed imports
This commit is contained in:
Steven Rollason authored and Paulus Schoutsen committed 2019-08-21 14:07:27 -07:00
1 parent 588eac82c7
commit 9bcb48985b
2 files changed
+117 -11

No files matched your search

@@ -166,6 +166,38 @@ class TestBinarySensorTemplate(unittest.TestCase):
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["entity_picture"] == "/local/sensor.png"
def test_attribute_templates(self):
"""Test attribute_templates template."""
with assert_setup_component(1):
assert setup.setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {
"test_template_sensor": {
"value_template": "{{ states.sensor.xyz.state }}",
"attribute_templates": {
"test_attribute": "It {{ states.sensor.test_state.state }}."
},
}
},
}
},
)
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes.get("test_attribute") == "It ."
self.hass.states.set("sensor.test_state", "Works")
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.test_template_sensor")
assert state.attributes["test_attribute"] == "It Works."
@mock.patch(
"homeassistant.components.template.binary_sensor."
"BinarySensorTemplate._async_render"
@@ -209,6 +241,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
MATCH_ALL,
None,
None,
None,
).result()
assert not vs.should_poll
assert "motion" == vs.device_class
@@ -268,6 +301,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
MATCH_ALL,
None,
None,
None,
).result()
mock_render.side_effect = TemplateError("foo")
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
@@ -394,6 +428,36 @@ async def test_template_delay_off(hass):
assert state.state == "on"
async def test_invalid_attribute_template(hass, caplog):
"""Test that errors are logged if rendering template fails."""
hass.states.async_set("binary_sensor.test_sensor", "true")
await setup.async_setup_component(
hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "template",
"sensors": {
"invalid_template": {
"value_template": "{{ states.binary_sensor.test_sensor }}",
"attribute_templates": {
"test_attribute": "{{ states.binary_sensor.unknown.attributes.picture }}"
},
}
},
}
},
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 2
await hass.helpers.entity_component.async_update_entity(
"binary_sensor.invalid_template"
)
assert ("Error rendering attribute test_attribute") in caplog.text
async def test_no_update_template_match_all(hass, caplog):
"""Test that we do not update sensors that match on all."""
hass.states.async_set("binary_sensor.test_sensor", "true")
@@ -414,12 +478,16 @@ async def test_no_update_template_match_all(hass, caplog):
"value_template": "{{ states.binary_sensor.test_sensor.state }}",
"entity_picture_template": "{{ 1 + 1 }}",
},
"all_attribute": {
"value_template": "{{ states.binary_sensor.test_sensor.state }}",
"attribute_templates": {"test_attribute": "{{ 1 + 1 }}"},
},
},
}
},
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 4
assert len(hass.states.async_all()) == 5
assert (
"Template binary sensor all_state has no entity ids "
"configured to track nor were we able to extract the entities to "
@@ -435,10 +503,16 @@ async def test_no_update_template_match_all(hass, caplog):
"configured to track nor were we able to extract the entities to "
"track from the entity_picture template"
) in caplog.text
assert (
"Template binary sensor all_attribute has no entity ids "
"configured to track nor were we able to extract the entities to "
"track from the test_attribute template"
) in caplog.text
assert hass.states.get("binary_sensor.all_state").state == "off"
assert hass.states.get("binary_sensor.all_icon").state == "off"
assert hass.states.get("binary_sensor.all_entity_picture").state == "off"
assert hass.states.get("binary_sensor.all_attribute").state == "off"
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
@@ -446,6 +520,7 @@ async def test_no_update_template_match_all(hass, caplog):
assert hass.states.get("binary_sensor.all_state").state == "on"
assert hass.states.get("binary_sensor.all_icon").state == "on"
assert hass.states.get("binary_sensor.all_entity_picture").state == "on"
assert hass.states.get("binary_sensor.all_attribute").state == "on"
hass.states.async_set("binary_sensor.test_sensor", "false")
await hass.async_block_till_done()
@@ -453,13 +528,18 @@ async def test_no_update_template_match_all(hass, caplog):
assert hass.states.get("binary_sensor.all_state").state == "on"
assert hass.states.get("binary_sensor.all_icon").state == "on"
assert hass.states.get("binary_sensor.all_entity_picture").state == "on"
assert hass.states.get("binary_sensor.all_attribute").state == "on"
await hass.helpers.entity_component.async_update_entity("binary_sensor.all_state")
await hass.helpers.entity_component.async_update_entity("binary_sensor.all_icon")
await hass.helpers.entity_component.async_update_entity(
"binary_sensor.all_entity_picture"
)
await hass.helpers.entity_component.async_update_entity(
"binary_sensor.all_attribute"
)
assert hass.states.get("binary_sensor.all_state").state == "on"
assert hass.states.get("binary_sensor.all_icon").state == "off"
assert hass.states.get("binary_sensor.all_entity_picture").state == "off"
assert hass.states.get("binary_sensor.all_attribute").state == "off"