Add mixin class CollectionEntity for the collection helper (#77703)

* Add mixin class CollectionEntity for the collection helper

* Improve typing

* Address review comments

* Fix tests
This commit is contained in:
Erik Montnemery 2022-09-03 12:56:49 +02:00 committed by GitHub
parent 56278a4421
commit b0d033ef29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 221 additions and 86 deletions

View file

@ -149,7 +149,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
logging.getLogger(f"{__name__}.yaml_collection"), id_manager
)
collection.sync_entity_lifecycle(
hass, DOMAIN, DOMAIN, component, yaml_collection, InputDatetime.from_yaml
hass, DOMAIN, DOMAIN, component, yaml_collection, InputDatetime
)
storage_collection = DateTimeStorageCollection(
@ -231,15 +231,15 @@ class DateTimeStorageCollection(collection.StorageCollection):
return has_date_or_time({**data, **update_data})
class InputDatetime(RestoreEntity):
class InputDatetime(collection.CollectionEntity, RestoreEntity):
"""Representation of a datetime input."""
_attr_should_poll = False
editable: bool
def __init__(self, config: dict) -> None:
def __init__(self, config: ConfigType) -> None:
"""Initialize a select input."""
self._config = config
self.editable = True
self._current_datetime = None
if not config.get(CONF_INITIAL):
@ -258,8 +258,15 @@ class InputDatetime(RestoreEntity):
)
@classmethod
def from_yaml(cls, config: dict) -> InputDatetime:
"""Return entity instance initialized from yaml storage."""
def from_storage(cls, config: ConfigType) -> InputDatetime:
"""Return entity instance initialized from storage."""
input_dt = cls(config)
input_dt.editable = True
return input_dt
@classmethod
def from_yaml(cls, config: ConfigType) -> InputDatetime:
"""Return entity instance initialized from yaml."""
input_dt = cls(config)
input_dt.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
input_dt.editable = False
@ -420,7 +427,7 @@ class InputDatetime(RestoreEntity):
)
self.async_write_ha_state()
async def async_update_config(self, config: dict) -> None:
async def async_update_config(self, config: ConfigType) -> None:
"""Handle when the config is updated."""
self._config = config
self.async_write_ha_state()