Skip to content

Commit

Permalink
Add update (#1002)
Browse files Browse the repository at this point in the history
* Update discover.py

* Update configHandler.py

* end wake up

* Update v2restapi.py

Add behavior instance to clip/v2/resource, to solve problem as in issue 873, now the app shows the automations

* wrong response

* add all buttons to response

* Update discover.py

Fix issue where lights with the same mac address but different modelid get the same ip address

* update wake-up

fix error "Exception while processing the schedule X | 'turn_lights_off_after'" if "turn_lights_off_after" is not specified

* fix add light in hue app

* Update requirements.txt

* Update requirements.txt

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update requirements.txt

* Update requirements.txt

* Update Dockerfile

* Update requirements.txt

* Update Dockerfile

* revert to old working

* Update Dockerfile

* Add identify request, non standard gateway, update version and api

* add easter egg

* Update Credits.py

* fixes, adds

fix typo and add update process

* Create updates_test.txt

* Delete updates_test.txt

* add version update

* Update Dockerfile

* Update restful.py

* Create update_test.txt

* Delete update_test.txt

* Create githubresponse.json

* Update githubresponse.json

* Update githubresponse.json

* Update updateManager.py

* Update updateManager.py

* add update

* Update updateManager.py

* Update githubInstall.sh

* Update updateManager.py

* combine code

* Update updateManager.py

* fix

* Update githubInstall.sh

* Update githubInstall.sh

* Update githubInstall.sh

* u

* Update githubInstall.sh

* Update scheduler.py

* Update updateManager.py
  • Loading branch information
hendriksen-mark authored Mar 3, 2024
1 parent 571a0e3 commit e72c095
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ COPY ./BridgeEmulator/services/ /opt/hue-emulator/services/
COPY ./BridgeEmulator/configManager/ /opt/hue-emulator/configManager/
COPY ./BridgeEmulator/logManager/ /opt/hue-emulator/logManager/
COPY ./BridgeEmulator/HueEmulator3.py /opt/hue-emulator/
COPY ./BridgeEmulator/githubInstall.sh /opt/hue-emulator/
COPY ./BridgeEmulator/githubUIInstall.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 \
Expand Down
27 changes: 27 additions & 0 deletions BridgeEmulator/configManager/configHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def load_config(self):
config["port"] = {"enabled": False,"ports": [80]}
if "zigbee_device_discovery_info" not in config:
config["zigbee_device_discovery_info"] = {"status": "ready"}
if "swupdate2" not in config:
config["swupdate2"] = {"autoinstall": {
"on": True,
"updatetime": "T14:00:00"
},
"bridge": {
"lastinstall": "2020-12-11T17:08:55",
"state": "noupdates"
},
"checkforupdate": False,
"lastchange": "2020-12-13T10:30:15",
"state": "unknown",
"install": False
}

if int(config["swversion"]) < 1958077010:
config["swversion"] = "1962154010"
Expand Down Expand Up @@ -111,6 +125,19 @@ def load_config(self):
"tpkasa": {"enabled":True},
"elgato": {"enabled":True},
"zigbee_device_discovery_info": {"status": "ready"},
"swupdate2": { "autoinstall": {
"on": True,
"updatetime": "T14:00:00"
},
"bridge": {
"lastinstall": "2020-12-11T17:08:55",
"state": "noupdates"
},
"checkforupdate": False,
"lastchange": "2020-12-13T10:30:15",
"state": "unknown",
"install": False
}
}
# load lights
if os.path.exists(self.configDir + "/lights.yaml"):
Expand Down
7 changes: 7 additions & 0 deletions BridgeEmulator/flaskUI/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from lights.light_types import lightTypes
from subprocess import check_output
from pprint import pprint
import os
import logManager
logging = logManager.logger.get_logger(__name__)
bridgeConfig = configManager.bridgeConfig.yaml_config
core = Blueprint('core',__name__)
@core.route('/')
Expand Down Expand Up @@ -81,6 +84,10 @@ def save_config():
configManager.bridgeConfig.save_config()
return "config saved"

@core.route('/reboot')
def reboot():
logging.info("start reboot")
os._exit(0)

