Skip to content

Commit

Permalink
fix espmega driver type conversion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SiwatS committed Dec 14, 2023
1 parent f520d04 commit 0853b3c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
3 changes: 1 addition & 2 deletions install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ rmdir dist /s /q
python ./setup.py sdist
# Get filename of the tar.gz file
for /f "delims=" %%a in ('dir /b dist\*.tar.gz') do set filename=%%a
pip3 install dist/%filename%
python -m PyInstaller --onefile --noconsole --icon=src/espmega_lightshow/icon.ico --name="ESPMega Lightshow (Portable)" src/espmega_lightshow/__main__.py
pip3 install dist/%filename%
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.8',
version='4.9',
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.8
Version: 4.9
Home-page: https://github.com/SiwatINC/espmega-lightshow
Author: Siwat Sirichai
Author-email: [email protected]
Expand Down
10 changes: 6 additions & 4 deletions src/espmega_lightshow/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ESPMegaLightDriver(LightDriver):

def __init__(self, controller: ESPMega, pwm_channel: int) -> int:
self.controller = controller
self.pwm_channel = pwm_channel
self.pwm_channel = int(pwm_channel)
if controller is None:
self.connected = False
self.exception = "Controller is not connected."
Expand Down Expand Up @@ -215,7 +215,9 @@ def get_controller(self, base_topic: str, light_server: str, light_server_port:
# If there is no connection, create one
self.mqtt_connections[(light_server, light_server_port)] = MQTTClient()
try:
self.mqtt_connections[(light_server, light_server_port)].connect(light_server, light_server_port)
print(f"Connecting to light server at {light_server}:{light_server_port}")
self.mqtt_connections[(light_server, light_server_port)].connect(light_server, int(light_server_port))
print(f"Starting thread for {light_server}:{light_server_port}")
self.mqtt_connections[(light_server, light_server_port)].loop_start()
print(f"Connected to {light_server}:{light_server_port}")
except Exception as e:
Expand Down Expand Up @@ -505,6 +507,7 @@ def read_light_map(self, light_map: list) -> list:
self._validate_light_map(light_map)
# Then we will initialize the light map
self.initialize_light_map(light_map)
print(light_map)
for row_index, row in enumerate(light_map):
for column_index, light in enumerate(row):
# light is a dictionary with fields varying by driver
Expand All @@ -519,7 +522,6 @@ def read_light_map(self, light_map: list) -> list:
continue
# Let's switch on the driver field
driver_type = light["driver"]
print(light)
# If the driver is espmega, we utilize the ESPMegaMultiController to manage the controllers
if driver_type == "espmega":
controller = self.espmega_driver_bank.get_controller(light["base_topic"], light["light_server"], light["light_server_port"])
Expand All @@ -543,7 +545,7 @@ def read_light_map_from_file(self, filename: str):
try:
with open(filename, "r") as file:
light_map = json.load(file)
self.read_light_map(light_map)
self.read_light_map(light_map)
except FileNotFoundError:
raise FileNotFoundError("The light map file does not exist.")
def initialize_light_map(self, light_map):
Expand Down
15 changes: 5 additions & 10 deletions test_universal_driver.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
from src.espmega_lightshow.drivers import UniversalLightGrid
from secrets_storage import ha_api_key
from time import sleep
example_universal_grid_map = [[{"driver":"espmega","light_server":"192.168.0.26","light_server_port":1883,"base_topic":"/espmega/ProR3","pwm_id":0},{"driver":"homeassistant","api_url":"http://192.168.0.25/api","api_key":ha_api_key,"entity_id":"light.laboratory_ceiling_light"}],[{"driver":"espmega","light_server":"192.168.0.26","light_server_port":1883,"base_topic":"/espmega/ProR3","pwm_id":1},{"driver":"homeassistant","api_url":"http://192.168.0.26/api","api_key":ha_api_key,"entity_id":"light.laboratory_light_strip"}]]
map_path = "C:\\Users\\siwat\\Nextcloud\\Documents\\m21_map_emg.json"
grid = UniversalLightGrid()
grid.read_light_map(example_universal_grid_map)
grid.read_light_map_from_file(map_path)

sleep(5)

# Check if light at 0,0 is connected
print(grid.get_light_state(0, 0))
grid.set_light_state(0, 0, True)
print(grid.get_light_state(0, 0))
grid.set_light_state(0, 1, False)
print(grid.get_light_state(0, 1))
grid.set_light_state(1, 0, True)
print(grid.get_light_state(1, 0))
grid.set_light_state(1, 1, False)

# Cycle the light on and off in order to test the driver
# Note that this is a 2x2 grid, we will go clockwise
Expand Down

0 comments on commit 0853b3c

Please sign in to comment.