Initial attempt

This commit is contained in:
Meatballs1
2024-03-19 20:32:34 +00:00
parent 250f6c3fb7
commit bf168621f0
2 changed files with 58 additions and 18 deletions

View File

@@ -92,6 +92,36 @@ class HCDevice:
return result
# Based on PR submitted https://github.com/Skons/hcpy/pull/1/files#diff-f68bc58ad15ebfcb2b6ceca7c538e00790a6093c67c7c84ebcefb0c9756d3c0eR112-R139
def test_program_data(self, data):
if 'program' not in data:
raise Exception("{self.name}. Message data invalid, no program specified.")
if isinstance(data['program'], str):
try:
data['program'] = int(data['program'])
except Exception as e:
raise Exception("{self.name}. Message data invalid, UID in 'program' must be an integer.")
elif isinstance(data['program'], int) is False:
raise Exception("{self.name}. Message data invalid. UID in 'program' must be an integer.")
uid = str(data['program'])
if uid not in self.features:
raise Exception(f"{self.name}. Unable to configure appliance. Program UID {uid} is not valid for this device.")
feature = self.features[uid]
# Diswasher is Dishcare.Dishwasher.Program.{name}
# Hood is Cooking.Common.Program.{name}
# May also be in the format BSH.Common.Program.Favorite.001
if ".Program." not in feature['name']:
raise Exception(f"{self.name}. Unable to configure appliance. Program UID {uid} is not a valid program.")
if 'options' in data:
## TODO: Check to see if options is mandatory
for option_uid in data['options']:
if str(option_uid) not in self.features:
raise Exception(f"{self.name}. Unable to configure appliance. Option UID {uid} is not valid for this device.")
# Test the feature of an appliance agains a data object
def test_feature(self, data):
if "uid" not in data:
@@ -156,8 +186,6 @@ class HCDevice:
f"The value must be an integer in the range {min} and {max}."
)
return True
def recv(self):
try:
buf = self.ws.recv()
@@ -198,10 +226,13 @@ class HCDevice:
if data is not None:
if action == "POST":
if self.test_feature(data) is False:
return
msg["data"] = [data]
else:
if 'resource' == '/ro/values':
# Raises exceptions on failure
self.test_feature(data)
elif 'resource' == '/ro/activeProgram':
# Raises exception on failure
self.test_program_data(data)
msg["data"] = [data]
try:
@@ -297,12 +328,12 @@ class HCDevice:
# print(self.name, now(), "services", self.services)
# we should figure out which ones to query now
# if "iz" in self.services:
# self.get("/iz/info", version=self.services["iz"]["version"])
# if "ni" in self.services:
# self.get("/ni/info", version=self.services["ni"]["version"])
# if "ei" in self.services:
# self.get("/ei/deviceReady", version=self.services["ei"]["version"], action="NOTIFY")
# if "iz" in self.services:
# self.get("/iz/info", version=self.services["iz"]["version"])
# if "ni" in self.services:
# self.get("/ni/info", version=self.services["ni"]["version"])
# if "ei" in self.services:
# self.get("/ei/deviceReady", version=self.services["ei"]["version"], action="NOTIFY")
# self.get("/if/info")

21
hc2mqtt
View File

@@ -92,10 +92,19 @@ def client_connect(client, device, mqtt_topic):
mqtt_state = msg.payload.decode()
mqtt_topic = msg.topic.split("/")
print(now(), f"received mqtt message {mqtt_state}")
if len(mqtt_topic) >= 2:
device_name = mqtt_topic[-2]
topic = mqtt_topic[-1]
else:
raise Exception(f"Invalid mqtt topic {msg.topic}")
try:
msg = json.loads(mqtt_state)
if "uid" in msg:
dev[mqtt_topic[-2]].get("/ro/values", 1, "POST", msg)
if topic == 'set' and 'uid' in msg:
dev[device_name].get("/ro/values", 1, "POST", msg)
elif topic == 'activeProgram':
dev[device_name].get("/ro/activeProgram", 1, "POST", msg)
else:
raise Exception(f"Payload {msg} is not correctly formatted")
except Exception as e:
@@ -147,10 +156,10 @@ def client_connect(client, device, mqtt_topic):
if value is None:
continue
# new_topic = topics[topic]
# if new_topic == "remaining":
# state["remainingseconds"] = value
# value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60)
# new_topic = topics[topic]
# if new_topic == "remaining":
# state["remainingseconds"] = value
# value = "%d:%02d" % (value / 60 / 60, (value / 60) % 60)
new_topic = device_topics[topic]
state[new_topic] = value