Add optional attribute option to scrape sensor (#10001)

* Add optional attribute option to scrape sensor

* Rename attribute variable to attr
This commit is contained in:
Martin Eberhardt 2017-10-21 16:03:29 +02:00 committed by Fabian Affolter
parent ce1a2cc2a6
commit 222cc4c393

View file

@ -21,6 +21,7 @@ REQUIREMENTS = ['beautifulsoup4==4.6.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_SELECT = 'select' CONF_SELECT = 'select'
CONF_ATTR = 'attribute'
DEFAULT_NAME = 'Web scrape' DEFAULT_NAME = 'Web scrape'
DEFAULT_VERIFY_SSL = True DEFAULT_VERIFY_SSL = True
@ -28,6 +29,7 @@ DEFAULT_VERIFY_SSL = True
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.string, vol.Required(CONF_RESOURCE): cv.string,
vol.Required(CONF_SELECT): cv.string, vol.Required(CONF_SELECT): cv.string,
vol.Optional(CONF_ATTR): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
@ -43,6 +45,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
payload = auth = headers = None payload = auth = headers = None
verify_ssl = config.get(CONF_VERIFY_SSL) verify_ssl = config.get(CONF_VERIFY_SSL)
select = config.get(CONF_SELECT) select = config.get(CONF_SELECT)
attr = config.get(CONF_ATTR)
unit = config.get(CONF_UNIT_OF_MEASUREMENT) unit = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template = config.get(CONF_VALUE_TEMPLATE) value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is not None: if value_template is not None:
@ -56,19 +59,20 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False return False
add_devices([ add_devices([
ScrapeSensor(hass, rest, name, select, value_template, unit) ScrapeSensor(hass, rest, name, select, attr, value_template, unit)
], True) ], True)
class ScrapeSensor(Entity): class ScrapeSensor(Entity):
"""Representation of a web scrape sensor.""" """Representation of a web scrape sensor."""
def __init__(self, hass, rest, name, select, value_template, unit): def __init__(self, hass, rest, name, select, attr, value_template, unit):
"""Initialize a web scrape sensor.""" """Initialize a web scrape sensor."""
self.rest = rest self.rest = rest
self._name = name self._name = name
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN
self._select = select self._select = select
self._attr = attr
self._value_template = value_template self._value_template = value_template
self._unit_of_measurement = unit self._unit_of_measurement = unit
@ -95,7 +99,10 @@ class ScrapeSensor(Entity):
raw_data = BeautifulSoup(self.rest.data, 'html.parser') raw_data = BeautifulSoup(self.rest.data, 'html.parser')
_LOGGER.debug(raw_data) _LOGGER.debug(raw_data)
value = raw_data.select(self._select)[0].text if self._attr is not None:
value = raw_data.select(self._select)[0][self._attr]
else:
value = raw_data.select(self._select)[0].text
_LOGGER.debug(value) _LOGGER.debug(value)
if self._value_template is not None: if self._value_template is not None: