Skip to content

Commit

Permalink
ha threaded driver
Browse files Browse the repository at this point in the history
  • Loading branch information
SiwatS committed Dec 13, 2023
1 parent 76fdf03 commit fc805d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def readme():

setup(
name='espmega_lightshow',
version='4.1',
version='4.3',
license='Apache 2.0',
author="Siwat Sirichai",
author_email='[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion src/espmega_lightshow.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: espmega-lightshow
Version: 4.1
Version: 4.3
Home-page: https://github.com/SiwatINC/espmega-lightshow
Author: Siwat Sirichai
Author-email: [email protected]
Expand Down
22 changes: 16 additions & 6 deletions src/espmega_lightshow/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant_api import Client as HomeAssistantClient
from homeassistant_api import errors as HomeAssistantErrors
from paho.mqtt.client import Client as MQTTClient
from threading import Thread

# This is the base class for all physical light drivers
class LightDriver(ABC):
Expand Down Expand Up @@ -244,6 +245,7 @@ def __init__(self, ha: HomeAssistantClient, entity_id: str):
# Connect to Home Assistant
self.ha = ha
self.entity_id = entity_id
self.command_waiting = False
try:
self.light_api = ha.get_domain("light")
except Exception as e:
Expand All @@ -257,14 +259,22 @@ def __init__(self, ha: HomeAssistantClient, entity_id: str):
return
self.connected = True

def _set_state_thread(self, state: bool):
if self.command_waiting:
return
try:
self.command_waiting = True
self.light_api.turn_on(entity_id=self.entity_id) if state else self.light_api.turn_off(entity_id=self.entity_id)
self.command_waiting = False
except Exception as e:
print(e)
self.connected = False
self.exception = e
self.command_waiting = False

def set_light_state(self, state: bool) -> None:
self.state = state
if self.connected:
try:
self.light_api.turn_on(entity_id=self.entity_id) if state else self.light_api.turn_off(entity_id=self.entity_id)
except Exception as e:
print(e)
self.connected = False
Thread(target=self._set_state_thread, args=(state,)).start()

def get_light_state(self) -> bool:
return self.state_to_multistate(self.state)
Expand Down

0 comments on commit fc805d7

Please sign in to comment.