@core.route('/login', methods=['GET', 'POST'])
def login():
Expand Down
10 changes: 9 additions & 1 deletion BridgeEmulator/flaskUI/restful.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from flask import request
from functions.rules import rulesProcessor
from services.entertainment import entertainmentService
from services.updateManager import githubCheck, versionCheck, githubInstall

try:
from time import tzset
Expand Down Expand Up @@ -55,7 +56,7 @@ def buildConfig():
result = staticConfig()
config = bridgeConfig["config"]
result.update({"Hue Essentials key": config["Hue Essentials key"], "Remote API enabled": config["Remote API enabled"], "apiversion": config["apiversion"], "bridgeid": config["bridgeid"],
"ipaddress": config["ipaddress"], "netmask": config["netmask"], "gateway": config["gateway"], "mac": config["mac"], "name": config["name"], "swversion": config["swversion"], "timezone": config["timezone"]})
"ipaddress": config["ipaddress"], "netmask": config["netmask"], "gateway": config["gateway"], "mac": config["mac"], "name": config["name"], "swversion": config["swversion"], "swupdate2": config["swupdate2"], "timezone": config["timezone"]})
result["UTC"] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
result["localtime"] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
result["whitelist"] = {}
Expand Down Expand Up @@ -269,6 +270,13 @@ def put(self, username, resource):
else:
bridgeConfig[resource][key] = value

if resource == "config" and "swupdate2" in putDict:
if "checkforupdate" in putDict["swupdate2"] and putDict["swupdate2"]["checkforupdate"] == True:
versionCheck()
githubCheck()
if "install" in putDict["swupdate2"] and putDict["swupdate2"]["install"] == True:
githubInstall()

# build response list
responseList = []
response_location = "/" + resource + "/"
Expand Down
1 change: 1 addition & 0 deletions BridgeEmulator/functions/request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from time import sleep

def sendRequest(url, method, data, timeout=3, delay=0):
if delay != 0:
Expand Down
47 changes: 47 additions & 0 deletions BridgeEmulator/githubInstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
curl -s localhost/save
cd /
[ ! -d diyhue ] && mkdir diyhue
curl -s -J -L -o $diyhue.tar.gz "https://github.com/diyhue/diyHue/archive/refs/heads/master.tar.gz"
tar xzf $diyhue.tar.gz --strip-components=1 -C diyhue
#cd diyhue
[ -d /opt/hue-emulator/flaskUI ] && rm -r /opt/hue-emulator/flaskUI
mv diyhue/BridgeEmulator/flaskUI /opt/hue-emulator/

[ -d /opt/hue-emulator/functions ] && rm -r /opt/hue-emulator/functions
mv diyhue/BridgeEmulator/functions /opt/hue-emulator/

[ -d /opt/hue-emulator/lights ] && rm -r /opt/hue-emulator/lights
mv diyhue/BridgeEmulator/lights /opt/hue-emulator/

[ -d /opt/hue-emulator/sensors ] && rm -r /opt/hue-emulator/sensors
mv diyhue/BridgeEmulator/sensors /opt/hue-emulator/

[ -d /opt/hue-emulator/HueObjects ] && rm -r /opt/hue-emulator/HueObjects
mv diyhue/BridgeEmulator/HueObjects /opt/hue-emulator/

[ -d /opt/hue-emulator/services ] && rm -r /opt/hue-emulator/services
mv diyhue/BridgeEmulator/services /opt/hue-emulator

[ -d /opt/hue-emulator/configManager ] && rm -r /opt/hue-emulator/configManager
mv diyhue/BridgeEmulator/configManager /opt/hue-emulator/

[ -d /opt/hue-emulator/logManager ] && rm -r /opt/hue-emulator/logManager
mv diyhue/BridgeEmulator/logManager /opt/hue-emulator/

mv diyhue/BridgeEmulator/HueEmulator3.py /opt/hue-emulator/
mv diyhue/BridgeEmulator/githubInstall.sh /opt/hue-emulator/
mv diyhue/BridgeEmulator/githubUIInstall.sh /opt/hue-emulator/
mv diyhue/BridgeEmulator/genCert.sh /opt/hue-emulator/
mv diyhue/BridgeEmulator/openssl.conf /opt/hue-emulator/
chmod +x /opt/hue-emulator/genCert.sh

