Fix TriggerEntity.async_added_to_hass (#100119)

This commit is contained in:
Erik Montnemery 2023-09-11 14:33:43 +02:00 committed by GitHub
parent a6e9bf830c
commit 42046a3ce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -23,8 +23,7 @@ class TriggerEntity(TriggerBaseEntity, CoordinatorEntity[TriggerUpdateCoordinato
async def async_added_to_hass(self) -> None:
"""Handle being added to Home Assistant."""
await TriggerBaseEntity.async_added_to_hass(self)
await CoordinatorEntity.async_added_to_hass(self) # type: ignore[arg-type]
await super().async_added_to_hass()
if self.coordinator.data is not None:
self._process_data()

View File

@ -1,7 +1,7 @@
"""The test for the Template sensor platform."""
from asyncio import Event
from datetime import timedelta
from unittest.mock import patch
from unittest.mock import ANY, patch
import pytest
from syrupy.assertion import SnapshotAssertion
@ -1192,6 +1192,48 @@ async def test_trigger_entity(
assert state.context is context
@pytest.mark.parametrize(("count", "domain"), [(1, "template")])
@pytest.mark.parametrize(
"config",
[
{
"template": [
{
"trigger": {"platform": "event", "event_type": "test_event"},
"sensors": {
"hello": {
"friendly_name": "Hello Name",
"value_template": "{{ trigger.event.data.beer }}",
"entity_picture_template": "{{ '/local/dogs.png' }}",
"icon_template": "{{ 'mdi:pirate' }}",
"attribute_templates": {
"last": "{{now().strftime('%D %X')}}",
"history_1": "{{this.attributes.last|default('Not yet set')}}",
},
},
},
},
],
},
],
)
async def test_trigger_entity_runs_once(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry
) -> None:
"""Test trigger entity handles a trigger once."""
state = hass.states.get("sensor.hello_name")
assert state is not None
assert state.state == STATE_UNKNOWN
hass.bus.async_fire("test_event", {"beer": 2})
await hass.async_block_till_done()
state = hass.states.get("sensor.hello_name")
assert state.state == "2"
assert state.attributes.get("last") == ANY
assert state.attributes.get("history_1") == "Not yet set"
@pytest.mark.parametrize(("count", "domain"), [(1, "template")])
@pytest.mark.parametrize(
"config",