-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
138 lines (115 loc) · 4.23 KB
/
__init__.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
#!/usr/bin/env python3
#########################################################################
# Copyright 2015 Sebastian Kuhn [email protected]
#########################################################################
# This file is part of SmartHome.py. http://mknx.github.io/smarthome/
#
# SmartHome.py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SmartHome.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SmartHome.py. If not, see <http://www.gnu.org/licenses/>.
#########################################################################
import logging
from datetime import datetime, timedelta
try:
from http.cookiejar import CookieJar
except ImportError:
from cookielib import CookieJar
try:
from urllib.parse import urlencode
from urllib.request import build_opener
from urllib.request import HTTPRedirectHandler
from urllib.request import HTTPHandler
from urllib.request import HTTPSHandler
from urllib.request import HTTPCookieProcessor
from urllib.request import HTTPError
except ImportError:
import urllib2
from .NibeHTMLParser import NibeHTMLParser
logger = logging.getLogger('')
class NibeUplink():
def __init__(self, smarthome, nibe_email, nibe_password, nibe_system, nibe_update_cyle=10):
logger.warn('Init NibeUplink')
self._sh = smarthome
self._items = dict()
self.nibe_email = nibe_email
self.nibe_password = nibe_password
self.nibe_system = nibe_system
self.nibe_update_cyle = nibe_update_cyle
self.login_url = "https://www.nibeuplink.com/Login"
self.service_url = "https://www.nibeuplink.com/System/"+self.nibe_system+"/Status/ServiceInfo"
self.cookies = CookieJar()
self.opener = None
self.parser = None
self.response = None
def login(self):
values = {'Email' : self.nibe_email,
'Password' : self.nibe_password,
'returnUrl' : ''
}
data = urlencode(values)
binary_data = data.encode('ascii')
self.opener = build_opener(
HTTPRedirectHandler(),
HTTPHandler(debuglevel=0),
HTTPSHandler(debuglevel=0),
HTTPCookieProcessor(self.cookies))
self.response = self.opener.open(self.login_url, binary_data)
def run(self):
self.alive = True
# Log into NibeUplink
values = {'Email' : self.nibe_email,
'Password' : self.nibe_password,
'returnUrl' : ''
}
data = urlencode(values)
binary_data = data.encode('ascii')
self.opener = build_opener(
HTTPRedirectHandler(),
HTTPHandler(debuglevel=0),
HTTPSHandler(debuglevel=0),
HTTPCookieProcessor(self.cookies))
self.parser = NibeHTMLParser()
try:
self.response = self.opener.open(self.login_url, binary_data)
except HTTPError as e:
logger.warn('Could not login')
else:
logger.warn('Logged in')
self._sh.scheduler.add('NibeUplink', self._update_values, prio=5, cycle=self.nibe_update_cyle)
def stop(self):
self.alive = False
def parse_item(self, item):
if 'nibe_reg' in item.conf:
self._items[item.conf['nibe_reg']] = item
else:
return None
def update_item(self, item, caller=None, source=None, dest=None):
logger.warn('NibeUplink: update_item():'+item)
return None
def _update_values(self):
self.login()
self.response = self.opener.open(self.service_url)
page = self.response.read()
try:
self.parser.feed(str(page))
except Exception as e:
logger.warn('Fehler:', e.args)
tags = self.parser.getDataArray()
for k,v in self._items.items():
item = self._items[k]
value = tags[k]
try:
item(float(value), 'NibeUplink')
except:
logger.warn('Problem Updating value: ' + k + ':' + tags[k])
else:
logger.debug('Updated value: ' + k + ':' + tags[k])