Extract collection entity registry cleaner into a helper (#30844)

* Extract entity_registry_keeper into separate helper
* Refactor input_*, timer and person to use new helper.
* Make Mypy happy.
* Better name.
This commit is contained in:
Alexei Chetroi authored and GitHub committed 2020-01-17 11:41:46 -05:00
1 parent da368f0cb8
commit 8a78b65f0d
9 files changed
+65 -91

No files matched your search

@@ -16,7 +16,7 @@ from homeassistant.const import (
STATE_ON,
)
from homeassistant.core import callback
from homeassistant.helpers import collection, entity_registry
from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent
@@ -113,18 +113,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
).async_setup(hass)
async def _collection_changed(
change_type: str, item_id: str, config: typing.Optional[typing.Dict]
) -> None:
"""Handle a collection change: clean up entity registry on removals."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
yaml_collection.async_add_listener(_collection_changed)
storage_collection.async_add_listener(_collection_changed)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
async def reload_service_handler(service_call: ServiceCallType) -> None:
"""Remove all input booleans and load new ones from config."""
@@ -15,7 +15,7 @@ from homeassistant.const import (
SERVICE_RELOAD,
)
from homeassistant.core import callback
from homeassistant.helpers import collection, entity_registry
from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
@@ -117,18 +117,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
).async_setup(hass)
async def _collection_changed(
change_type: str, item_id: str, config: typing.Optional[typing.Dict]
) -> None:
"""Handle a collection change: clean up entity registry on removals."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
yaml_collection.async_add_listener(_collection_changed)
storage_collection.async_add_listener(_collection_changed)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
async def reload_service_handler(service_call: ServiceCallType) -> None:
"""Reload yaml entities."""
@@ -15,7 +15,7 @@ from homeassistant.const import (
SERVICE_RELOAD,
)
from homeassistant.core import callback
from homeassistant.helpers import collection, entity_registry
from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
@@ -142,18 +142,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
).async_setup(hass)
async def _collection_changed(
change_type: str, item_id: str, config: typing.Optional[typing.Dict]
) -> None:
"""Handle a collection change: clean up entity registry on removals."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
yaml_collection.async_add_listener(_collection_changed)
storage_collection.async_add_listener(_collection_changed)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
async def reload_service_handler(service_call: ServiceCallType) -> None:
"""Reload yaml entities."""
@@ -12,7 +12,7 @@ from homeassistant.const import (
SERVICE_RELOAD,
)
from homeassistant.core import callback
from homeassistant.helpers import collection, entity_registry
from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
@@ -116,18 +116,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
).async_setup(hass)
async def _collection_changed(
change_type: str, item_id: str, config: typing.Optional[typing.Dict]
) -> None:
"""Handle a collection change: clean up entity registry on removals."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
yaml_collection.async_add_listener(_collection_changed)
storage_collection.async_add_listener(_collection_changed)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
async def reload_service_handler(service_call: ServiceCallType) -> None:
"""Reload yaml entities."""
@@ -15,7 +15,7 @@ from homeassistant.const import (
SERVICE_RELOAD,
)
from homeassistant.core import callback
from homeassistant.helpers import collection, entity_registry
from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
@@ -144,18 +144,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
).async_setup(hass)
async def _collection_changed(
change_type: str, item_id: str, config: typing.Optional[typing.Dict]
) -> None:
"""Handle a collection change: clean up entity registry on removals."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
yaml_collection.async_add_listener(_collection_changed)
storage_collection.async_add_listener(_collection_changed)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
async def reload_service_handler(service_call: ServiceCallType) -> None:
"""Reload yaml entities."""
+2 -11
View File
@@ -159,7 +159,6 @@ class PersonStorageCollection(collection.StorageCollection):
):
"""Initialize a person storage collection."""
super().__init__(store, logger, id_manager)
self.async_add_listener(self._collection_changed)
self.yaml_collection = yaml_collection
async def async_load(self) -> None:
@@ -228,16 +227,6 @@ class PersonStorageCollection(collection.StorageCollection):
if any(person for person in persons if person.get(CONF_USER_ID) == user_id):
raise ValueError("User already taken")
async def _collection_changed(
self, change_type: str, item_id: str, config: Optional[dict]
) -> None:
"""Handle a collection change."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(self.hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
async def filter_yaml_data(hass: HomeAssistantType, persons: List[dict]) -> List[dict]:
"""Validate YAML data that we can't validate via schema."""
@@ -294,6 +283,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
collection.attach_entity_component_collection(
entity_component, storage_collection, lambda conf: Person(conf, True)
)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
await yaml_collection.async_load(
await filter_yaml_data(hass, config.get(DOMAIN, []))
+3 -13
View File
@@ -13,7 +13,7 @@ from homeassistant.const import (
SERVICE_RELOAD,
)
from homeassistant.core import callback
from homeassistant.helpers import collection, entity_registry
from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import async_track_point_in_utc_time
@@ -119,18 +119,8 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
).async_setup(hass)
async def _collection_changed(
change_type: str, item_id: str, config: typing.Optional[typing.Dict]
) -> None:
"""Handle a collection change: clean up entity registry on removals."""
if change_type != collection.CHANGE_REMOVED:
return
ent_reg = await entity_registry.async_get_registry(hass)
ent_reg.async_remove(ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))
yaml_collection.async_add_listener(_collection_changed)
storage_collection.async_add_listener(_collection_changed)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, yaml_collection)
collection.attach_entity_registry_cleaner(hass, DOMAIN, DOMAIN, storage_collection)
async def reload_service_handler(service_call: ServiceCallType) -> None:
"""Reload yaml entities."""