#cd /
[ ! -d /diyhueUI ] && mkdir diyhueUI
curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip
unzip -qo diyHueUI.zip -d diyhueUI
#cd diyhueUI
mv diyhueUI/index.html /opt/hue-emulator/flaskUI/templates/
[ -d /opt/hue-emulator/flaskUI/static ] && rm -r /opt/hue-emulator/flaskUI/static
mv diyhueUI/static /opt/hue-emulator/flaskUI/

curl -s "localhost.lan/reboot"
11 changes: 11 additions & 0 deletions BridgeEmulator/githubUIInstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
curl -s localhost/save
cd /
[ ! -d /diyhueUI ] && mkdir diyhueUI
curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip
unzip -qo diyHueUI.zip -d diyhueUI
cd diyhueUI
mv index.html /opt/hue-emulator/flaskUI/templates/
[ -d /opt/hue-emulator/flaskUI/static ] && rm -r /opt/hue-emulator/flaskUI/static
mv static /opt/hue-emulator/flaskUI/

curl -s localhost/reboot
2 changes: 1 addition & 1 deletion BridgeEmulator/services/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def received_message(self, m):
del message["state"]["dark"]

if bridgeSensor.modelid == "SML001" and "lightlevel" in message["state"]:
if message["state"][lightlevel] > bridgeSensor.config["tholddark"]:
if message["state"]["lightlevel"] > bridgeSensor.config["tholddark"]:
message["state"]["dark"] = False
else:
message["state"]["dark"] = True
Expand Down
1 change: 1 addition & 0 deletions BridgeEmulator/services/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sensors.sensor_types import sensorTypes
from lights.discover import addNewLight
from functions.rules import rulesProcessor
import requests

logging = logManager.logger.get_logger(__name__)
bridgeConfig = configManager.bridgeConfig.yaml_config
Expand Down
4 changes: 4 additions & 0 deletions BridgeEmulator/services/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from functions.request import sendRequest
from functions.daylightSensor import daylightSensor
from functions.scripts import triggerScript
from services.updateManager import versionCheck, githubCheck

bridgeConfig = configManager.bridgeConfig.yaml_config
logging = logManager.logger.get_logger(__name__)
Expand Down Expand Up @@ -104,6 +105,9 @@ def runScheduler():
if (datetime.now().strftime("%M:%S") == "00:10"): #auto save configuration every hour
configManager.bridgeConfig.save_config()
Thread(target=daylightSensor, args=[bridgeConfig["config"]["timezone"], bridgeConfig["sensors"]["1"]]).start()
if (datetime.now().strftime("%H") == "23"): #check for updates every day at 23:00:10
versionCheck()
githubCheck()
if (datetime.now().strftime("%H") == "23" and datetime.now().strftime("%A") == "Sunday"): #backup config every Sunday at 23:00:10
configManager.bridgeConfig.save_config(backup=True)
sleep(1)
98 changes: 98 additions & 0 deletions BridgeEmulator/services/updateManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import requests
import configManager
import logManager
import json
from datetime import datetime, timezone
import subprocess

bridgeConfig = configManager.bridgeConfig.yaml_config
logging = logManager.logger.get_logger(__name__)

def versionCheck():
swversion = bridgeConfig["config"]["swversion"]
url = "https://firmware.meethue.com/v1/checkupdate/?deviceTypeId=BSB002&version=" + swversion
response = requests.get(url)
if response.status_code == 200:
device_data = json.loads(response.text)
if len(device_data["updates"]) != 0:
new_version = str(device_data["updates"][len(device_data["updates"])-1]["version"])
new_versionName = str(device_data["updates"][len(device_data["updates"])-1]["versionName"])
if new_version > swversion:
logging.info("swversion number update from Philips, old: " + swversion + " new:" + new_version)
bridgeConfig["config"]["swversion"] = new_version
bridgeConfig["config"]["apiversion"] = new_versionName
bridgeConfig["config"]["swupdate2"]["lastchange"] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
bridgeConfig["config"]["swupdate2"]["bridge"]["lastinstall"] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
else:
logging.info("swversion higher than Philips")
else:
logging.info("no swversion number update from Philips")

def githubCheck():
#creation_time = "2024-02-18 19:50:15.000000000 +0100\n"
creation_time = subprocess.run("stat -c %y HueEmulator3.py", shell=True, capture_output=True, text=True)#2024-02-18 19:50:15.000000000 +0100\n
creation_time_arg1 = creation_time.stdout.replace(".", " ").split(" ")#2024-02-18, 19:50:15, 000000000, +0100\n
creation_time = creation_time_arg1[0] + " " + creation_time_arg1[1] + " " + creation_time_arg1[3].replace("\n", "")#2024-02-18 19:50:15 +0100
creation_time = datetime.strptime(creation_time, "%Y-%m-%d %H:%M:%S %z").astimezone(timezone.utc).strftime("%Y-%m-%d %H")#2024-02-18 18

url = "https://api.github.com/repos/diyhue/diyhue/branches/master"
#url = "https://api.github.com/repos/hendriksen-mark/diyhue/branches/master"
response = requests.get(url)
if response.status_code == 200:
device_data = json.loads(response.text)
publish_time = datetime.strptime(device_data["commit"]["commit"]["author"]["date"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H")

logging.info("creation_time diyHue : " + str(creation_time))
logging.info("publish_time diyHue : " + str(publish_time))

if publish_time > creation_time:
logging.info("update on github")
bridgeConfig["config"]["swupdate2"]["state"] = "allreadytoinstall"
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "allreadytoinstall"
elif githubUICheck() == True:
logging.info("UI update on github")
bridgeConfig["config"]["swupdate2"]["state"] = "anyreadytoinstall"
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "anyreadytoinstall"
else:
logging.info("no update for diyHue or UI on github")
bridgeConfig["config"]["swupdate2"]["state"] = "noupdates"
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "noupdates"

bridgeConfig["config"]["swupdate2"]["checkforupdate"] = False

def githubUICheck():
#creation_time = "2024-02-18 19:50:15.000000000 +0100\n"
creation_time = subprocess.run("stat -c %y flaskUI/templates/index.html", shell=True, capture_output=True, text=True)#2024-02-18 19:50:15.000000000 +0100\n
creation_time_arg1 = creation_time.stdout.replace(".", " ").split(" ")#2024-02-18, 19:50:15, 000000000, +0100\n
creation_time = creation_time_arg1[0] + " " + creation_time_arg1[1] + " " + creation_time_arg1[3].replace("\n", "")#2024-02-18 19:50:15 +0100
creation_time = datetime.strptime(creation_time, "%Y-%m-%d %H:%M:%S %z").astimezone(timezone.utc).strftime("%Y-%m-%d %H")#2024-02-18 18

url = "https://api.github.com/repos/diyhue/diyhueUI/branches/master"
#url = "https://api.github.com/repos/hendriksen-mark/diyhueUI/branches/master"
response = requests.get(url)
if response.status_code == 200:
device_data = json.loads(response.text)
publish_time = datetime.strptime(device_data["commit"]["commit"]["author"]["date"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H")

logging.info("creation_time UI : " + str(creation_time))
logging.info("publish_time UI : " + str(publish_time))

if publish_time > creation_time:
return True
else:
return False


def githubInstall_test():
if bridgeConfig["config"]["swupdate2"]["state"] == "anyreadytoinstall":#ui update
bridgeConfig["config"]["swupdate2"]["state"] = "installing"
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing"
subprocess.Popen("sh githubUIInstall.sh",shell=True, close_fds=True)
if bridgeConfig["config"]["swupdate2"]["state"] == "allreadytoinstall":#diyhue + ui update
bridgeConfig["config"]["swupdate2"]["state"] = "installing"
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing"
subprocess.Popen("sh githubInstall.sh",shell=True, close_fds=True)

def githubInstall():
logging.info("work in progress")
bridgeConfig["config"]["swupdate2"]["install"] = False

0 comments on commit e72c095

Please sign in to comment.