8 from suntime import Sun
10 parser = argparse.ArgumentParser()
11 parser.add_argument("-s",
15 help="Print Status Information",
17 parser.add_argument("-f",
21 help="Force retrieval of Power value",
23 args = parser.parse_args()
26 def isDaylight(lat, lon, toleranceSeconds):
29 sunriseTimestamp = int(sun.get_local_sunrise_time().timestamp())
30 sunsetTimestamp = int(sun.get_local_sunset_time().timestamp())
31 nowTimestamp = int(time.time())
33 if ((sunriseTimestamp + toleranceSeconds) < nowTimestamp) and (
34 (sunsetTimestamp - toleranceSeconds) > nowTimestamp):
40 def getCurrentPower(userName, password, stationId):
41 with requests.Session() as s:
43 r = s.post('https://www.envertecportal.com/apiaccount/login',
51 'https://www.envertecportal.com/ApiStations/getStationInfo',
53 'stationId': stationId
55 timeout=(20, 60)).json()
56 power = r['Data']['Power']
58 r = s.post('https://www.envertecportal.com/apiAccount/Logout',
61 # connect timeouts occur so frequently since the portal relaunch,
62 # ignore them for the time beeing completely
63 except requests.exceptions.ConnectTimeout as eTimeout:
65 except requests.exceptions.RequestException as e:
72 # use our stateFile to determine if we have a state change
73 # used to decide if we print something - thus generate a mail - later on
74 def stateCheck(newState, stateFile):
76 with open(stateFile, "r") as f:
78 except FileNotFoundError:
81 if newState == oldState:
85 with open(stateFile, "w") as f:
91 # read configuration file
92 conf = configparser.ConfigParser()
93 conf.read('portalmonitor.ini')
95 # retrieve current power value as reported by envertecportal
96 if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
97 conf['config'].getint('toleranceSeconds')) or args.force:
98 currentPower = getCurrentPower(conf['config']['userName'],
99 conf['config']['password'],
100 conf['config']['stationId'])
103 print(f"Current Power: {currentPower}")
105 if currentPower == 0:
106 if stateCheck('FAILED', conf['config']['stateFile']):
107 print('Error: Power dropped to 0 but we should have daylight!')
110 if stateCheck('OK', conf['config']['stateFile']):
111 print(f"Resolved: Inverter reports {currentPower}W")