Convert more files to async/await syntax (#14142)
* Move more files to async/await syntax * Attempt Work around pylint bug Using lazytox :P
This commit is contained in:
1 parent
a0b14c2913
commit
a4bf421044
27 files changed
+229
-327
No files matched your search
@@ -245,34 +245,31 @@ def get_entity_ids(hass, entity_id, domain_filter=None):
|
||||
if ent_id.startswith(domain_filter)]
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
async def async_setup(hass, config):
|
||||
"""Set up all groups found defined in the configuration."""
|
||||
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)
|
||||
await _async_process_config(hass, config, component)
|
||||
|
||||
@asyncio.coroutine
|
||||
def reload_service_handler(service):
|
||||
async def reload_service_handler(service):
|
||||
"""Remove all user-defined groups and load new ones from config."""
|
||||
auto = list(filter(lambda e: not e.user_defined, component.entities))
|
||||
|
||||
conf = yield from component.async_prepare_reload()
|
||||
conf = await component.async_prepare_reload()
|
||||
if conf is None:
|
||||
return
|
||||
yield from _async_process_config(hass, conf, component)
|
||||
await _async_process_config(hass, conf, component)
|
||||
|
||||
yield from component.async_add_entities(auto)
|
||||
await component.async_add_entities(auto)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_RELOAD, reload_service_handler,
|
||||
schema=RELOAD_SERVICE_SCHEMA)
|
||||
|
||||
@asyncio.coroutine
|
||||
def groups_service_handler(service):
|
||||
async def groups_service_handler(service):
|
||||
"""Handle dynamic group service functions."""
|
||||
object_id = service.data[ATTR_OBJECT_ID]
|
||||
entity_id = ENTITY_ID_FORMAT.format(object_id)
|
||||
@@ -287,7 +284,7 @@ def async_setup(hass, config):
|
||||
ATTR_VISIBLE, ATTR_ICON, ATTR_VIEW, ATTR_CONTROL
|
||||
) if service.data.get(attr) is not None}
|
||||
|
||||
yield from Group.async_create_group(
|
||||
await Group.async_create_group(
|
||||
hass, service.data.get(ATTR_NAME, object_id),
|
||||
object_id=object_id,
|
||||
entity_ids=entity_ids,
|
||||
@@ -308,11 +305,11 @@ def async_setup(hass, config):
|
||||
if ATTR_ADD_ENTITIES in service.data:
|
||||
delta = service.data[ATTR_ADD_ENTITIES]
|
||||
entity_ids = set(group.tracking) | set(delta)
|
||||
yield from group.async_update_tracked_entity_ids(entity_ids)
|
||||
await group.async_update_tracked_entity_ids(entity_ids)
|
||||
|
||||
if ATTR_ENTITIES in service.data:
|
||||
entity_ids = service.data[ATTR_ENTITIES]
|
||||
yield from group.async_update_tracked_entity_ids(entity_ids)
|
||||
await group.async_update_tracked_entity_ids(entity_ids)
|
||||
|
||||
if ATTR_NAME in service.data:
|
||||
group.name = service.data[ATTR_NAME]
|
||||
@@ -335,13 +332,13 @@ def async_setup(hass, config):
|
||||
need_update = True
|
||||
|
||||
if need_update:
|
||||
yield from group.async_update_ha_state()
|
||||
await group.async_update_ha_state()
|
||||
|
||||
return
|
||||
|
||||
# remove group
|
||||
if service.service == SERVICE_REMOVE:
|
||||
yield from component.async_remove_entity(entity_id)
|
||||
await component.async_remove_entity(entity_id)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_SET, groups_service_handler,
|
||||
@@ -351,8 +348,7 @@ def async_setup(hass, config):
|
||||
DOMAIN, SERVICE_REMOVE, groups_service_handler,
|
||||
schema=REMOVE_SERVICE_SCHEMA)
|
||||
|
||||
@asyncio.coroutine
|
||||
def visibility_service_handler(service):
|
||||
async def visibility_service_handler(service):
|
||||
"""Change visibility of a group."""
|
||||
visible = service.data.get(ATTR_VISIBLE)
|
||||
|
||||
@@ -363,7 +359,7 @@ def async_setup(hass, config):
|
||||
tasks.append(group.async_update_ha_state())
|
||||
|
||||
if tasks:
|
||||
yield from asyncio.wait(tasks, loop=hass.loop)
|
||||
await asyncio.wait(tasks, loop=hass.loop)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_SET_VISIBILITY, visibility_service_handler,
|
||||
@@ -372,8 +368,7 @@ def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def _async_process_config(hass, config, component):
|
||||
async def _async_process_config(hass, config, component):
|
||||
"""Process group configuration."""
|
||||
for object_id, conf in config.get(DOMAIN, {}).items():
|
||||
name = conf.get(CONF_NAME, object_id)
|
||||
@@ -384,7 +379,7 @@ def _async_process_config(hass, config, component):
|
||||
|
||||
# Don't create tasks and await them all. The order is important as
|
||||
# groups get a number based on creation order.
|
||||
yield from Group.async_create_group(
|
||||
await Group.async_create_group(
|
||||
hass, name, entity_ids, icon=icon, view=view,
|
||||
control=control, object_id=object_id)
|
||||
|
||||
@@ -428,10 +423,9 @@ class Group(Entity):
|
||||
hass.loop).result()
|
||||
|
||||
@staticmethod
|
||||
@asyncio.coroutine
|
||||
def async_create_group(hass, name, entity_ids=None, user_defined=True,
|
||||
visible=True, icon=None, view=False, control=None,
|
||||
object_id=None):
|
||||
async def async_create_group(hass, name, entity_ids=None,
|
||||
user_defined=True, visible=True, icon=None,
|
||||
view=False, control=None, object_id=None):
|
||||
"""Initialize a group.
|
||||
|
||||
This method must be run in the event loop.
|
||||
@@ -453,7 +447,7 @@ class Group(Entity):
|
||||
component = hass.data[DOMAIN] = \
|
||||
EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
yield from component.async_add_entities([group], True)
|
||||
await component.async_add_entities([group], True)
|
||||
|
||||
return group
|
||||
|
||||
@@ -520,17 +514,16 @@ class Group(Entity):
|
||||
self.async_update_tracked_entity_ids(entity_ids), self.hass.loop
|
||||
).result()
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_update_tracked_entity_ids(self, entity_ids):
|
||||
async def async_update_tracked_entity_ids(self, entity_ids):
|
||||
"""Update the member entity IDs.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
yield from self.async_stop()
|
||||
await self.async_stop()
|
||||
self.tracking = tuple(ent_id.lower() for ent_id in entity_ids)
|
||||
self.group_on, self.group_off = None, None
|
||||
|
||||
yield from self.async_update_ha_state(True)
|
||||
await self.async_update_ha_state(True)
|
||||
self.async_start()
|
||||
|
||||
@callback
|
||||
@@ -544,8 +537,7 @@ class Group(Entity):
|
||||
self.hass, self.tracking, self._async_state_changed_listener
|
||||
)
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_stop(self):
|
||||
async def async_stop(self):
|
||||
"""Unregister the group from Home Assistant.
|
||||
|
||||
This method must be run in the event loop.
|
||||
@@ -554,27 +546,24 @@ class Group(Entity):
|
||||
self._async_unsub_state_changed()
|
||||
self._async_unsub_state_changed = None
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_update(self):
|
||||
async def async_update(self):
|
||||
"""Query all members and determine current group state."""
|
||||
self._state = STATE_UNKNOWN
|
||||
self._async_update_group_state()
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_added_to_hass(self):
|
||||
async def async_added_to_hass(self):
|
||||
"""Callback when added to HASS."""
|
||||
if self.tracking:
|
||||
self.async_start()
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_will_remove_from_hass(self):
|
||||
async 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
|
||||
|
||||
@asyncio.coroutine
|
||||
def _async_state_changed_listener(self, entity_id, old_state, new_state):
|
||||
async def _async_state_changed_listener(self, entity_id, old_state,
|
||||
new_state):
|
||||
"""Respond to a member state changing.
|
||||
|
||||
This method must be run in the event loop.
|
||||
@@ -584,7 +573,7 @@ class Group(Entity):
|
||||
return
|
||||
|
||||
self._async_update_group_state(new_state)
|
||||
yield from self.async_update_ha_state()
|
||||
await self.async_update_ha_state()
|
||||
|
||||
@property
|
||||
def _tracking_states(self):
|
||||
|
||||
Reference in new issue
Block a user