Skip to content

Commit adec157

Browse files
Petro31abmantis
andauthored
Allow trigger based numeric sensors to be set to unknown (home-assistant#137047)
* Allow trigger based numeric sensors to be set to unknown * resolve comments * Do case insensitive check * use _parse_result --------- Co-authored-by: abmantis <[email protected]>
1 parent d6da686 commit adec157

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

homeassistant/components/template/sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def __init__(
339339
"""Initialize."""
340340
super().__init__(hass, coordinator, config)
341341

342+
self._parse_result.add(CONF_STATE)
342343
if (last_reset_template := config.get(ATTR_LAST_RESET)) is not None:
343344
if last_reset_template.is_static:
344345
self._static_rendered[ATTR_LAST_RESET] = last_reset_template.template

tests/components/template/test_sensor.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,45 @@ async def test_trigger_entity_available(hass: HomeAssistant) -> None:
15271527
assert state.state == "unavailable"
15281528

15291529

1530+
@pytest.mark.parametrize(("source_event_value"), [None, "None"])
1531+
async def test_numeric_trigger_entity_set_unknown(
1532+
hass: HomeAssistant, source_event_value: str | None
1533+
) -> None:
1534+
"""Test trigger entity state parsing with numeric sensors."""
1535+
assert await async_setup_component(
1536+
hass,
1537+
"template",
1538+
{
1539+
"template": [
1540+
{
1541+
"trigger": {"platform": "event", "event_type": "test_event"},
1542+
"sensor": [
1543+
{
1544+
"name": "Source",
1545+
"state": "{{ trigger.event.data.value }}",
1546+
},
1547+
],
1548+
},
1549+
],
1550+
},
1551+
)
1552+
await hass.async_block_till_done()
1553+
1554+
hass.bus.async_fire("test_event", {"value": 1})
1555+
await hass.async_block_till_done()
1556+
1557+
state = hass.states.get("sensor.source")
1558+
assert state is not None
1559+
assert state.state == "1"
1560+
1561+
hass.bus.async_fire("test_event", {"value": source_event_value})
1562+
await hass.async_block_till_done()
1563+
1564+
state = hass.states.get("sensor.source")
1565+
assert state is not None
1566+
assert state.state == STATE_UNKNOWN
1567+
1568+
15301569
async def test_trigger_entity_available_skips_state(
15311570
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
15321571
) -> None:

0 commit comments

Comments
 (0)