Clean up entity component (#11691)

* Clean up entity component

* Lint

* List -> Tuple

* Add Entity.async_remove back

* Unflake setting up group test
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-01-22 22:54:41 -08:00
1 parent d478517c51
commit 183e0543b4
14 files changed
+230 -191

No files matched your search

+36 -40
View File
@@ -42,8 +42,6 @@ ATTR_ORDER = 'order'
ATTR_VIEW = 'view'
ATTR_VISIBLE = 'visible'
DATA_ALL_GROUPS = 'data_all_groups'
SERVICE_SET_VISIBILITY = 'set_visibility'
SERVICE_SET = 'set'
SERVICE_REMOVE = 'remove'
@@ -250,8 +248,10 @@ def get_entity_ids(hass, entity_id, domain_filter=None):
@asyncio.coroutine
def async_setup(hass, config):
"""Set up all groups found definded in the configuration."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
hass.data[DATA_ALL_GROUPS] = {}
component = hass.data.get(DOMAIN)
if component is None:
component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass)
yield from _async_process_config(hass, config, component)
@@ -271,10 +271,11 @@ def async_setup(hass, config):
def groups_service_handler(service):
"""Handle dynamic group service functions."""
object_id = service.data[ATTR_OBJECT_ID]
service_groups = hass.data[DATA_ALL_GROUPS]
entity_id = ENTITY_ID_FORMAT.format(object_id)
group = component.get_entity(entity_id)
# new group
if service.service == SERVICE_SET and object_id not in service_groups:
if service.service == SERVICE_SET and group is None:
entity_ids = service.data.get(ATTR_ENTITIES) or \
service.data.get(ATTR_ADD_ENTITIES) or None
@@ -289,12 +290,15 @@ def async_setup(hass, config):
user_defined=False,
**extra_arg
)
return
if group is None:
_LOGGER.warning("%s:Group '%s' doesn't exist!",
service.service, object_id)
return
# update group
if service.service == SERVICE_SET:
group = service_groups[object_id]
need_update = False
if ATTR_ADD_ENTITIES in service.data:
@@ -333,12 +337,7 @@ def async_setup(hass, config):
# remove group
if service.service == SERVICE_REMOVE:
if object_id not in service_groups:
_LOGGER.warning("Group '%s' doesn't exist!", object_id)
return
del_group = service_groups.pop(object_id)
yield from del_group.async_stop()
yield from component.async_remove_entity(entity_id)
hass.services.async_register(
DOMAIN, SERVICE_SET, groups_service_handler,
@@ -395,7 +394,7 @@ class Group(Entity):
"""Track a group of entity ids."""
def __init__(self, hass, name, order=None, visible=True, icon=None,
view=False, control=None, user_defined=True):
view=False, control=None, user_defined=True, entity_ids=None):
"""Initialize a group.
This Object has factory function for creation.
@@ -405,7 +404,10 @@ class Group(Entity):
self._state = STATE_UNKNOWN
self._icon = icon
self.view = view
self.tracking = []
if entity_ids:
self.tracking = tuple(ent_id.lower() for ent_id in entity_ids)
else:
self.tracking = tuple()
self.group_on = None
self.group_off = None
self.visible = visible
@@ -439,23 +441,21 @@ class Group(Entity):
hass, name,
order=len(hass.states.async_entity_ids(DOMAIN)),
visible=visible, icon=icon, view=view, control=control,
user_defined=user_defined
user_defined=user_defined, entity_ids=entity_ids
)
group.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id or name, hass=hass)
# run other async stuff
if entity_ids is not None:
yield from group.async_update_tracked_entity_ids(entity_ids)
else:
yield from group.async_update_ha_state(True)
# If called before the platform async_setup is called (test cases)
if DATA_ALL_GROUPS not in hass.data:
hass.data[DATA_ALL_GROUPS] = {}
component = hass.data.get(DOMAIN)
if component is None:
component = hass.data[DOMAIN] = \
EntityComponent(_LOGGER, DOMAIN, hass)
yield from component.async_add_entities([group], True)
hass.data[DATA_ALL_GROUPS][object_id] = group
return group
@property
@@ -534,10 +534,6 @@ class Group(Entity):
yield from self.async_update_ha_state(True)
self.async_start()
def start(self):
"""Start tracking members."""
self.hass.add_job(self.async_start)
@callback
def async_start(self):
"""Start tracking members.
@@ -549,17 +545,15 @@ class Group(Entity):
self.hass, self.tracking, self._async_state_changed_listener
)
def stop(self):
"""Unregister the group from Home Assistant."""
run_coroutine_threadsafe(self.async_stop(), self.hass.loop).result()
@asyncio.coroutine
def async_stop(self):
"""Unregister the group from Home Assistant.
This method must be run in the event loop.
"""
yield from self.async_remove()
if self._async_unsub_state_changed:
self._async_unsub_state_changed()
self._async_unsub_state_changed = None
@asyncio.coroutine
def async_update(self):
@@ -567,17 +561,19 @@ class Group(Entity):
self._state = STATE_UNKNOWN
self._async_update_group_state()
def async_remove(self):
"""Remove group from HASS.
@asyncio.coroutine
def async_added_to_hass(self):
"""Callback when added to HASS."""
if self.tracking:
self.async_start()
This method must be run in the event loop and returns a coroutine.
"""
@asyncio.coroutine
def async_will_remove_from_hass(self):
"""Callback when removed from HASS."""
if self._async_unsub_state_changed:
self._async_unsub_state_changed()
self._async_unsub_state_changed = None
return super().async_remove()
@asyncio.coroutine
def _async_state_changed_listener(self, entity_id, old_state, new_state):
"""Respond to a member state changing.