Skip to content

Commit 107d532

Browse files
committed
Fix errors if the sensors are unavailable
1 parent 0db6700 commit 107d532

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

hackspaceapi/spaceapi.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ def get_sensors() -> dict:
6363

6464
# Handle entities with temp attributes
6565
if "temperature" in data["attributes"]:
66-
value = float(data["attributes"]["temperature"])
66+
try:
67+
value = float(data["attributes"]["temperature"])
68+
except ValueError:
69+
logging.warning("Failed to convert '{0}' to float, so skipping sensor {1}".format(data["attributes"]["temperature"], sensor.location or data["attributes"]["friendly_name"]))
70+
continue
6771
unit_val = data["attributes"]["temperature_unit"]
6872
else:
69-
value = float(data["state"])
73+
try:
74+
value = float(data["state"])
75+
except ValueError:
76+
logging.warning("Failed to convert '{0}' to float, so skipping sensor {1}".format(data["state"], sensor.location or data["attributes"]["friendly_name"]))
77+
continue
7078
unit_val = data["attributes"]["unit_of_measurement"]
7179

7280
results["temperature"].append(
@@ -91,7 +99,11 @@ def get_sensors() -> dict:
9199
value = float(data["attributes"]["humidity"])
92100
unit_val = "%" # Humidity attributes generally don't have a unit value, assume %
93101
else:
94-
value = float(data["state"])
102+
try:
103+
value = float(data["state"])
104+
except ValueError:
105+
logging.warning("Failed to convert '{0}' to float, so skipping sensor {1}".format(data["state"], sensor.location or data["attributes"]["friendly_name"]))
106+
continue
95107
unit_val = data["attributes"]["unit_of_measurement"]
96108

97109
results["humidity"].append(

0 commit comments

Comments
 (0)