Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create base TriggerEntity #91128

Merged
Prev Previous commit
Next Next commit
unique_id
  • Loading branch information
gjohansson-ST committed Apr 12, 2023
commit ac2b767f0950d0090167a72828188f2c2b5524c7
24 changes: 13 additions & 11 deletions homeassistant/components/template/trigger_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TriggerBaseEntity(Entity):
domain: str
extra_template_keys: tuple | None = None
extra_template_keys_complex: tuple | None = None
_unique_id: str | None

def __init__(
self,
Expand All @@ -46,9 +47,7 @@ def __init__(
"""Initialize the entity."""
self.hass = hass

entity_unique_id = config.get(CONF_UNIQUE_ID)

self._unique_id: str | None = entity_unique_id
self._set_unique_id(config.get(CONF_UNIQUE_ID))

self._config = config

Expand Down Expand Up @@ -125,6 +124,10 @@ async def async_added_to_hass(self) -> None:
"""Handle being added to Home Assistant."""
template.attach(self.hass, self._config)

def _set_unique_id(self, unique_id: str | None) -> None:
"""Set unique id."""
self._unique_id = unique_id

def restore_attributes(self, last_state: State) -> None:
"""Restore attributes."""
for conf_key, attr in CONF_TO_ATTRIBUTE.items():
Expand Down Expand Up @@ -197,21 +200,20 @@ def __init__(
CoordinatorEntity.__init__(self, coordinator)
TriggerBaseEntity.__init__(self, hass, config)

entity_unique_id = config.get(CONF_UNIQUE_ID)

self._unique_id: str | None
if entity_unique_id and coordinator.unique_id:
self._unique_id = f"{coordinator.unique_id}-{entity_unique_id}"
else:
self._unique_id = entity_unique_id

async def async_added_to_hass(self) -> None:
"""Handle being added to Home Assistant."""
await TriggerBaseEntity.async_added_to_hass(self)
gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved
await super().async_added_to_hass()
if self.coordinator.data is not None:
self._process_data()

def _set_unique_id(self, unique_id: str | None) -> None:
"""Set unique id."""
if unique_id and self.coordinator.unique_id:
self._unique_id = f"{self.coordinator.unique_id}-{unique_id}"
else:
self._unique_id = unique_id

@callback
def _process_data(self) -> None:
"""Process new data."""
Expand Down