Skip to content

Commit

Permalink
move to vite
Browse files Browse the repository at this point in the history
  • Loading branch information
hendriksen-mark committed Oct 30, 2024
1 parent 58aa81c commit e4253ea
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 114 deletions.
8 changes: 4 additions & 4 deletions .build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ COPY ./BridgeEmulator/HueEmulator3.py /opt/hue-emulator/
COPY ./BridgeEmulator/githubInstall.sh /opt/hue-emulator/

## Install the web interface
RUN curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip \
&& mkdir diyhueUI \
#RUN curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip \
RUN curl -sL https://github.com/hendriksen-mark/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip \ && mkdir diyhueUI \
&& unzip -qo diyHueUI.zip -d diyhueUI \
&& rm diyHueUI.zip \
&& mv diyhueUI/index.html flaskUI/templates/ \
&& cp -r diyhueUI/static flaskUI/ \
&& mv diyhueUI/dist/index.html flaskUI/templates/ \
&& cp -r diyhueUI/dist/assets flaskUI/ \
&& rm -r diyhueUI

## Copy correct (compiled) C file from previous image
Expand Down
2 changes: 1 addition & 1 deletion BridgeEmulator/HueEmulator3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
logging = logManager.logger.get_logger(__name__)
_ = logManager.logger.get_logger("werkzeug")
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app = Flask(__name__, template_folder='flaskUI/templates',static_url_path="/static", static_folder='flaskUI/static')
app = Flask(__name__, template_folder='flaskUI/templates',static_url_path="/assets", static_folder='flaskUI/assets')
api = Api(app)
cors = CORS(app, resources={r"*": {"origins": "*"}})

Expand Down
28 changes: 20 additions & 8 deletions BridgeEmulator/HueObjects/Light.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ def __init__(self, data):
self.modelid = data["modelid"]
self.id_v1 = data["id_v1"]
self.id_v2 = data["id_v2"] if "id_v2" in data else genV2Uuid()
self.uniqueid = data["uniqueid"] if "uniqueid" in data else generate_unique_id(
)
self.uniqueid = data["uniqueid"] if "uniqueid" in data else generate_unique_id()
self.state = data["state"] if "state" in data else deepcopy(
lightTypes[self.modelid]["state"])
self.protocol = data["protocol"] if "protocol" in data else "dummy"
self.config = data["config"] if "config" in data else deepcopy(
lightTypes[self.modelid]["config"])
self.protocol_cfg = data["protocol_cfg"] if "protocol_cfg" in data else {
}
self.protocol_cfg = data["protocol_cfg"] if "protocol_cfg" in data else {}
self.streaming = False
self.dynamics = deepcopy(lightTypes[self.modelid]["dynamics"])
self.effect = "no_effect"
Expand Down Expand Up @@ -181,10 +179,12 @@ def setV1State(self, state, advertise=True):
except Exception as e:
self.state["reachable"] = False
logging.warning(self.name + " light error, details: %s", e)
return
if advertise:
v2State = v1StateToV2(state)
self.genStreamEvent(v2State)
if "lights" in state:
for item in state["lights"]:
light_state = state["lights"][item]
v2State = v1StateToV2(light_state)
self.genStreamEvent(v2State)

def setV2State(self, state):
v1State = v2StateToV1(state)
Expand Down Expand Up @@ -359,7 +359,19 @@ def getV2Api(self):
result["signaling"] = {"signal_values": [
"no_signal",
"on_off"]}
result["powerup"] = {"preset": "last_on_state"}
result["powerup"] = {
"preset": "last_on_state",
"configured": True,
"on": {
"mode": "on",
"on": {
"on": True
}
},
"dimming": {
"mode": "previous"
}
}
result["service_id"] = self.protocol_cfg["light_nr"]-1 if "light_nr" in self.protocol_cfg else 0
result["type"] = "light"
return result
Expand Down
82 changes: 24 additions & 58 deletions BridgeEmulator/configManager/argumentHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def process_arguments(configDir, args):

def parse_arguments():
argumentDict = {"BIND_IP": '', "HOST_IP": '', "HTTP_PORT": '', "HTTPS_PORT": '', "FULLMAC": '', "MAC": '', "DEBUG": False, "DOCKER": False,
"IP_RANGE_START": '', "IP_RANGE_END": '', "DECONZ": '', "scanOnHostIP": False, "disableOnlineDiscover": '', "noLinkButton": False,
"noServeHttps": False, "TZ": ''}
"scanOnHostIP": False,"noLinkButton": False, "noServeHttps": False}
ap = argparse.ArgumentParser()

