This repository has been archived by the owner on Sep 29, 2022. It is now read-only.
forked from danieldotnl/hass-multiscrape
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensor.py
402 lines (340 loc) · 14 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
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
"""Support for RESTful API sensors."""
import logging
from xml.parsers.expat import ExpatError
from bs4 import BeautifulSoup
import requests
from requests import Session
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import voluptuous as vol
import xmltodict
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_AUTHENTICATION,
CONF_FORCE_UPDATE,
CONF_HEADERS,
CONF_METHOD,
CONF_NAME,
CONF_PASSWORD,
CONF_PAYLOAD,
CONF_RESOURCE,
CONF_RESOURCE_TEMPLATE,
CONF_TIMEOUT,
CONF_UNIT_OF_MEASUREMENT,
CONF_USERNAME,
CONF_VALUE_TEMPLATE,
CONF_VERIFY_SSL,
HTTP_BASIC_AUTHENTICATION,
HTTP_DIGEST_AUTHENTICATION,
)
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
DEFAULT_METHOD = "GET"
DEFAULT_NAME = "Multiscrape Sensor"
DEFAULT_VERIFY_SSL = True
DEFAULT_FORCE_UPDATE = False
DEFAULT_TIMEOUT = 10
DEFAULT_PARSER = "lxml"
CONF_SELECTORS = "selectors"
CONF_ATTR = "attribute"
CONF_SELECT = "select"
CONF_INDEX = "index"
CONF_PARSER = "parser"
# OT 19.8.2020 Add prelogin parameters
CONF_PRELOGIN = "prelogin"
CONF_PRELOGINPAGE = "preloginpage"
CONF_PRELOGINFORM = "preloginform"
CONF_USERNAMEFIELD = "username_field"
CONF_PASSWORDFIELD = "password_field"
DEFAULT_USERNAMEFIELD = "username"
DEFAULT_PASSWORDFIELD = "password"
METHODS = ["POST", "GET"]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Exclusive(CONF_RESOURCE, CONF_RESOURCE): cv.url,
vol.Exclusive(CONF_RESOURCE_TEMPLATE, CONF_RESOURCE): cv.template,
vol.Optional(CONF_AUTHENTICATION): vol.In(
[HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION]
),
vol.Optional(CONF_HEADERS): vol.Schema({cv.string: cv.string}),
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): vol.In(METHODS),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PAYLOAD): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
vol.Optional(CONF_FORCE_UPDATE, default=DEFAULT_FORCE_UPDATE): cv.boolean,
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
vol.Optional(CONF_PARSER, default=DEFAULT_PARSER): cv.string,
# OT 19.8.2020
vol.Optional(CONF_PRELOGIN, default={}): vol.Schema({
vol.Required(CONF_PRELOGINPAGE): cv.url,
vol.Required(CONF_PRELOGINFORM): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_USERNAMEFIELD, default=DEFAULT_USERNAMEFIELD): cv.string,
vol.Optional(CONF_PASSWORDFIELD, default=DEFAULT_PASSWORDFIELD): cv.string,
})
}
)
SENSOR_SCHEMA = vol.Schema(
{
vol.Required(CONF_SELECT): cv.string,
vol.Optional(CONF_ATTR): cv.string,
vol.Optional(CONF_INDEX, default=0): cv.positive_int,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_SELECTORS): cv.schema_with_slug_keys(SENSOR_SCHEMA)}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the RESTful sensor."""
name = config.get(CONF_NAME)
resource = config.get(CONF_RESOURCE)
resource_template = config.get(CONF_RESOURCE_TEMPLATE)
method = config.get(CONF_METHOD)
payload = config.get(CONF_PAYLOAD)
verify_ssl = config.get(CONF_VERIFY_SSL)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
headers = config.get(CONF_HEADERS)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template = config.get(CONF_VALUE_TEMPLATE)
selectors = config.get(CONF_SELECTORS)
# OT 19.8.2020
prelogin = config.get(CONF_PRELOGIN)
force_update = config.get(CONF_FORCE_UPDATE)
timeout = config.get(CONF_TIMEOUT)
parser = config.get(CONF_PARSER)
if value_template is not None:
value_template.hass = hass
if resource_template is not None:
resource_template.hass = hass
resource = resource_template.render()
if username and password:
if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
auth = HTTPDigestAuth(username, password)
else:
auth = HTTPBasicAuth(username, password)
else:
auth = None
rest = RestData(method, resource, auth, headers, payload, verify_ssl, timeout)
rest.update()
if rest.data is None:
raise PlatformNotReady
# Must update the sensor now (including fetching the rest resource) to
# ensure it's updating its state.
add_entities(
[
MultiscrapeSensor(
hass,
rest,
name,
unit,
value_template,
selectors,
force_update,
resource_template,
parser,
prelogin, # OT 19.8.2020
)
],
True,
)
class MultiscrapeSensor(Entity):
"""Implementation of a REST sensor."""
def __init__(
self,
hass,
rest,
name,
unit_of_measurement,
value_template,
selectors,
force_update,
resource_template,
parser,
prelogin, # OT 19.8.2020
):
"""Initialize the sensor."""
self._hass = hass
self.rest = rest
self._name = name
self._state = None
self._unit_of_measurement = unit_of_measurement
self._value_template = value_template
self._selectors = selectors
self._attributes = None
self._force_update = force_update
self._resource_template = resource_template
self._parser = parser
self._prelogin = prelogin # OT 19.8.2020
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
@property
def available(self):
"""Return if the sensor data are available."""
return self.rest.data is not None
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def force_update(self):
"""Force update."""
return self._force_update
def update(self):
# TODO: Make logic to detect when already logged in. Now if loginform not found assumes we are logged in.
if self._prelogin:
_LOGGER.debug("Prelogin started")
_LOGGER.debug("Preloginpage: %s", self._prelogin[CONF_PRELOGINPAGE])
previousmethod = self.rest._method
self.rest.set_url(self._prelogin[CONF_PRELOGINPAGE])
self.rest.update()
if self.rest.data is None:
_LOGGER.error("Unable to retrieve prelogin data for %s", self._name)
return
# TODO: value is only used to debug :(
value = self.rest.data
#_LOGGER.debug("Prelogin page fetched from resource: %s", value[:35000])
result = BeautifulSoup(self.rest.data, self._parser)
# Look for the login form checking these attributes for a match in order: 'name', 'id', 'class', 'action'.
form = result.find('form', attrs={'name':self._prelogin[CONF_PRELOGINFORM]})
if form is None:
form = result.find('form', attrs={'id':self._prelogin[CONF_PRELOGINFORM]})
if form is None:
form = result.find('form', attrs={'class':self._prelogin[CONF_PRELOGINFORM]})
if form is None:
form = result.find('form', attrs={'action':self._prelogin[CONF_PRELOGINFORM]})
if form is None:
_LOGGER.debug("Unable to find form with name %s, assume we are already logged in", self._prelogin[CONF_PRELOGINFORM])
else:
# Find all fields and extract them to formdata
#
fields = form.findAll('input')
formdata = dict( (field.get('name'), field.get('value')) for field in fields)
# Get username and password from config
formdata[self._prelogin[CONF_USERNAMEFIELD]] = self._prelogin[CONF_USERNAME]
formdata[self._prelogin[CONF_PASSWORDFIELD]] = self._prelogin[CONF_PASSWORD]
# Set form data to rest request and post it
self.rest.set_request_data(formdata)
# Method is read from the form
self.rest._method = form['method']
self.rest.update()
# TODO: value is only used to debug :(
value = self.rest.data
#_LOGGER.debug("Prelogin page fetched after login from resource: %s", value[:2500])
# Set url back to normal
#self.rest.set_url(self._resource_template.render())
# TODO: Set method back to original
#self.rest._method = self.method
self.rest._method = previousmethod
_LOGGER.debug("Prelogin ended")
else:
# If not prelogin do normal fetch
if self._resource_template is not None:
self.rest.set_url(self._resource_template.render())
self.rest.update()
if self.rest.data is None:
_LOGGER.error("Unable to retrieve data for %s", self._name)
return
value = self.rest.data
#_LOGGER.debug("Data fetched from resource: %s", value)
if self._selectors:
result = BeautifulSoup(self.rest.data, self._parser)
result.prettify()
#_LOGGER.debug("Data parsed by BeautifulSoup: %s", result)
self._attributes = {}
if value:
for device, device_config in self._selectors.items():
name = device_config.get(CONF_NAME)
select = device_config.get(CONF_SELECT)
attr = device_config.get(CONF_ATTR)
index = device_config.get(CONF_INDEX)
value_template = device_config.get(CONF_VALUE_TEMPLATE)
unit = device_config.get(CONF_UNIT_OF_MEASUREMENT)
try:
if attr is not None:
value = result.select(select)[index][attr]
else:
tag = result.select(select)[index]
if tag.name in ("style", "script", "template"):
value = tag.string
else:
value = tag.text
_LOGGER.debug("Sensor %s selected: %s", name, value)
except IndexError as e:
_LOGGER.error("Sensor %s was unable to extract data from HTML", name)
_LOGGER.debug("Exception: %s", e)
continue
if value_template is not None:
if value_template is not None:
value_template.hass = self._hass
self._attributes[name] = value_template.render_with_possible_json_value(
value, None
)
else:
self._attributes[name] = value
self._state = "None"
# OT 21.8.2020
# TODO: last update would be nice
#self._attributes['updated']
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._attributes
class RestData:
"""Class for handling the data retrieval."""
def __init__(
self, method, resource, auth, headers, data, verify_ssl, timeout=DEFAULT_TIMEOUT
):
"""Initialize the data object."""
self._method = method
self._resource = resource
self._auth = auth
self._headers = headers
self._request_data = data
self._verify_ssl = verify_ssl
self._timeout = timeout
self._http_session = Session()
self.data = None
self.headers = None
def __del__(self):
"""Destroy the http session on destroy."""
self._http_session.close()
def set_url(self, url):
"""Set url."""
self._resource = url
def set_request_data(self, request_data):
"""Set request data."""
self._request_data = request_data
def update(self):
"""Get the latest data from REST service with provided method."""
_LOGGER.debug("Updating from %s", self._resource)
try:
response = self._http_session.request(
self._method,
self._resource,
headers=self._headers,
auth=self._auth,
data=self._request_data,
timeout=self._timeout,
verify=self._verify_ssl,
)
self.data = response.text
self.headers = response.headers
except requests.exceptions.RequestException as ex:
_LOGGER.error("Error fetching data: %s failed with %s", self._resource, ex)
self.data = None
self.headers = None