Skip to content

Commit

Permalink
Add index parameter to scrape sensor (#21084)
Browse files Browse the repository at this point in the history
* Add index parameter to scrape sensor

The scrape sensor selects the first element of the list returned by
BeautifulSoup. This commit adds an optional index parameter to allow the
selection of a different element from the list of results. To make this
a non-breaking change, if no index value is configured, the sensor
defaults to the previous behaviour of returning the first element.

* Set default value for index to avoid later checks
  • Loading branch information
davidbb authored and fabaff committed Feb 16, 2019
1 parent 539d24d commit 4509cae
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions homeassistant/components/sensor/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

CONF_ATTR = 'attribute'
CONF_SELECT = 'select'
CONF_INDEX = 'index'

DEFAULT_NAME = 'Web scrape'
DEFAULT_VERIFY_SSL = True
Expand All @@ -34,6 +35,7 @@
vol.Required(CONF_RESOURCE): cv.string,
vol.Required(CONF_SELECT): cv.string,
vol.Optional(CONF_ATTR): cv.string,
vol.Optional(CONF_INDEX, default=0): cv.positive_int,
vol.Optional(CONF_AUTHENTICATION):
vol.In([HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION]),
vol.Optional(CONF_HEADERS): vol.Schema({cv.string: cv.string}),
Expand All @@ -56,6 +58,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
verify_ssl = config.get(CONF_VERIFY_SSL)
select = config.get(CONF_SELECT)
attr = config.get(CONF_ATTR)
index = config.get(CONF_INDEX)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
Expand All @@ -77,19 +80,21 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
raise PlatformNotReady

add_entities([
ScrapeSensor(rest, name, select, attr, value_template, unit)], True)
ScrapeSensor(rest, name, select, attr, index, value_template, unit)],
True)


class ScrapeSensor(Entity):
"""Representation of a web scrape sensor."""

def __init__(self, rest, name, select, attr, value_template, unit):
def __init__(self, rest, name, select, attr, index, value_template, unit):
"""Initialize a web scrape sensor."""
self.rest = rest
self._name = name
self._state = None
self._select = select
self._attr = attr
self._index = index
self._value_template = value_template
self._unit_of_measurement = unit

Expand Down Expand Up @@ -119,9 +124,9 @@ def update(self):

try:
if self._attr is not None:
value = raw_data.select(self._select)[0][self._attr]
value = raw_data.select(self._select)[self._index][self._attr]
else:
value = raw_data.select(self._select)[0].text
value = raw_data.select(self._select)[self._index].text
_LOGGER.debug(value)
except IndexError:
_LOGGER.error("Unable to extract data from HTML")
Expand Down

0 comments on commit 4509cae

Please sign in to comment.