# Arguements can also be passed as Environment Variables.
Expand All @@ -49,14 +48,13 @@ def parse_arguments():
ap.add_argument("--https-port", help="The port to listen on for HTTPS (Docker)", type=int)
ap.add_argument("--mac", help="The MAC address of the host system (Docker)", type=str)
ap.add_argument("--no-serve-https", action='store_true', help="Don't listen on port 443 with SSL")
ap.add_argument("--ip-range", help="Set IP range for light discovery. Format: <START_IP>,<STOP_IP>", type=str)
ap.add_argument("--scan-on-host-ip", action='store_true',
help="Scan the local IP address when discovering new lights")
ap.add_argument("--deconz", help="Provide the IP address of your Deconz host. 127.0.0.1 by default.", type=str)
ap.add_argument("--ip-range", help="Depricated use webui, Set IP range for light discovery. Format: <START_IP>,<STOP_IP>", type=str)
ap.add_argument("--sub-ip-range", help="Depricated use webui, Set SUB IP range for light discovery. Format: <START_IP>,<STOP_IP>", type=str)
ap.add_argument("--scan-on-host-ip", action='store_true', help="Scan the local IP address when discovering new lights")
ap.add_argument("--deconz", help="Depricated use webui, Provide the IP address of your Deconz host. 127.0.0.1 by default.", type=str)
ap.add_argument("--no-link-button", action='store_true',
help="DANGEROUS! Don't require the link button to be pressed to pair the Hue app, just allow any app to connect")
ap.add_argument("--disable-online-discover", help="Disable Online and Remote API functions")
ap.add_argument("--TZ", help="Set time zone", type=str)
ap.add_argument("--disable-online-discover", help="Depricated use webui, Disable Online and Remote API functions")

args = ap.parse_args()

Expand All @@ -72,27 +70,22 @@ def parse_arguments():
if args.debug or get_environment_variable('DEBUG', True):
argumentDict["DEBUG"] = True

config_path = '/opt/hue-emulator/config'
if args.config_path:
config_path = args.config_path
elif get_environment_variable('CONFIG_PATH'):
config_path = get_environment_variable('CONFIG_PATH')
else:
config_path = '/opt/hue-emulator/config'
argumentDict["CONFIG_PATH"] = config_path

bind_ip = '0.0.0.0'
if args.bind_ip:
bind_ip = args.bind_ip
elif get_environment_variable('BIND_IP'):
bind_ip = get_environment_variable('BIND_IP')
else:
bind_ip = '0.0.0.0'
argumentDict["BIND_IP"] = bind_ip

tz = "Europe/London"
if args.TZ:
tz = args.TZ
elif get_environment_variable('TZ'):
tz = get_environment_variable('TZ')
argumentDict["TZ"] = tz

if args.ip:
host_ip = args.ip
elif get_environment_variable('IP'):
Expand Down Expand Up @@ -120,11 +113,12 @@ def parse_arguments():
argumentDict["HTTPS_PORT"] = host_https_port

logging.info("Using Host %s:%s" % (host_ip, host_http_port))
logging.info("Using Host %s:%s" % (host_ip, host_https_port))

if args.mac:
if args.mac and str(args.mac).replace(":", "").capitalize != "XXXXXXXXXXXX":
dockerMAC = args.mac # keeps : for cert generation
mac = str(args.mac).replace(":", "")
elif get_environment_variable('MAC'):
elif get_environment_variable('MAC') and get_environment_variable('MAC').strip('\u200e').replace(":", "").capitalize != "XXXXXXXXXXXX":
dockerMAC = get_environment_variable('MAC').strip('\u200e')
mac = str(dockerMAC).replace(":", "")
else:
Expand All @@ -139,49 +133,21 @@ def parse_arguments():
argumentDict["FULLMAC"] = dockerMAC
argumentDict["MAC"] = mac
argumentDict["DOCKER"] = docker
logging.info("Host MAC given as " + mac)
if mac.capitalize == "XXXXXXXXXXXX" or mac == "":
logging.exception("No valid MAC address provided " + str(mac))
logging.exception("To fix this visit: https://diyhue.readthedocs.io/en/latest/getting_started.html")
raise SystemExit("CRITICAL! No valid MAC address provided " + str(mac))
else:
logging.info("Host MAC given as " + mac)

