-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin.py
executable file
·827 lines (731 loc) · 29.7 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# A Python plugin for Domoticz to access airly api for smog information in Poland
#
# Author: fisher
#
# TODO: Update text sensors only when changed
#
#
# v0.1.0 - initial version, fetching data from airly sensor
# v0.1.1 - response body decode - error handling, minor language corrections
# v0.1.2 - removed gettext based translations - it caused plugin instability
# v0.2 - added support for GIOS stations, fixed a bug where the update was suspended in the event of a response decode error
# v0.2.1 - pm25 & pm10 percentage indicator
# v0.2.2 - better exception handling
# v0.3.0 - airly APIv2 support, airly logo added to pm1/10/2.5 sensors
# v0.3.1 - add NO2 SO2 O3 and CO measurements (by CHKDSK88)
# v0.3.2 - rounding percentage values to integer for correct data push to influxdb (by pkilar43)
# v0.3.3 - compatibility with Python 3.7+ (by tschaban)
"""
<plugin key="AIRLY" name="domoticz-airly" author="fisher" version="0.3.3" wikilink="https://www.domoticz.com/wiki/Plugins/domoticz-airly.html" externallink="https://github.com/lrybak/domoticz-airly">
<params>
<param field="Mode1" label="Airly API key" default="" width="400px" required="true" />
<param field="Mode2" label="Airly installation id" width="40px" default="" required="true" />
<param field="Mode3" label="Check every x minutes" width="40px" default="15" required="true" />
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import datetime
import json
from http.client import HTTPSConnection
from urllib.parse import urlparse
from urllib.parse import urlencode
import socket
L10N = {
'pl': {
"Air Quality Index":
"Jakość powietrza",
"PM1":
"PM1",
"PM2.5":
"PM2.5",
"PM10":
"PM10",
"NO₂":
"NO₂",
"O₃":
"O₃",
"SO₂":
"SO₂",
"CO":
"CO",
"PM2.5 Norm":
"PM2.5 Norma",
"PM10 Norm":
"PM10 Norma",
"NO₂ Norm":
"NO₂ Norma",
"O₃ Norm":
"O₃ Norma",
"SO₂ Norm":
"SO₂ Norma",
"CO Norm":
"CO Norma",
"Air pollution Level":
"Zanieczyszczenie powietrza",
"Advice":
"Wskazówki",
"Temperature":
"Temperatura",
"Air pressure":
"Ciśnienie powietrza",
"Humidity":
"Wilgotność",
"Installation information":
"Informacje o stacji",
"Device Unit=%(Unit)d; Name='%(Name)s' already exists":
"Urządzenie Unit=%(Unit)d; Name='%(Name)s' już istnieje",
"Creating device Name=%(Name)s; Unit=%(Unit)d; ; TypeName=%(TypeName)s; Used=%(Used)d":
"Tworzę urządzenie Name=%(Name)s; Unit=%(Unit)d; ; TypeName=%(TypeName)s; Used=%(Used)d",
"%(Vendor)s - %(Address)s, %(Locality)s<br/>Station founder: %(sensorFounder)s":
"%(Vendor)s - %(Address)s, %(Locality)s<br/>Sponsor stacji: %(sensorFounder)s",
"%(Vendor)s - %(Locality)s %(StreetNumber)s<br/>Station founder: %(sensorFounder)s":
"%(Vendor)s - %(Locality)s %(StreetNumber)s<br/>Sponsor stacji: %(sensorFounder)s",
"Sensor id (%(installation_id)d) not exists":
"Sensor (%(installation_id)d) nie istnieje",
"Not authorized":
"Brak autoryzacji",
"Starting device update":
"Rozpoczynanie aktualizacji urządzeń",
"Update unit=%d; nValue=%d; sValue=%s":
"Aktualizacja unit=%d; nValue=%d; sValue=%s",
"Bad air today!":
"Zła jakość powietrza",
"Enter correct airly API key - get one on https://developer.airly.eu":
"Wprowadź poprawny klucz api - pobierz klucz na stronie https://developer.airly.eu",
"Awaiting next poll: %s":
"Oczekiwanie na następne pobranie: %s",
"Next poll attempt at: %s":
"Następna próba pobrania: %s",
"Connection to airly api failed: %s":
"Połączenie z airly api nie powiodło się: %s",
"Unrecognized error: %s":
"Nierozpoznany błąd: %s"
},
'en': { }
}
def _(key):
try:
return L10N[Settings["Language"]][key]
except KeyError:
return key
class UnauthorizedException(Exception):
def __init__(self, expression, message):
self.expression = expression
self.message = message
class SensorNotFoundException(Exception):
def __init__(self, expression, message):
self.expression = expression
self.message = message
class TooManyRequestsException(Exception):
def __init__(self, expression, message):
self.expression = expression
self.message = message
class ConnectionErrorException(Exception):
def __init__(self, expression, message):
self.expression = expression
self.message = message
class BasePlugin:
enabled = False
def __init__(self):
# Consts
self.version = "0.3.1"
self.airly_api_user_agent = "domoticz-airly/%s" % self.version
# Api v2
self.api_v2_installation_measurements = "https://airapi.airly.eu/v2/measurements/installation"
self.api_v2_installation_info = "https://airapi.airly.eu/v2/installations/%(installationId)d"
self.airly_api_headers = {
"User-Agent": self.airly_api_user_agent,
"Accept": "application/json",
"apikey": ""
}
self.EXCEPTIONS = {
"SENSOR_NOT_FOUND": 1,
"UNAUTHORIZED": 2,
}
self.debug = False
self.inProgress = False
# Do not change below UNIT constants!
self.UNIT_AIR_QUALITY_INDEX = 1
self.UNIT_AIR_POLLUTION_LEVEL = 2
self.UNIT_PM1 = 3
self.UNIT_PM25 = 4
self.UNIT_PM10 = 5
self.UNIT_TEMPERATURE = 6
self.UNIT_BAROMETER = 7
self.UNIT_HUMIDITY = 8
self.UNIT_STATION_LOCATION = 9
self.UNIT_AIR_POLLUTION_ADVICE = 10
self.UNIT_PM25_PERCENTAGE = 11
self.UNIT_PM10_PERCENTAGE = 12
self.UNIT_NO2_PERCENTAGE = 13
self.UNIT_O3_PERCENTAGE = 14
self.UNIT_SO2_PERCENTAGE = 15
self.UNIT_CO_PERCENTAGE = 16
self.UNIT_NO2 = 21
self.UNIT_O3 = 22
self.UNIT_SO2 = 23
self.UNIT_CO = 24
self.UNIT_PM25_NORM = 25
self.UNIT_PM10_NORM = 50
self.UNIT_NO2_NORM = 200
self.UNIT_O3_NORM = 100
self.UNIT_SO2_NORM = 350
self.UNIT_CO_NORM = 30000
# Icons
self.iconName = "airly"
self.nextpoll = datetime.datetime.now()
return
def onStart(self):
Domoticz.Debug("onStart called")
if Parameters["Mode6"] == 'Debug':
self.debug = True
Domoticz.Debugging(1)
DumpConfigToLog()
else:
Domoticz.Debugging(0)
Domoticz.Heartbeat(20)
self.pollinterval = int(Parameters["Mode3"]) * 60
if self.iconName not in Images: Domoticz.Image('icons.zip').Create()
iconID = Images[self.iconName].ID
self.variables = {
self.UNIT_AIR_QUALITY_INDEX: {
"Name": _("Air Quality Index"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "CAQI"},
"Image": iconID,
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_PM1: {
"Name": _("PM1"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Image": iconID,
"Used": 0,
"nValue": 0,
"sValue": None,
},
self.UNIT_PM25: {
"Name": _("PM2.5"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Image": iconID,
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_PM10: {
"Name": _("PM10"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Image": iconID,
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_NO2: {
"Name": _("NO₂"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_O3: {
"Name": _("O₃"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_SO2: {
"Name": _("SO₂"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_CO: {
"Name": _("CO"),
"TypeName": "Custom",
"Options": {"Custom": "1;%s" % "µg/m³"},
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_AIR_POLLUTION_LEVEL: {
"Name": _("Air pollution Level"),
"TypeName": "Alert",
"Image": 7,
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_AIR_POLLUTION_ADVICE: {
"Name": _("Advice"),
"TypeName": "Alert",
"Image": 7,
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_TEMPERATURE: {
"Name": _("Temperature"),
"TypeName": "Temperature",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_BAROMETER: {
"Name": _("Air pressure"),
"TypeName": "Barometer",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_HUMIDITY: {
"Name": _("Humidity"),
"TypeName": "Humidity",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_STATION_LOCATION: {
"Name": _("Sensor information"),
"TypeName": "Text",
"Image": 7,
"Used": 0,
"nValue": 0,
"sValue": None,
},
self.UNIT_PM25_PERCENTAGE: {
"Name": _("PM2.5 Norm"),
"TypeName": "Percentage",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_PM10_PERCENTAGE: {
"Name": _("PM10 Norm"),
"TypeName": "Percentage",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_NO2_PERCENTAGE: {
"Name": _("NO₂ Norm"),
"TypeName": "Percentage",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_O3_PERCENTAGE: {
"Name": _("O₃ Norm"),
"TypeName": "Percentage",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_SO2_PERCENTAGE: {
"Name": _("SO₂ Norm"),
"TypeName": "Percentage",
"Used": 1,
"nValue": 0,
"sValue": None,
},
self.UNIT_CO_PERCENTAGE: {
"Name": _("CO Norm"),
"TypeName": "Percentage",
"Used": 1,
"nValue": 0,
"sValue": None,
},
}
self.onHeartbeat(fetch=True)
def onStop(self):
Domoticz.Log("onStop called")
Domoticz.Debugging(0)
def onConnect(self, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Data, Status, Extra):
Domoticz.Log("onMessage called")
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log(
"onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(
Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self):
Domoticz.Log("onDisconnect called")
def postponeNextPool(self, seconds=3600):
self.nextpoll = (datetime.datetime.now() + datetime.timedelta(seconds=seconds))
return self.nextpoll
def createDevice(self, key=None):
"""create Domoticz virtual device"""
def createSingleDevice(key):
"""inner helper function to handle device creation"""
item = self.variables[key]
_unit = key
_name = item['Name']
# skip if already exists
if key in Devices:
Domoticz.Debug(_("Device Unit=%(Unit)d; Name='%(Name)s' already exists") % {'Unit': key, 'Name': _name})
return
try:
_options = item['Options']
except KeyError:
_options = {}
_typename = item['TypeName']
try:
_used = item['Used']
except KeyError:
_used = 0
try:
_image = item['Image']
except KeyError:
_image = 0
Domoticz.Debug(_("Creating device Name=%(Name)s; Unit=%(Unit)d; ; TypeName=%(TypeName)s; Used=%(Used)d") % {
'Name': _name,
'Unit': _unit,
'TypeName': _typename,
'Used': _used,
})
Domoticz.Device(
Name=_name,
Unit=_unit,
TypeName=_typename,
Image=_image,
Options=_options,
Used=_used
).Create()
if key:
createSingleDevice(key)
else:
for k in self.variables.keys():
createSingleDevice(k)
def onHeartbeat(self, fetch=False):
Domoticz.Debug("onHeartbeat called")
now = datetime.datetime.now()
if fetch == False:
if self.inProgress or (now < self.nextpoll):
Domoticz.Debug(_("Awaiting next pool: %s") % str(self.nextpoll))
return
# Set next pool time
self.postponeNextPool(seconds=self.pollinterval)
# First call, lets query API for sensor location data
try:
if fetch:
res = self.installation_info(Parameters["Mode2"])
address = ""
if "street" in res["address"] and res["address"]["street"] is not None:
address = res["address"]["street"]
if "number" in res["address"] and res["address"]["number"] is not None:
address = address + " " + res["address"]["number"]
if len(address) > 0:
self.variables[self.UNIT_STATION_LOCATION]['sValue'] = _("%(Address)s, %(City)s<br/>Station founder: %(sensorFounder)s") % {
"Address": address,
"City": res["address"]["city"],
"sensorFounder": res["sponsor"]["name"],
}
else:
self.variables[self.UNIT_STATION_LOCATION]['sValue'] = _("%(City)s<br/>Station founder: %(sensorFounder)s") % {
"City": res["address"]["city"],
"sensorFounder": res["sponsor"]["name"],
}
self.doUpdate()
except UnauthorizedException as ue:
Domoticz.Error(ue.message)
Domoticz.Error(_("Enter correct airly API key - get one on https://developer.airly.eu"))
return
except TooManyRequestsException as tmre:
Domoticz.Error(tmre.message)
# postpone next pool to tomorrow
next_attempt = self.postponeNextPool()
Domoticz.Error(_("Next pool attempt at: %s") % str(next_attempt))
return
except ConnectionErrorException as cee:
Domoticz.Error(_("Connection to airly api failed: %s") % str(cee.message))
return
except Exception as e:
Domoticz.Error(e.message)
return
try:
# check if another thread is not running
# and time between last fetch has elapsed
self.inProgress = True
res = self.installation_measurement(Parameters["Mode2"])
# iterate through values map
values={}
for item in res["values"]:
try:
values[item['name']] = item['value']
except KeyError:
pass # No key/value
try:
self.variables[self.UNIT_PM10]['sValue'] = values["PM10"]
self.variables[self.UNIT_PM10_PERCENTAGE]['sValue'] = str(round((values["PM10"]/self.UNIT_PM10_NORM) * 100))
except KeyError:
pass # No pm10 value
try:
self.variables[self.UNIT_PM25]['sValue'] = values["PM25"]
self.variables[self.UNIT_PM25_PERCENTAGE]['sValue'] = str(round((values["PM25"] / self.UNIT_PM25_NORM) * 100))
except KeyError:
pass # No pm25 value
try:
self.variables[self.UNIT_PM1]['sValue'] = values["PM1"]
except KeyError:
pass # No pm1 value
try:
self.variables[self.UNIT_NO2]['sValue'] = values["NO2"]
self.variables[self.UNIT_NO2_PERCENTAGE]['sValue'] = str(round((values["NO2"]/self.UNIT_NO2_NORM) * 100))
except KeyError:
pass # No no2 value
try:
self.variables[self.UNIT_O3]['sValue'] = values["O3"]
self.variables[self.UNIT_O3_PERCENTAGE]['sValue'] = str(round((values["O3"]/self.UNIT_O3_NORM) * 100))
except KeyError:
pass # No o3 value
try:
self.variables[self.UNIT_SO2]['sValue'] = values["SO2"]
self.variables[self.UNIT_SO2_PERCENTAGE]['sValue'] = str(round((values["SO2"]/self.UNIT_SO2_NORM) * 100))
except KeyError:
pass # No so2 value
try:
self.variables[self.UNIT_CO]['sValue'] = values["CO"]
self.variables[self.UNIT_CO_PERCENTAGE]['sValue'] = str(round((values["CO"]/self.UNIT_CO_NORM) * 100))
except KeyError:
pass # No co value
try:
self.variables[self.UNIT_AIR_QUALITY_INDEX]['sValue'] = str(round(res["indexes"][0]["value"]))
except KeyError:
pass # No airQualityIndex value
try:
if res["indexes"][0]["level"] == "VERY_LOW":
pollutionLevel = 1 # green
elif res["indexes"][0]["level"] == "LOW":
pollutionLevel = 1 # green
elif res["indexes"][0]["level"] == "MEDIUM":
pollutionLevel = 2 # yellow
elif res["indexes"][0]["level"] == "HIGH":
pollutionLevel = 3 # orange
elif res["indexes"][0]["level"] == "EXTREME":
pollutionLevel = 4 # red
elif res["indexes"][0]["level"] == "AIRMAGEDDON":
pollutionLevel = 4 # red
else:
pollutionLevel = 0
pollutionDescription = res["indexes"][0]["description"]
pollutionAdvice = res["indexes"][0]["advice"]
self.variables[self.UNIT_AIR_POLLUTION_LEVEL]['nValue'] = pollutionLevel
self.variables[self.UNIT_AIR_POLLUTION_LEVEL]['sValue'] = pollutionDescription
self.variables[self.UNIT_AIR_POLLUTION_ADVICE]['nValue'] = pollutionLevel
self.variables[self.UNIT_AIR_POLLUTION_ADVICE]['sValue'] = pollutionAdvice
except KeyError:
pass # No air pollution value
try:
humidity = int(round(values["HUMIDITY"]))
if humidity < 40:
humidity_status = 2 # dry HUMIDITY
elif 40 <= humidity <= 60:
humidity_status = 0 # normal HUMIDITY
elif 40 < humidity <= 70:
humidity_status = 1 # comfortable HUMIDITY
else:
humidity_status = 3 # wet HUMIDITY
self.variables[self.UNIT_HUMIDITY]['nValue'] = humidity
self.variables[self.UNIT_HUMIDITY]['sValue'] = str(humidity_status)
except KeyError:
pass # No humidity value
try:
self.variables[self.UNIT_TEMPERATURE]['sValue'] = values["TEMPERATURE"]
except KeyError:
pass # No temperature value
try:
# in hpa + normal forecast
self.variables[self.UNIT_BAROMETER]['sValue'] = str(values["PRESSURE"]) + ";0"
except KeyError:
pass # No pressure value
self.doUpdate()
except SensorNotFoundException as snfe:
Domoticz.Error(_("Sensor id (%(installation_id)d) not exists") % {'installation_id': int(Parameters["Mode2"])})
return
except UnauthorizedException as ue:
Domoticz.Error(ue.message)
Domoticz.Error(_("Enter correct airly API key - get one on https://developer.airly.eu"))
return
except TooManyRequestsException as tmre:
Domoticz.Error(tmre.message)
# postpone next pool to tomorrow
next_attempt = self.postponeNextPool()
Domoticz.Error(_("Next pool attempt at: %s") % str(next_attempt))
return
except ConnectionErrorException as cee:
Domoticz.Error(_("Connection to airly api failed: %s") % str(cee.message))
return
except Exception as e:
Domoticz.Error(_("Unrecognized error: %s") % str(e))
finally:
self.inProgress = False
def doUpdate(self):
Domoticz.Log(_("Starting device update"))
for unit in self.variables:
nV = self.variables[unit]['nValue']
sV = self.variables[unit]['sValue']
# cast float to str
if isinstance(sV, float):
sV = str(float("{0:.0f}".format(sV)))
# Create device if required
if sV:
self.createDevice(key=unit)
if unit in Devices:
Domoticz.Log(_("Update unit=%d; nValue=%d; sValue=%s") % (unit, nV, sV))
Devices[unit].Update(nValue=nV, sValue=sV)
def api_airly_headers(self):
"""return http request headers"""
self.airly_api_headers['apikey'] = Parameters['Mode1']
self.airly_api_headers['User-Agent'] = self.airly_api_user_agent
self.airly_api_headers['User-Agent'] = self.airly_api_user_agent
self.airly_api_headers['Accept-Language'] = Settings["Language"]
return self.airly_api_headers
def installation_measurement(self, installation_id):
"""current sensor measurements"""
installation_id = int(installation_id)
airly_api = urlparse(self.api_v2_installation_measurements)
params = urlencode({
'installationId': installation_id,
'indexType': 'AIRLY_CAQI'
})
try:
conn = HTTPSConnection(airly_api.netloc)
conn.request(
method="GET",
url=airly_api.path + "?" + params,
headers=self.api_airly_headers(),
)
response = conn.getresponse()
response_object = {}
response_body = response.read()
except Exception as e:
raise ConnectionErrorException('', str(e))
try:
response_object = json.loads(response_body.decode("utf-8"))
except UnicodeDecodeError as ude:
Domoticz.Error(str(ude.message))
# reset nextpool datestamp to force running in next run
self.postponeNextPool(seconds=0)
if response.status == 200:
if "current" in response_object and len(response_object['current']) > 0:
return response_object['current']
else:
raise SensorNotFoundException(installation_id, "")
return response_object
elif response.status in (401, 403, 404):
raise UnauthorizedException(
response.status,
response_object['message'] if "message" in response_object else 'UnauthorizedException'
)
elif response.status == 429:
raise TooManyRequestsException(
response.status,
response_object['message'] if "message" in response_object else 'TooManyRequestsException1'
)
else:
Domoticz.Error(
str(response.status) + ": " +
response_object['message'] if "message" in response_object else 'UnknownError'
)
def installation_info(self, installation_id):
"""Station's info with coordinates, address and current pollution level"""
installation_id = int(installation_id)
airly_api = urlparse(self.api_v2_installation_info)
try:
conn = HTTPSConnection(airly_api.netloc)
conn.request(
method="GET",
url=airly_api.path % {'installationId': installation_id},
headers=self.api_airly_headers(),
)
response = conn.getresponse()
response_object = {}
response_body = response.read()
except Exception as e:
raise ConnectionErrorException('', str(e))
try:
response_object = json.loads(response_body.decode("utf-8"))
except UnicodeDecodeError as ude:
Domoticz.Error(ude.message)
# reset nextpool datestamp to force running in next run
self.nextpoll = datetime.datetime.now()
return response_object
if response.status == 200:
return response_object
elif response.status == 301:
Domoticz.Error(
str(response.status) + ": " +
response_object['message'] if "message" in response_object else 'UnknownError'
)
elif response.status in (403, 404):
raise UnauthorizedException(
response.status,
response_object['message'] if "message" in response_object else 'UnauthorizedException'
)
elif response.status == 429:
raise TooManyRequestsException(
response.status,
response_object['message'] if "message" in response_object else 'TooManyRequestsException2'
)
else:
Domoticz.Error(
str(response.status) + ": " +
response_object['message'] if "message" in response_object else 'UnknownError'
)
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Status, Description):
global _plugin
_plugin.onConnect(Status, Description)
def onMessage(Data, Status, Extra):
global _plugin
_plugin.onMessage(Data, Status, Extra)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect():
global _plugin
_plugin.onDisconnect()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return