-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriverlevel.py
executable file
·74 lines (69 loc) · 3.03 KB
/
riverlevel.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
import appdaemon.plugins.hass.hassapi as hass
import datetime
import time
import urllib.request
import json
class riverlevel(hass.Hass):
def initialize(self):
# self.LOGLEVEL="DEBUG"
self.log("riverlevel app")
if "stationid" in self.args:
self.stationid=self.args["stationid"]
else:
self.log("stationid must be set in ap./ha pdaemon.cfg file")
if "interval" in self.args:
self.interval=self.args["interval"]
else:
self.interval=7
if "controlname" in self.args:
self.control_name=self.args["controlname"]
else:
self.control_name="sensor.river_level"
if "country" in self.args:
self.log("country in appdaemon.cfg={}".format(self.args["country"]))
self.country=self.args["country"]
else:
self.country="US"
self.log("set default country={}".format(self.country))
if "rivername" in self.args:
self.log("river name={}".format(self.args["rivername"]))
self.rivername=self.args["rivername"]
else:
self.rivername=self.stationid
self.run_every(self.timer_handler,self.datetime(),60*self.interval)
self.log("initialization complete")
def timer_handler(self,kwargs):
self.log("self.country={}".format(self.country))
if self.country=="UK":
url=str("http://environment.data.gov.uk/flood-monitoring/id/stations/{}".format(self.stationid))
self.log("uk url={}".format(url))
data=urllib.request.urlopen(url).read()
jd=json.loads(data.decode("utf-8"))
if "latestReading" in jd["items"]["measures"]:
newlevel=jd["items"]["measures"]["latestReading"]["value"]
lastReading=jd["items"]["measures"]["latestReading"]["dateTime"]
else:
newlevel=jd["items"]["measures"][0]["latestReading"]["value"]
lastReading=jd["items"]["measures"][0]["latestReading"]["dateTime"]
lat=jd["items"]["lat"]
lon=jd["items"]["long"]
stage_datum=jd["items"]["stageScale"]["datum"]
elif self.country=="US":
url="https://waterservices.usgs.gov/nwis/iv/?format=json&sites={}¶meterCd=00060,00065&siteStatus=all".format(self.stationid)
self.log("us url={}".format(url))
try:
data=urllib.request.urlopen(url).read()
except:
self.log("Error reading url = {}".format(url))
return
jd=json.loads(data.decode("utf-8"))
for ts in jd["value"]["timeSeries"]:
if ts["variable"]["variableCode"][0]["variableID"]==45807202:
lat=ts["sourceInfo"]["geoLocation"]["geogLocation"]["latitude"]
lon=ts["sourceInfo"]["geoLocation"]["geogLocation"]["longitude"]
lastReading=ts["values"][0]["value"][0]["dateTime"]
stage_datum="unk"
newlevel=ts["values"][0]["value"][0]["value"]
break
self.log("River Level {} ={}".format(self.stationid,newlevel))
self.set_state(self.control_name,state=float(newlevel),attributes={"name":self.rivername,"stationID":self.stationid,"latitude":lat,"longitude":lon,"lastReading":lastReading, "stage_datum":stage_datum, "unit_of_measurement":"ft"})