Skip to content

Commit

Permalink
Merge pull request #93 from iMicknl/async_apply_action
Browse files Browse the repository at this point in the history
Remove the lock on apply_action
  • Loading branch information
vlebourl authored Jul 2, 2020
2 parents c07dabe + f9f471c commit 75f3e8e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 5 deletions.
4 changes: 4 additions & 0 deletions custom_components/tahoma/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def device_class(self):

def update(self):
"""Update the state."""
if self.should_wait():
self.schedule_update_ha_state(True)
return

self.controller.get_states([self.tahoma_device])

if CORE_CONTACT_STATE in self.tahoma_device.active_states:
Expand Down
4 changes: 4 additions & 0 deletions custom_components/tahoma/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ def update_temp(self, state=None):

def update(self):
"""Update the state."""
if self.should_wait():
self.schedule_update_ha_state(True)
return

self.controller.get_states([self.tahoma_device])
self.update_temp(None)
if self._widget == W_ST:
Expand Down
4 changes: 4 additions & 0 deletions custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def __init__(self, tahoma_device, controller):

def update(self):
"""Update method."""
if self.should_wait():
self.schedule_update_ha_state(True)
return

self.controller.get_states([self.tahoma_device])

# Set current position.
Expand Down
7 changes: 4 additions & 3 deletions custom_components/tahoma/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ def effect(self) -> str:
return self._effect

def update(self):
"""Fetch new state data for this light.
"""Fetch new state data for this light."""

This is the only method that should fetch new data for Home Assistant.
"""
if self.should_wait():
self.schedule_update_ha_state(True)
return

self.controller.get_states([self.tahoma_device])

Expand Down
4 changes: 4 additions & 0 deletions custom_components/tahoma/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def __init__(self, tahoma_device, controller):

def update(self):
"""Update method."""
if self.should_wait():
self.schedule_update_ha_state(True)
return

self.controller.get_states([self.tahoma_device])
self._battery_level = self.tahoma_device.active_states["core:BatteryState"]
self._name = self.tahoma_device.active_states["core:NameState"]
Expand Down
4 changes: 4 additions & 0 deletions custom_components/tahoma/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def device_class(self) -> Optional[str]:

def update(self):
"""Update the state."""
if self.should_wait():
self.schedule_update_ha_state(True)
return

self.controller.get_states([self.tahoma_device])

if CORE_LUMINANCE_STATE in self.tahoma_device.active_states:
Expand Down
4 changes: 4 additions & 0 deletions custom_components/tahoma/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def __init__(self, tahoma_device, controller):
def update(self):
"""Update method."""
# Postpone the immediate state check for changes that take time.
if self.should_wait():
self.schedule_update_ha_state(True)
return

if self._skip_update:
self._skip_update = False
return
Expand Down
11 changes: 9 additions & 2 deletions custom_components/tahoma/tahoma_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, tahoma_device, controller):
self.tahoma_device = tahoma_device
self._name = self.tahoma_device.label
self.controller = controller
self._exec_queue = []

async def async_added_to_hass(self):
"""Entity created."""
Expand Down Expand Up @@ -113,6 +114,12 @@ def device_info(self):
"sw_version": self.tahoma_device.type,
}

def should_wait(self):
"""Wait for actions to finish."""
exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
return True if self._exec_queue else False

async def async_apply_action(self, cmd_name, *args):
"""Apply Action to Device in async context."""
await self.hass.async_add_executor_job(self.apply_action, cmd_name, *args)
Expand All @@ -123,5 +130,5 @@ def apply_action(self, cmd_name, *args):
action = Action(self.tahoma_device.url)
action.add_command(cmd_name, *args)
exec_id = self.controller.apply_actions("HomeAssistant", [action])
while exec_id in self.controller.get_current_executions():
continue
self._exec_queue.append(exec_id)
return exec_id

0 comments on commit 75f3e8e

Please sign in to comment.