Skip to content

Commit

Permalink
Fix switch state hick-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
frimtec committed Jul 4, 2021
1 parent df27da2 commit d4197c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
1 change: 0 additions & 1 deletion custom_components/compal_wifi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""The Compal WiFi component."""
import threading
from datetime import datetime
from datetime import timedelta

from compal_wifi_switch import Commands

Expand Down
18 changes: 8 additions & 10 deletions custom_components/compal_wifi/switch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Support for WiFi switches using Compal modem."""
import threading

from datetime import datetime

from homeassistant.helpers.entity import ToggleEntity
Expand Down Expand Up @@ -74,6 +73,7 @@ def switch_wifi_blocking():
enable_guest = False
if state == Switch.ON:
enable_guest = compal_config.guest
new_processing_state = None
try:
Commands.switch(
compal_config.host,
Expand All @@ -83,9 +83,9 @@ def switch_wifi_blocking():
enable_guest,
compal_config.pause,
)
wifi_switch.set_processing_state("off")
new_processing_state = "off"
except:
wifi_switch.set_processing_state("error")
new_processing_state = "error"
finally:
try:
compal_config.current_modem_state = Commands.status(
Expand All @@ -96,6 +96,7 @@ def switch_wifi_blocking():
except:
compal_config.update_state = "error"
finally:
wifi_switch.set_processing_state(new_processing_state)
compal_config.semaphore.release()

threading.Thread(
Expand All @@ -112,7 +113,6 @@ def __init__(self, config, radio, initial_state):
self._name = f"wifi.{radio}"
self._state = initial_state
self._switch_progress = "off"
self._last_change_time = datetime.now()

def set_processing_state(self, state):
self._switch_progress = state
Expand All @@ -131,23 +131,21 @@ def name(self):
@property
def is_on(self):
"""Return the state of the entity."""
if self._config.last_update > self._last_change_time:
wifi_state = extract_wifi_state(self._config.current_modem_state)
if wifi_state[self._radio.value] != self._state:
self._state = wifi_state[self._radio.value]
if self._switch_progress != "on":
self._state = extract_wifi_state(self._config.current_modem_state)[
self._radio.value
]
return self._state

def turn_on(self, **kwargs):
"""Turn the device on."""
switch_wifi(self, Switch.ON, self._radio)
self._state = True
self._last_change_time = datetime.now()

def turn_off(self, **kwargs):
"""Turn the device off."""
switch_wifi(self, Switch.OFF, self._radio)
self._state = False
self._last_change_time = datetime.now()

@property
def device_state_attributes(self):
Expand Down

0 comments on commit d4197c6

Please sign in to comment.