Merge pull request #31 from Meatballs1/set_multiple_values

Support JSON arrays in input data
This commit is contained in:
pmagyar
2024-03-21 09:20:40 +01:00
committed by GitHub

View File

@@ -93,7 +93,8 @@ class HCDevice:
return result
# Based on PR submitted https://github.com/Skons/hcpy/pull/1
def test_program_data(self, data):
def test_program_data(self, data_array):
for data in data_array:
if "program" not in data:
raise TypeError("Message data invalid, no program specified.")
@@ -128,7 +129,8 @@ class HCDevice:
)
# Test the feature of an appliance agains a data object
def test_feature(self, data):
def test_feature(self, data_array):
for data in data_array:
if "uid" not in data:
raise Exception("Unable to configure appliance. UID is required.")
@@ -164,9 +166,10 @@ class HCDevice:
if "values" in feature:
if isinstance(data["value"], int) is False:
raise Exception(
f"Unable to configure appliance. The value {data['value']} must be an integer."
f" Allowed values are {feature['values']}."
f"Unable to configure appliance. The value {data['value']} must "
f"be an integer. Allowed values are {feature['values']}."
)
value = str(data["value"])
# values are strings in the feature list,
# but always seem to be an integer. An integer must be provided
@@ -230,6 +233,9 @@ class HCDevice:
}
if data is not None:
if isinstance(data, list) is False:
data = [data]
if action == "POST":
if resource == "/ro/values":
# Raises exceptions on failure
@@ -238,7 +244,7 @@ class HCDevice:
# Raises exception on failure
self.test_program_data(data)
msg["data"] = [data]
msg["data"] = data
try:
self.ws.send(msg)