Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the lock on apply_action #93

Merged
merged 9 commits into from
Jul 2, 2020
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