if args.ip_range or get_environment_variable('IP_RANGE'):
if args.ip_range:
ranges = args.ip_range
else:
ranges = get_environment_variable('IP_RANGE')
ranges = ranges.split(',')
if ranges[0] and int(ranges[0]) >= 0:
ip_range_start = int(ranges[0])
else:
ip_range_start = 0
if args.sub_ip_range or get_environment_variable('SUB_IP_RANGE') or get_environment_variable('SUB_IP_RANGE_START') or get_environment_variable('SUB_IP_RANGE_END'):
logging.warn("IP range is depricated in commandline and not active, please setup via webui")

if ranges[1] and int(ranges[1]) > 0:
ip_range_end = int(ranges[1])
else:
ip_range_end = 255
elif get_environment_variable('IP_RANGE_START') and get_environment_variable('IP_RANGE_END'):
ip_range_start = get_environment_variable('IP_RANGE_START')
ip_range_end = get_environment_variable('IP_RANGE_END')
else:
ip_range_start = 0
ip_range_end = 255
argumentDict["IP_RANGE_START"] = ip_range_start
argumentDict["IP_RANGE_END"] = ip_range_end
logging.info("IP range for light discovery: " + str(ip_range_start) + "-" + str(ip_range_end))

if args.deconz:
deconz_ip = args.deconz
elif get_environment_variable('DECONZ'):
deconz_ip = get_environment_variable('DECONZ')
else:
deconz_ip = "127.0.0.1"
argumentDict["DECONZ"] = deconz_ip
logging.info("Deconz IP given as " + deconz_ip)
if args.deconz or get_environment_variable('DECONZ'):
logging.warn("DECONZ is depricated in commandline and not active, please setup via webui")

if args.disable_online_discover or get_environment_variable('disableonlinediscover'):
disableOnlineDiscover = True
logging.info("Online Discovery/Remote API Disabled!")
else:
disableOnlineDiscover = False
logging.info("Online Discovery/Remote API Enabled!")
argumentDict["disableOnlineDiscover"] = disableOnlineDiscover
logging.warn("disableonlinediscover is depricated in commandline and not active, please setup via webui")

if argumentDict['noServeHttps']:
logging.info("HTTPS Port Disabled")
Expand Down
53 changes: 47 additions & 6 deletions BridgeEmulator/configManager/configHandler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from configManager import configInit
from configManager.argumentHandler import parse_arguments
from configManager.argumentHandler import parse_arguments, generate_certificate
import os
import pathlib
import subprocess
import logManager
import yaml
import uuid
import weakref
from copy import deepcopy
from HueObjects import Light, Group, EntertainmentConfiguration, Scene, ApiUser, Rule, ResourceLink, Schedule, Sensor, BehaviorInstance, SmartScene
try:
from time import tzset
Expand All @@ -30,7 +31,8 @@ def _write_yaml(path, contents):

class Config:
yaml_config = None
configDir = parse_arguments()["CONFIG_PATH"]
argsDict = parse_arguments()
configDir = argsDict["CONFIG_PATH"]
runningDir = str(pathlib.Path(__file__)).replace("/configManager/configHandler.py","")

