Add config flow for template sensor (#98970)

* Add config flow for template sensor

* Tweak error reporting

* Improve validation

* Fix test

* Rename translation strings

* Improve validation

* Fix sensor async_setup_entry

* Avoid duplicating sensor device class translations

* Avoid duplicating sensor device class translations

* Add config flow tests

* Include all units from DEVICE_CLASS_UNITS in unit_of_measurement select

* Address review comments
This commit is contained in:
Erik Montnemery 2023-08-30 16:22:52 +02:00 committed by GitHub
parent bc5f934f35
commit 63c538b024
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 1174 additions and 54 deletions

View file

@ -224,14 +224,18 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
self._delay_off_raw = config.get(CONF_DELAY_OFF)
async def async_added_to_hass(self) -> None:
"""Restore state and register callbacks."""
"""Restore state."""
if (
(self._delay_on_raw is not None or self._delay_off_raw is not None)
and (last_state := await self.async_get_last_state()) is not None
and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE)
):
self._state = last_state.state == STATE_ON
await super().async_added_to_hass()
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute("_state", self._template, None, self._update_state)
if self._delay_on_raw is not None:
@ -250,7 +254,7 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
"_delay_off", self._delay_off_raw, cv.positive_time_period
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_state(self, result):