forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
238 lines (198 loc) · 7.75 KB
/
sensor.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
"""Sensor for Suez Water Consumption data."""
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, VOLUME_LITERS
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
CONF_COUNTER_ID = 'counter_id'
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=720)
SCAN_INTERVAL = timedelta(minutes=720)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_COUNTER_ID): cv.string,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the sensor platform."""
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
counter_id = config.get(CONF_COUNTER_ID)
_LOGGER.debug(
"Username is %s and counter_id is %s.",
username, counter_id
)
add_devices([SuezClient(username, password, counter_id)], True)
class SuezClient(Entity):
"""Global variables."""
BASE_URI = 'https://www.toutsurmoneau.fr'
API_ENDPOINT_LOGIN = '/mon-compte-en-ligne/je-me-connecte'
API_ENDPOINT_DATA = '/mon-compte-en-ligne/statJData/'
API_ENDPOINT_HISTORY = '/mon-compte-en-ligne/statMData/'
"""Representation of a Sensor."""
def __init__(self, username, password, counter_id):
"""Initialize the data object."""
self._name = "Suez Water Client"
self._username = username
self._password = password
self._counter_id = counter_id
self._token = ''
self._headers = {}
self._attributes = {}
self.data = {}
self.success = False
self._state = 0
self._icon = 'mdi:water-pump'
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return VOLUME_LITERS
@property
def state_attributes(self):
"""Return the state attributes."""
return self._attributes
@property
def icon(self):
"""Return the icon of the sensor."""
return self._icon
def _get_token(self):
import requests
import re
headers = {
'Accept': "application/json, text/javascript, */*; q=0.01",
'Content-Type': 'application/x-www-form-urlencoded',
'Accept-Language': 'fr,fr-FR;q=0.8,en;q=0.6',
'User-Agent': 'curl/7.54.0',
'Connection': 'keep-alive',
'Cookie': ''
}
url = self.BASE_URI+self.API_ENDPOINT_LOGIN
response = requests.get(url, headers=headers)
headers['Cookie'] = ""
for key in response.cookies.get_dict():
if headers['Cookie']:
headers['Cookie'] += "; "
headers['Cookie'] += key + "=" + response.cookies[key]
phrase = re.compile('_csrf_token" value="(.*)" />')
result = phrase.search(response.content.decode('utf-8'))
self._token = result.group(1)
_LOGGER.debug("Le token is %s", self._token)
self._headers = headers
def _get_cookie(self):
import requests
login = requests.Session()
data = {
'_username': self._username,
'_password': self._password,
'_csrf_token': self._token,
'signin[username]': self._username,
'signin[password]': None,
'tsme_user_login[_username]': self._username,
'tsme_user_login[_password]': self._password
}
url = self.BASE_URI+self.API_ENDPOINT_LOGIN
login.post(url, headers=self._headers, data=data)
_LOGGER.debug("Cookie is %s", login.cookies.get("eZSESSID"))
self._headers['Cookie'] = ''
self._headers['Cookie'] = 'eZSESSID='+login.cookies.get("eZSESSID")
def _fetch_data(self):
"""Fetch latest data from Suez."""
import datetime
import requests
now = datetime.datetime.now()
today_year = now.strftime("%Y")
today_month = now.strftime("%m")
yesterday = datetime.datetime.now() - datetime.timedelta(1)
yesterday_year = yesterday.strftime('%Y')
yesterday_month = yesterday.strftime('%m')
yesterday_day = yesterday.strftime('%d')
url = self.BASE_URI+self.API_ENDPOINT_DATA
url += '{}/{}/{}'.format(
yesterday_year,
yesterday_month, self._counter_id
)
data = requests.get(url, headers=self._headers)
try:
self._state = int(float(data.json()[int(
yesterday_day)-1][1])*1000)
self.success = True
except ValueError:
_LOGGER.debug("Issue with this yesterday data")
pass
try:
if yesterday_month != today_month:
url = self.BASE_URI+self.API_ENDPOINT_DATA
url += '{}/{}/{}'.format(
today_year,
today_month, self._counter_id
)
_LOGGER.debug("Getting data for previous month")
data = requests.get(url, headers=self._headers)
self._attributes['thisMonthConsumption'] = {}
for item in data.json():
self._attributes['thisMonthConsumption'][item[0]] = int(
float(item[1])*1000)
except ValueError:
_LOGGER.debug("Issue with this month data")
pass
try:
if int(today_month) == 1:
last_month = 12
last_month_year = int(today_year) - 1
else:
last_month = int(today_month) - 1
last_month_year = today_year
url = self.BASE_URI+self.API_ENDPOINT_DATA
url += '{}/{}/{}'.format(
last_month_year, last_month,
self._counter_id
)
_LOGGER.debug("Getting data for previous month")
data = requests.get(url, headers=self._headers)
self._attributes['previousMonthConsumption'] = {}
for item in data.json():
self._attributes['previousMonthConsumption'][item[0]] = int(
float(item[1])*1000)
except ValueError:
_LOGGER.debug("Issue with this previous month data")
pass
try:
url = self.BASE_URI+self.API_ENDPOINT_HISTORY
url += '{}'.format(self._counter_id)
data = requests.get(url, headers=self._headers)
fetched_data = data.json()
self._attributes['highestMonthlyConsumption'] = int(
float(fetched_data[-1])*1000)
fetched_data.pop()
self._attributes['lastYearOverAll'] = int(
float(fetched_data[-1])*1000)
fetched_data.pop()
self._attributes['thisYearOverAll'] = int(
float(fetched_data[-1])*1000)
fetched_data.pop()
self._attributes['history'] = {}
for item in fetched_data:
self._attributes['history'][item[3]] = int(
float(item[1])*1000)
_LOGGER.debug("_attributes est %s", self._attributes)
except ValueError:
_LOGGER.debug("Issue with history data")
raise
def update(self):
"""Return the latest collected data from Linky."""
self._get_token()
self._get_cookie()
self._fetch_data()
if not self.success:
return
_LOGGER.debug("Suez data state is: %s", self._state)