Skip to content

Commit

Permalink
ha api is now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
SiwatS committed Dec 13, 2023
1 parent fc805d7 commit 641cecc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
5 changes: 2 additions & 3 deletions 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.3',
version='4.5',
license='Apache 2.0',
author="Siwat Sirichai",
author_email='[email protected]',
Expand All @@ -22,8 +22,7 @@ def readme():
keywords='light mqtt espmega',
install_requires=[
'espmega',
'pillow',
'homeassistant-api'
'pillow'
],

)
4 changes: 3 additions & 1 deletion src/espmega_lightshow.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Metadata-Version: 2.1
Name: espmega-lightshow
Version: 4.3
Version: 4.5
Home-page: https://github.com/SiwatINC/espmega-lightshow
Author: Siwat Sirichai
Author-email: [email protected]
License: Apache 2.0
Keywords: light mqtt espmega
Description-Content-Type: text/markdown
Requires-Dist: espmega
Requires-Dist: pillow

# ESPMega Light Show
This is a program made for the ESPMega PLCs for easily programming light show and running light show script
Expand Down
1 change: 0 additions & 1 deletion src/espmega_lightshow.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
espmega
pillow
homeassistant-api
27 changes: 19 additions & 8 deletions src/espmega_lightshow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from tkinter import ttk
import json
from tkinter import filedialog
from espmega_lightshow.drivers import UniversalLightGrid, LightDriver, ESPMegaLightDriver, HomeAssistantLightDriver
from espmega_lightshow.drivers import UniversalLightGrid, LightDriver, ESPMegaLightDriver, HomeAssistantLightDriver, PackageManager, DriverDependencyNotMetError
from dataclasses import dataclass
import sys
import json
Expand Down Expand Up @@ -294,10 +294,23 @@ def color_to_state(color: str):
else:
return LIGHT_DISABLED

def load_light_grid():
global light_grid
try:
light_grid = LightGrid(design_mode=design_mode)
light_grid.read_light_map_from_file(filename=light_map_file)
except FileNotFoundError:
messagebox.showerror(
MSG_FILE_NOT_FOUND_TITLE, "The light map file could not be found.")
except json.decoder.JSONDecodeError:
messagebox.showerror(
MSG_LOAD_ERROR_TITLE, "The light map file is corrupted.")
except DriverDependencyNotMetError as e:
messagebox.showerror(MSG_LOAD_ERROR_TITLE, e)


# Load light map from light_map.json
light_grid = LightGrid(design_mode=design_mode)
light_grid.read_light_map_from_file(filename=light_map_file)
load_light_grid()
rows = light_grid.rows
columns = light_grid.columns

Expand Down Expand Up @@ -512,7 +525,7 @@ def render_frame(frame: list):

def change_light_config(event):
# Get a list of all configuration options for each light driver
config_options = {"disabled":[],"espmega": ESPMegaLightDriver.get_driver_properties()["configuration_parameters"], "homeassistant": HomeAssistantLightDriver.get_driver_properties()["configuration_parameters"]}
config_options = PackageManager.get_config_options()
config_vars = {}
config_frames = {}
global script_active
Expand Down Expand Up @@ -559,8 +572,7 @@ def submit_light_config():
json.dump(light_grid.light_map, file)

# Reload the light_grid
light_grid = LightGrid(design_mode=design_mode)
light_grid.read_light_map_from_file(filename=light_map_file)
load_light_grid()

render_frame_at_index(slider.get())
root.update()
Expand Down Expand Up @@ -690,8 +702,7 @@ def reconnect_light_controllers():
global light_grid
global design_mode
old_light_map = light_grid.light_map
light_grid = LightGrid(design_mode=design_mode)
light_grid.read_light_map(old_light_map)
load_light_grid()
render_frame_at_index(slider.get())
root.update()

Expand Down
37 changes: 35 additions & 2 deletions src/espmega_lightshow/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@
from espmega.espmega_r3 import ESPMega_standalone, ESPMega
import json
from typing import Optional
from homeassistant_api import Client as HomeAssistantClient
from homeassistant_api import errors as HomeAssistantErrors
homeassistant_api_available = True
try :
from homeassistant_api import Client as HomeAssistantClient
from homeassistant_api import errors as HomeAssistantErrors
except ImportError:
homeassistant_api_available = False
HomeAssistantClient = any
HomeAssistantErrors = any
from paho.mqtt.client import Client as MQTTClient
from threading import Thread

class DriverDependencyNotMetError(Exception):
pass

class PackageManager:
@staticmethod
def get_available_drivers() -> dict:
# This function should return a dictionary of driver names to driver classes
# The driver classes should be subclasses of LightDriver
# The driver names should be strings
# The driver classes should be able to be initialized without any parameters
drivers = {}
driver['disabled'] = []
# Check if homeassistant_api is available
if homeassistant_api_available:
drivers["homeassistant"] = HomeAssistantLightDriver
drivers["espmega"] = ESPMegaLightDriver
@staticmethod
def get_config_options():
config_option = {}
config_option["disabled"] = []
if homeassistant_api_available:
config_option["homeassistant"] = HomeAssistantLightDriver.get_driver_properties()["configuration_parameters"]
config_option["espmega"] = ESPMegaLightDriver.get_driver_properties()["configuration_parameters"]
return config_option
# This is the base class for all physical light drivers
class LightDriver(ABC):
LIGHT_STATE_OFF = 0
Expand Down Expand Up @@ -293,6 +323,9 @@ def __init__(self):
# Dictionary of a tuple of api url and api key to Home Assistant client
self.ha_clients = {}
def get_ha_client(self, api_url: str, api_key: str) -> HomeAssistantClient:
# If homeassistant_api is not available, raise an exception
if not homeassistant_api_available:
raise DriverDependencyNotMetError("The homeassistant_api package is not installed but is used in the loaded light map.\nPlease install the package by running 'pip install homeassistant_api'\nNote that this requires Visual Studio Build Tools 2015 to be installed.")
# Check if there is a Home Assistant client for the api url and api key
if (api_url, api_key) not in self.ha_clients:
# If there is no Home Assistant client, create one
Expand Down

0 comments on commit 641cecc

Please sign in to comment.