New template sensor attributes (#26127)
* updated sensor and test files * Formatting fixes * Updated attribute template code * Black formatting * Code improvements based on feedback on binary_sensor pull request * Updated tests * Remove duplicated code and fix tests * Black formatting on tests * Remove link from docstring * Moved default to schema * Formatting fix and change to use dict[key] to retrieve attribute_templates
This commit is contained in:
parent
1617c2cd64
commit
79488daddf
2 changed files with 123 additions and 13 deletions
|
@ -174,6 +174,38 @@ class TestTemplateSensor:
|
|||
state = self.hass.states.get("sensor.test_template_sensor")
|
||||
assert state.attributes["friendly_name"] == "It Works."
|
||||
|
||||
def test_attribute_templates(self):
|
||||
"""Test attribute_templates template."""
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(
|
||||
self.hass,
|
||||
"sensor",
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "template",
|
||||
"sensors": {
|
||||
"test_template_sensor": {
|
||||
"value_template": "{{ states.sensor.test_state.state }}",
|
||||
"attribute_templates": {
|
||||
"test_attribute": "It {{ states.sensor.test_state.state }}."
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("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("sensor.test_template_sensor")
|
||||
assert state.attributes["test_attribute"] == "It Works."
|
||||
|
||||
def test_template_syntax_error(self):
|
||||
"""Test templating syntax error."""
|
||||
with assert_setup_component(0):
|
||||
|
@ -345,6 +377,34 @@ class TestTemplateSensor:
|
|||
assert "device_class" not in state.attributes
|
||||
|
||||
|
||||
async def test_invalid_attribute_template(hass, caplog):
|
||||
"""Test that errors are logged if rendering template fails."""
|
||||
hass.states.async_set("sensor.test_sensor", "startup")
|
||||
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "template",
|
||||
"sensors": {
|
||||
"invalid_template": {
|
||||
"value_template": "{{ states.sensor.test_sensor.state }}",
|
||||
"attribute_templates": {
|
||||
"test_attribute": "{{ states.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("sensor.invalid_template")
|
||||
|
||||
assert ("Error rendering attribute test_attribute") in caplog.text
|
||||
|
||||
|
||||
async def test_no_template_match_all(hass, caplog):
|
||||
"""Test that we do not allow sensors that match on all."""
|
||||
hass.states.async_set("sensor.test_sensor", "startup")
|
||||
|
@ -369,12 +429,22 @@ async def test_no_template_match_all(hass, caplog):
|
|||
"value_template": "{{ states.sensor.test_sensor.state }}",
|
||||
"friendly_name_template": "{{ 1 + 1 }}",
|
||||
},
|
||||
"invalid_attribute": {
|
||||
"value_template": "{{ states.sensor.test_sensor.state }}",
|
||||
"attribute_templates": {"test_attribute": "{{ 1 + 1 }}"},
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert hass.states.get("sensor.invalid_state").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_icon").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_entity_picture").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_friendly_name").state == "unknown"
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all()) == 5
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert (
|
||||
"Template sensor invalid_state has no entity ids "
|
||||
"configured to track nor were we able to extract the entities to "
|
||||
|
@ -395,11 +465,17 @@ async def test_no_template_match_all(hass, caplog):
|
|||
"configured to track nor were we able to extract the entities to "
|
||||
"track from the friendly_name template"
|
||||
) in caplog.text
|
||||
assert (
|
||||
"Template sensor invalid_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("sensor.invalid_state").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_icon").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_entity_picture").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_friendly_name").state == "unknown"
|
||||
assert hass.states.get("sensor.invalid_attribute").state == "unknown"
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
|
@ -408,6 +484,7 @@ async def test_no_template_match_all(hass, caplog):
|
|||
assert hass.states.get("sensor.invalid_icon").state == "startup"
|
||||
assert hass.states.get("sensor.invalid_entity_picture").state == "startup"
|
||||
assert hass.states.get("sensor.invalid_friendly_name").state == "startup"
|
||||
assert hass.states.get("sensor.invalid_attribute").state == "startup"
|
||||
|
||||
hass.states.async_set("sensor.test_sensor", "hello")
|
||||
await hass.async_block_till_done()
|
||||
|
@ -416,6 +493,7 @@ async def test_no_template_match_all(hass, caplog):
|
|||
assert hass.states.get("sensor.invalid_icon").state == "startup"
|
||||
assert hass.states.get("sensor.invalid_entity_picture").state == "startup"
|
||||
assert hass.states.get("sensor.invalid_friendly_name").state == "startup"
|
||||
assert hass.states.get("sensor.invalid_attribute").state == "startup"
|
||||
|
||||
await hass.helpers.entity_component.async_update_entity("sensor.invalid_state")
|
||||
await hass.helpers.entity_component.async_update_entity("sensor.invalid_icon")
|
||||
|
@ -425,8 +503,10 @@ async def test_no_template_match_all(hass, caplog):
|
|||
await hass.helpers.entity_component.async_update_entity(
|
||||
"sensor.invalid_friendly_name"
|
||||
)
|
||||
await hass.helpers.entity_component.async_update_entity("sensor.invalid_attribute")
|
||||
|
||||
assert hass.states.get("sensor.invalid_state").state == "2"
|
||||
assert hass.states.get("sensor.invalid_icon").state == "hello"
|
||||
assert hass.states.get("sensor.invalid_entity_picture").state == "hello"
|
||||
assert hass.states.get("sensor.invalid_friendly_name").state == "hello"
|
||||
assert hass.states.get("sensor.invalid_attribute").state == "hello"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue