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
6 changes: 6 additions & 0 deletions custom_components/tahoma/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def device_class(self):

def update(self):
"""Update the state."""
exec_queue = self.controller.get_current_executions()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is the same in all implementations? Should we move it to the tahoma_device and call this function from here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could take this form:

    def should_wait(self):
        """Wait for actions to finish (this would be in tahoma_device"""
        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

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

self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
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
6 changes: 6 additions & 0 deletions custom_components/tahoma/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ def update_temp(self, state=None):

def update(self):
"""Update the state."""
exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
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
6 changes: 6 additions & 0 deletions custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def __init__(self, tahoma_device, controller):

def update(self):
"""Update method."""
exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
self.schedule_update_ha_state(True)
return

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

# Set current position.
Expand Down
9 changes: 6 additions & 3 deletions custom_components/tahoma/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ 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.
"""
exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
self.schedule_update_ha_state(True)
return

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

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

def update(self):
"""Update method."""
exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
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
6 changes: 6 additions & 0 deletions custom_components/tahoma/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def device_class(self) -> Optional[str]:

def update(self):
"""Update the state."""
exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
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
6 changes: 6 additions & 0 deletions custom_components/tahoma/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def update(self):
self._skip_update = False
return

exec_queue = self.controller.get_current_executions()
self._exec_queue = [e for e in self._exec_queue if e in exec_queue]
if self._exec_queue:
self.schedule_update_ha_state(True)
return

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

_LOGGER.debug("Update %s, state: %s", self._name, self._state)
Expand Down
5 changes: 3 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 @@ -123,5 +124,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