Allow trigger based numeric sensors to be set to unknown (#137047)

* Allow trigger based numeric sensors to be set to unknown

* resolve comments

* Do case insensitive check

* use _parse_result

---------

Co-authored-by: abmantis <amfcalt@gmail.com>
This commit is contained in:
Petro31 2025-07-02 09:35:47 -04:00 committed by GitHub
parent d6da686ffe
commit adec157d43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -339,6 +339,7 @@ class TriggerSensorEntity(TriggerEntity, RestoreSensor):
"""Initialize."""
super().__init__(hass, coordinator, config)
self._parse_result.add(CONF_STATE)
if (last_reset_template := config.get(ATTR_LAST_RESET)) is not None:
if last_reset_template.is_static:
self._static_rendered[ATTR_LAST_RESET] = last_reset_template.template

View File

@ -1527,6 +1527,45 @@ async def test_trigger_entity_available(hass: HomeAssistant) -> None:
assert state.state == "unavailable"
@pytest.mark.parametrize(("source_event_value"), [None, "None"])
async def test_numeric_trigger_entity_set_unknown(
hass: HomeAssistant, source_event_value: str | None
) -> None:
"""Test trigger entity state parsing with numeric sensors."""
assert await async_setup_component(
hass,
"template",
{
"template": [
{
"trigger": {"platform": "event", "event_type": "test_event"},
"sensor": [
{
"name": "Source",
"state": "{{ trigger.event.data.value }}",
},
],
},
],
},
)
await hass.async_block_till_done()
hass.bus.async_fire("test_event", {"value": 1})
await hass.async_block_till_done()
state = hass.states.get("sensor.source")
assert state is not None
assert state.state == "1"
hass.bus.async_fire("test_event", {"value": source_event_value})
await hass.async_block_till_done()
state = hass.states.get("sensor.source")
assert state is not None
assert state.state == STATE_UNKNOWN
async def test_trigger_entity_available_skips_state(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: