Skip to content

Commit e72c095

Browse files
Add update (diyhue#1002)
* 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
1 parent 571a0e3 commit e72c095

File tree

11 files changed

+208
-2
lines changed

11 files changed

+208
-2
lines changed

.build/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ COPY ./BridgeEmulator/services/ /opt/hue-emulator/services/
3333
COPY ./BridgeEmulator/configManager/ /opt/hue-emulator/configManager/
3434
COPY ./BridgeEmulator/logManager/ /opt/hue-emulator/logManager/
3535
COPY ./BridgeEmulator/HueEmulator3.py /opt/hue-emulator/
36+
COPY ./BridgeEmulator/githubInstall.sh /opt/hue-emulator/
37+
COPY ./BridgeEmulator/githubUIInstall.sh /opt/hue-emulator/
3638

3739
## Install the web interface
3840
RUN curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip \

BridgeEmulator/configManager/configHandler.py

+27
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ def load_config(self):
7474
config["port"] = {"enabled": False,"ports": [80]}
7575
if "zigbee_device_discovery_info" not in config:
7676
config["zigbee_device_discovery_info"] = {"status": "ready"}
77+
if "swupdate2" not in config:
78+
config["swupdate2"] = {"autoinstall": {
79+
"on": True,
80+
"updatetime": "T14:00:00"
81+
},
82+
"bridge": {
83+
"lastinstall": "2020-12-11T17:08:55",
84+
"state": "noupdates"
85+
},
86+
"checkforupdate": False,
87+
"lastchange": "2020-12-13T10:30:15",
88+
"state": "unknown",
89+
"install": False
90+
}
7791

7892
if int(config["swversion"]) < 1958077010:
7993
config["swversion"] = "1962154010"
@@ -111,6 +125,19 @@ def load_config(self):
111125
"tpkasa": {"enabled":True},
112126
"elgato": {"enabled":True},
113127
"zigbee_device_discovery_info": {"status": "ready"},
128+
"swupdate2": { "autoinstall": {
129+
"on": True,
130+
"updatetime": "T14:00:00"
131+
},
132+
"bridge": {
133+
"lastinstall": "2020-12-11T17:08:55",
134+
"state": "noupdates"
135+
},
136+
"checkforupdate": False,
137+
"lastchange": "2020-12-13T10:30:15",
138+
"state": "unknown",
139+
"install": False
140+
}
114141
}
115142
# load lights
116143
if os.path.exists(self.configDir + "/lights.yaml"):

BridgeEmulator/flaskUI/core/views.py

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from lights.light_types import lightTypes
1111
from subprocess import check_output
1212
from pprint import pprint
13+
import os
14+
import logManager
15+
logging = logManager.logger.get_logger(__name__)
1316
bridgeConfig = configManager.bridgeConfig.yaml_config
1417
core = Blueprint('core',__name__)
1518
@core.route('/')
@@ -81,6 +84,10 @@ def save_config():
8184
configManager.bridgeConfig.save_config()
8285
return "config saved"
8386

87+
@core.route('/reboot')
88+
def reboot():
89+
logging.info("start reboot")
90+
os._exit(0)
8491

8592
@core.route('/login', methods=['GET', 'POST'])
8693
def login():

BridgeEmulator/flaskUI/restful.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from flask import request
1717
from functions.rules import rulesProcessor
1818
from services.entertainment import entertainmentService
19+
from services.updateManager import githubCheck, versionCheck, githubInstall
1920

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

273+
if resource == "config" and "swupdate2" in putDict:
274+
if "checkforupdate" in putDict["swupdate2"] and putDict["swupdate2"]["checkforupdate"] == True:
275+
versionCheck()
276+
githubCheck()
277+
if "install" in putDict["swupdate2"] and putDict["swupdate2"]["install"] == True:
278+
githubInstall()
279+
272280
# build response list
273281
responseList = []
274282
response_location = "/" + resource + "/"

BridgeEmulator/functions/request.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import requests
2+
from time import sleep
23

34
def sendRequest(url, method, data, timeout=3, delay=0):
45
if delay != 0:

BridgeEmulator/githubInstall.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
curl -s localhost/save
2+
cd /
3+
[ ! -d diyhue ] && mkdir diyhue
4+
curl -s -J -L -o $diyhue.tar.gz "https://github.com/diyhue/diyHue/archive/refs/heads/master.tar.gz"
5+
tar xzf $diyhue.tar.gz --strip-components=1 -C diyhue
6+
#cd diyhue
7+
[ -d /opt/hue-emulator/flaskUI ] && rm -r /opt/hue-emulator/flaskUI
8+
mv diyhue/BridgeEmulator/flaskUI /opt/hue-emulator/
9+
10+
[ -d /opt/hue-emulator/functions ] && rm -r /opt/hue-emulator/functions
11+
mv diyhue/BridgeEmulator/functions /opt/hue-emulator/
12+
13+
[ -d /opt/hue-emulator/lights ] && rm -r /opt/hue-emulator/lights
14+
mv diyhue/BridgeEmulator/lights /opt/hue-emulator/
15+
16+
[ -d /opt/hue-emulator/sensors ] && rm -r /opt/hue-emulator/sensors
17+
mv diyhue/BridgeEmulator/sensors /opt/hue-emulator/
18+
19+
[ -d /opt/hue-emulator/HueObjects ] && rm -r /opt/hue-emulator/HueObjects
20+
mv diyhue/BridgeEmulator/HueObjects /opt/hue-emulator/
21+
22+
[ -d /opt/hue-emulator/services ] && rm -r /opt/hue-emulator/services
23+
mv diyhue/BridgeEmulator/services /opt/hue-emulator
24+
25+
[ -d /opt/hue-emulator/configManager ] && rm -r /opt/hue-emulator/configManager
26+
mv diyhue/BridgeEmulator/configManager /opt/hue-emulator/
27+
28+
[ -d /opt/hue-emulator/logManager ] && rm -r /opt/hue-emulator/logManager
29+
mv diyhue/BridgeEmulator/logManager /opt/hue-emulator/
30+
31+
mv diyhue/BridgeEmulator/HueEmulator3.py /opt/hue-emulator/
32+
mv diyhue/BridgeEmulator/githubInstall.sh /opt/hue-emulator/
33+
mv diyhue/BridgeEmulator/githubUIInstall.sh /opt/hue-emulator/
34+
mv diyhue/BridgeEmulator/genCert.sh /opt/hue-emulator/
35+
mv diyhue/BridgeEmulator/openssl.conf /opt/hue-emulator/
36+
chmod +x /opt/hue-emulator/genCert.sh
37+
38+
#cd /
39+
[ ! -d /diyhueUI ] && mkdir diyhueUI
40+
curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip
41+
unzip -qo diyHueUI.zip -d diyhueUI
42+
#cd diyhueUI
43+
mv diyhueUI/index.html /opt/hue-emulator/flaskUI/templates/
44+
[ -d /opt/hue-emulator/flaskUI/static ] && rm -r /opt/hue-emulator/flaskUI/static
45+
mv diyhueUI/static /opt/hue-emulator/flaskUI/
46+
47+
curl -s "localhost.lan/reboot"

BridgeEmulator/githubUIInstall.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
curl -s localhost/save
2+
cd /
3+
[ ! -d /diyhueUI ] && mkdir diyhueUI
4+
curl -sL https://github.com/diyhue/diyHueUI/releases/latest/download/DiyHueUI-release.zip -o diyHueUI.zip
5+
unzip -qo diyHueUI.zip -d diyhueUI
6+
cd diyhueUI
7+
mv index.html /opt/hue-emulator/flaskUI/templates/
8+
[ -d /opt/hue-emulator/flaskUI/static ] && rm -r /opt/hue-emulator/flaskUI/static
9+
mv static /opt/hue-emulator/flaskUI/
10+
11+
curl -s localhost/reboot

BridgeEmulator/services/deconz.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def received_message(self, m):
119119
del message["state"]["dark"]
120120

121121
if bridgeSensor.modelid == "SML001" and "lightlevel" in message["state"]:
122-
if message["state"][lightlevel] > bridgeSensor.config["tholddark"]:
122+
if message["state"]["lightlevel"] > bridgeSensor.config["tholddark"]:
123123
message["state"]["dark"] = False
124124
else:
125125
message["state"]["dark"] = True

BridgeEmulator/services/mqtt.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from sensors.sensor_types import sensorTypes
1515
from lights.discover import addNewLight
1616
from functions.rules import rulesProcessor
17+
import requests
1718

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

BridgeEmulator/services/scheduler.py

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from functions.request import sendRequest
99
from functions.daylightSensor import daylightSensor
1010
from functions.scripts import triggerScript
11+
from services.updateManager import versionCheck, githubCheck
1112

1213
bridgeConfig = configManager.bridgeConfig.yaml_config
1314
logging = logManager.logger.get_logger(__name__)
@@ -104,6 +105,9 @@ def runScheduler():
104105
if (datetime.now().strftime("%M:%S") == "00:10"): #auto save configuration every hour
105106
configManager.bridgeConfig.save_config()
106107
Thread(target=daylightSensor, args=[bridgeConfig["config"]["timezone"], bridgeConfig["sensors"]["1"]]).start()
108+
if (datetime.now().strftime("%H") == "23"): #check for updates every day at 23:00:10
109+
versionCheck()
110+
githubCheck()
107111
if (datetime.now().strftime("%H") == "23" and datetime.now().strftime("%A") == "Sunday"): #backup config every Sunday at 23:00:10
108112
configManager.bridgeConfig.save_config(backup=True)
109113
sleep(1)
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import requests
2+
import configManager
3+
import logManager
4+
import json
5+
from datetime import datetime, timezone
6+
import subprocess
7+
8+
bridgeConfig = configManager.bridgeConfig.yaml_config
9+
logging = logManager.logger.get_logger(__name__)
10+
11+
def versionCheck():
12+
swversion = bridgeConfig["config"]["swversion"]
13+
url = "https://firmware.meethue.com/v1/checkupdate/?deviceTypeId=BSB002&version=" + swversion
14+
response = requests.get(url)
15+
if response.status_code == 200:
16+
device_data = json.loads(response.text)
17+
if len(device_data["updates"]) != 0:
18+
new_version = str(device_data["updates"][len(device_data["updates"])-1]["version"])
19+
new_versionName = str(device_data["updates"][len(device_data["updates"])-1]["versionName"])
20+
if new_version > swversion:
21+
logging.info("swversion number update from Philips, old: " + swversion + " new:" + new_version)
22+
bridgeConfig["config"]["swversion"] = new_version
23+
bridgeConfig["config"]["apiversion"] = new_versionName
24+
bridgeConfig["config"]["swupdate2"]["lastchange"] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
25+
bridgeConfig["config"]["swupdate2"]["bridge"]["lastinstall"] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
26+
else:
27+
logging.info("swversion higher than Philips")
28+
else:
29+
logging.info("no swversion number update from Philips")
30+
31+
def githubCheck():
32+
#creation_time = "2024-02-18 19:50:15.000000000 +0100\n"
33+
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
34+
creation_time_arg1 = creation_time.stdout.replace(".", " ").split(" ")#2024-02-18, 19:50:15, 000000000, +0100\n
35+
creation_time = creation_time_arg1[0] + " " + creation_time_arg1[1] + " " + creation_time_arg1[3].replace("\n", "")#2024-02-18 19:50:15 +0100
36+
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
37+
38+
url = "https://api.github.com/repos/diyhue/diyhue/branches/master"
39+
#url = "https://api.github.com/repos/hendriksen-mark/diyhue/branches/master"
40+
response = requests.get(url)
41+
if response.status_code == 200:
42+
device_data = json.loads(response.text)
43+
publish_time = datetime.strptime(device_data["commit"]["commit"]["author"]["date"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H")
44+
45+
logging.info("creation_time diyHue : " + str(creation_time))
46+
logging.info("publish_time diyHue : " + str(publish_time))
47+
48+
if publish_time > creation_time:
49+
logging.info("update on github")
50+
bridgeConfig["config"]["swupdate2"]["state"] = "allreadytoinstall"
51+
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "allreadytoinstall"
52+
elif githubUICheck() == True:
53+
logging.info("UI update on github")
54+
bridgeConfig["config"]["swupdate2"]["state"] = "anyreadytoinstall"
55+
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "anyreadytoinstall"
56+
else:
57+
logging.info("no update for diyHue or UI on github")
58+
bridgeConfig["config"]["swupdate2"]["state"] = "noupdates"
59+
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "noupdates"
60+
61+
bridgeConfig["config"]["swupdate2"]["checkforupdate"] = False
62+
63+
def githubUICheck():
64+
#creation_time = "2024-02-18 19:50:15.000000000 +0100\n"
65+
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
66+
creation_time_arg1 = creation_time.stdout.replace(".", " ").split(" ")#2024-02-18, 19:50:15, 000000000, +0100\n
67+
creation_time = creation_time_arg1[0] + " " + creation_time_arg1[1] + " " + creation_time_arg1[3].replace("\n", "")#2024-02-18 19:50:15 +0100
68+
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
69+
70+
url = "https://api.github.com/repos/diyhue/diyhueUI/branches/master"
71+
#url = "https://api.github.com/repos/hendriksen-mark/diyhueUI/branches/master"
72+
response = requests.get(url)
73+
if response.status_code == 200:
74+
device_data = json.loads(response.text)
75+
publish_time = datetime.strptime(device_data["commit"]["commit"]["author"]["date"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H")
76+
77+
logging.info("creation_time UI : " + str(creation_time))
78+
logging.info("publish_time UI : " + str(publish_time))
79+
80+
if publish_time > creation_time:
81+
return True
82+
else:
83+
return False
84+
85+
86+
def githubInstall_test():
87+
if bridgeConfig["config"]["swupdate2"]["state"] == "anyreadytoinstall":#ui update
88+
bridgeConfig["config"]["swupdate2"]["state"] = "installing"
89+
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing"
90+
subprocess.Popen("sh githubUIInstall.sh",shell=True, close_fds=True)
91+
if bridgeConfig["config"]["swupdate2"]["state"] == "allreadytoinstall":#diyhue + ui update
92+
bridgeConfig["config"]["swupdate2"]["state"] = "installing"
93+
bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing"
94+
subprocess.Popen("sh githubInstall.sh",shell=True, close_fds=True)
95+
96+
def githubInstall():
97+
logging.info("work in progress")
98+
bridgeConfig["config"]["swupdate2"]["install"] = False

0 commit comments

Comments
 (0)