def __init__(self):
Expand All @@ -43,6 +45,9 @@ def load_config(self):
#load config
if os.path.exists(self.configDir + "/config.yaml"):
config = _open_yaml(self.configDir + "/config.yaml")
if "timezone" not in config:
logging.warn("No Time Zone in config, please set Time Zone in webui, default to Europe/London")
config["timezone"] = "Europe/London"
os.environ['TZ'] = config["timezone"]
if tzset is not None:
tzset()
Expand All @@ -51,6 +56,14 @@ def load_config(self):
self.yaml_config["apiUsers"][user] = ApiUser.ApiUser(user, data["name"], data["client_key"], data["create_date"], data["last_use_date"])
del config["whitelist"]
# updgrade config
if "discovery" not in config:
config["discovery"] = False
if "IP_RANGE" not in config:
config["IP_RANGE"] = {
"IP_RANGE_START": 0,
"IP_RANGE_END": 255,
"SUB_IP_RANGE_START": int(self.argsDict["HOST_IP"].split('.')[2]),
"SUB_IP_RANGE_END": int(self.argsDict["HOST_IP"].split('.')[2])}
if "homeassistant" not in config:
config["homeassistant"] = {"enabled": False}
if "yeelight" not in config:
Expand Down Expand Up @@ -110,7 +123,7 @@ def load_config(self):
"name":"DiyHue Bridge",
"netmask":"255.255.255.0",
"swversion":"1965111030",
"timezone":parse_arguments()["TZ"],
"timezone": "Europe/London",
"linkbutton":{"lastlinkbuttonpushed": 1599398980},
"users":{"[email protected]":{"password":"pbkdf2:sha256:150000$bqqXSOkI$199acdaf81c18f6ff2f29296872356f4eb78827784ce4b3f3b6262589c788742"}},
"hue": {},
Expand Down Expand Up @@ -138,7 +151,13 @@ def load_config(self):
"lastchange": "2020-12-13T10:30:15",
"state": "noupdates",
"install": False
}
},
"IP_RANGE": {
"IP_RANGE_START": 0,
"IP_RANGE_END": 255,
"SUB_IP_RANGE_START": int(self.argsDict["HOST_IP"].split('.')[2]),
"SUB_IP_RANGE_END": int(self.argsDict["HOST_IP"].split('.')[2])
}
}
# load lights
if os.path.exists(self.configDir + "/lights.yaml"):
Expand Down Expand Up @@ -287,6 +306,15 @@ def reset_config(self):
self.load_config()
return backup

def remove_cert(self):
try:
os.popen('mv ' + self.configDir + '/cert.pem ' + self.configDir + '/backup/')
logging.info("Certificate removed")
except:
logging.exception("Something went wrong when deleting the certificate")
generate_certificate(self.argsDict["MAC"], self.argsDict["CONFIG_PATH"])
return

def restore_backup(self):
try:
os.popen('rm -r ' + self.configDir + '/*.yaml')
Expand All @@ -308,18 +336,31 @@ def download_log(self):
return self.configDir + "/diyhue_log.tar"

def download_debug(self):
_write_yaml(self.configDir + "/config_debug.yaml", self.yaml_config["config"])
debug = _open_yaml(self.configDir + "/config_debug.yaml")
#_write_yaml(self.configDir + "/config_debug.yaml", self.yaml_config["config"])
#debug = _open_yaml(self.configDir + "/config_debug.yaml")
debug = deepcopy(self.yaml_config["config"])
debug["whitelist"] = "privately"
debug["Hue Essentials key"] = "privately"
debug["users"] = "privately"
if debug["mqtt"]["enabled"]:
debug["mqtt"]["mqttPassword"] = "privately"
if debug["homeassistant"]["enabled"]:
debug["homeassistant"]["homeAssistantToken"] = "privately"
if debug["hue"]:
debug["hue"]["hueUser"] = "privately"
debug["hue"]["hueKey"] = "privately"
if debug["tradfri"]:
debug["tradfri"]["psk"] = "privately"
if debug["alarm"]["enabled"]:
debug["alarm"]["email"] = "privately"
info = {}
info["OS"] = os.uname().sysname
info["Architecture"] = os.uname().machine
info["os_version"] = os.uname().version
info["os_release"] = os.uname().release
info["Hue-Emulator Version"] = subprocess.run("stat -c %y HueEmulator3.py", shell=True, capture_output=True, text=True).stdout.replace("\n", "")
info["WebUI Version"] = subprocess.run("stat -c %y flaskUI/templates/index.html", shell=True, capture_output=True, text=True).stdout.replace("\n", "")
info["arguments"] = self.argsDict
_write_yaml(self.configDir + "/config_debug.yaml", debug)
_write_yaml(self.configDir + "/system_info.yaml", info)
subprocess.run('tar --exclude=' + "'config.yaml'" + ' -cvf ' + self.configDir + '/config_debug.tar ' +
Expand Down
2 changes: 1 addition & 1 deletion BridgeEmulator/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ unzip -qo diyHue.zip
cd diyHue-$branchSelection/BridgeEmulator/

echo -e "\033[36m Installing Python Dependencies.\033[0m"

python3 -m pip install --upgrade pip
pip3 install -r ../requirements.txt --break-system-packages


Expand Down
Loading

0 comments on commit e4253ea

Please sign in to comment.