From 24404897d161550f472019b5b55e1a44b87eae8f Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Sun, 5 Dec 2021 19:26:46 +0100 Subject: [PATCH 1/2] use async and await --- custom_components/variable/__init__.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/custom_components/variable/__init__.py b/custom_components/variable/__init__.py index 47b80ab..ecd3a6c 100644 --- a/custom_components/variable/__init__.py +++ b/custom_components/variable/__init__.py @@ -97,25 +97,17 @@ async def async_setup(hass, config): Variable(variable_id, name, value, attributes, restore, force_update) ) - @asyncio.coroutine - def async_set_variable_service(call): + async def async_set_variable_service(call): """Handle calls to the set_variable service.""" entity_id = ENTITY_ID_FORMAT.format(call.data.get(ATTR_VARIABLE)) entity = component.get_entity(entity_id) if entity: - target_variables = [entity] - tasks = [ - variable.async_set_variable( + await entity.async_set_variable( call.data.get(ATTR_VALUE), call.data.get(ATTR_ATTRIBUTES), call.data.get(ATTR_REPLACE_ATTRIBUTES, False), - ) - for variable in target_variables - ] - if tasks: - yield from asyncio.wait(tasks, loop=hass.loop) - + ) else: _LOGGER.warning(f"Failed to set unknown variable: {entity_id}") @@ -186,8 +178,7 @@ def force_update(self) -> bool: """Force update""" return self._force_update - @asyncio.coroutine - def async_set_variable( + async def async_set_variable( self, value, attributes, @@ -215,4 +206,4 @@ def async_set_variable( if updated_value is not None: self._value = updated_value - yield from self.async_update_ha_state() + await self.async_update_ha_state() From 65bd992f517f01fff9ca28028966bc423a524817 Mon Sep 17 00:00:00 2001 From: Wibias <37517432+Wibias@users.noreply.github.com> Date: Sun, 5 Dec 2021 21:32:16 +0100 Subject: [PATCH 2/2] Change so flake8 rules go trough --- custom_components/variable/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/custom_components/variable/__init__.py b/custom_components/variable/__init__.py index ecd3a6c..1eee279 100644 --- a/custom_components/variable/__init__.py +++ b/custom_components/variable/__init__.py @@ -1,5 +1,4 @@ """variable implementation for Home Assistant.""" -import asyncio import logging import voluptuous as vol @@ -104,9 +103,9 @@ async def async_set_variable_service(call): if entity: await entity.async_set_variable( - call.data.get(ATTR_VALUE), - call.data.get(ATTR_ATTRIBUTES), - call.data.get(ATTR_REPLACE_ATTRIBUTES, False), + call.data.get(ATTR_VALUE), + call.data.get(ATTR_ATTRIBUTES), + call.data.get(ATTR_REPLACE_ATTRIBUTES, False), ) else: _LOGGER.warning(f"Failed to set unknown variable: {entity_id}")