Home Foren Hardware Support und Bugs Einstieg in API-Anwendung Antwort auf: Einstieg in API-Anwendung

sundriver
Teilnehmer
Beitragsanzahl: 150

Hallo homeheater,

hier ein Beispiel in der Programmiersprache Python, wie ich Temperaturen auslese. Vielleicht hilft Dir das weiter:

class Temperature:
„““
constructor
„““
def __init__(self):
self.__URL_TEMPS = URL.miniserver_url + „/get/json/v1/1/temps“
self.__OPEN_TIMEOUT = 20
self.__READ_TIMEOUT = 120
self.__ETAGENNAME_KEY = ‚etagenname‘
self.__ROOM_KEY = ‚raeume‘
self.__SENSORS_KEY = ’sensoren‘
self.__SENSOR_NAME_KEY = ’name‘
self.__VALUE_KEY = ‚wert‘

self.__temps = []

„““
request temperatures
„““
def request_temperatures(self):
try:
r = requests.get(url = self.__URL_TEMPS, timeout=(self.__OPEN_TIMEOUT, self.__READ_TIMEOUT))
self.__temps = r.json()
except:
self.__temps = []

„““
get a temperature value
„““
def temperature(self, floor, sensor_name):
try:
dict = [d for d in self.__temps if d[self.__ETAGENNAME_KEY]== floor][0]
rooms = (dict[self.__ROOM_KEY])
sensors = [x[self.__SENSORS_KEY] for x in rooms]

for dict in sensors:
sensor = next((l for l in dict if l.get(self.__SENSOR_NAME_KEY) == sensor_name), None)
if sensor != None:
break

return str(round(sensor[self.__VALUE_KEY], 1))
except:
return self.__NO_TEMP