From efae0cb7f46d3d747dd314d390b6876275cd583b Mon Sep 17 00:00:00 2001 From: David Barrera Date: Sun, 3 Feb 2019 20:59:13 -0500 Subject: [PATCH 1/2] 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. --- homeassistant/components/sensor/scrape.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/scrape.py b/homeassistant/components/sensor/scrape.py index 6dd52789f714ac..c82edb4058e87d 100644 --- a/homeassistant/components/sensor/scrape.py +++ b/homeassistant/components/sensor/scrape.py @@ -26,6 +26,7 @@ CONF_ATTR = 'attribute' CONF_SELECT = 'select' +CONF_INDEX = 'index' DEFAULT_NAME = 'Web scrape' DEFAULT_VERIFY_SSL = True @@ -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): 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}), @@ -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) @@ -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 @@ -117,11 +122,14 @@ def update(self): raw_data = BeautifulSoup(self.rest.data, 'html.parser') _LOGGER.debug(raw_data) + if self._index is None: + self._index = 0 + 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") From 85c35bc902f368974cbc71b343f269f1100ee440 Mon Sep 17 00:00:00 2001 From: David Barrera Date: Fri, 15 Feb 2019 09:03:39 -0500 Subject: [PATCH 2/2] Set default value for index to avoid later checks --- homeassistant/components/sensor/scrape.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/scrape.py b/homeassistant/components/sensor/scrape.py index c82edb4058e87d..70dfae392bef5b 100644 --- a/homeassistant/components/sensor/scrape.py +++ b/homeassistant/components/sensor/scrape.py @@ -35,7 +35,7 @@ vol.Required(CONF_RESOURCE): cv.string, vol.Required(CONF_SELECT): cv.string, vol.Optional(CONF_ATTR): cv.string, - vol.Optional(CONF_INDEX): cv.positive_int, + 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}), @@ -122,9 +122,6 @@ def update(self): raw_data = BeautifulSoup(self.rest.data, 'html.parser') _LOGGER.debug(raw_data) - if self._index is None: - self._index = 0 - try: if self._attr is not None: value = raw_data.select(self._select)[self._index][self._attr]