Skip to content

Commit

Permalink
Merge pull request #11 from edenhaus/async
Browse files Browse the repository at this point in the history
- use async and await
- remove unused asyncio
  • Loading branch information
enkama authored Dec 5, 2021
2 parents 7902c7a + 65bd992 commit 1dc9830
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions custom_components/variable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""variable implementation for Home Assistant."""
import asyncio
import logging

import voluptuous as vol
Expand Down Expand Up @@ -97,25 +96,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(
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)

await entity.async_set_variable(
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}")

Expand Down Expand Up @@ -186,8 +177,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,
Expand Down Expand Up @@ -215,4 +205,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()

0 comments on commit 1dc9830

Please sign in to comment.