Fix geizhals price parsing (#15990)

* fix geizhals price parsing

* Fix lint issue

* switch to the geizhals pypi package

* throttle updates

* update geizhals version

* initialize empty device

* minor changes to trigger another TravisCI test

* device => _device

* bump geizhals version
This commit is contained in:
Julian Kahnert 2018-08-27 09:39:11 +02:00 committed by Paulus Schoutsen
parent dec2d8d5b0
commit 2e9db1f5c4
2 changed files with 35 additions and 78 deletions

View File

@ -13,15 +13,15 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle from homeassistant.util import Throttle
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.const import (CONF_DOMAIN, CONF_NAME) from homeassistant.const import CONF_NAME
REQUIREMENTS = ['beautifulsoup4==4.6.3'] REQUIREMENTS = ['geizhals==0.0.7']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_DESCRIPTION = 'description' CONF_DESCRIPTION = 'description'
CONF_PRODUCT_ID = 'product_id' CONF_PRODUCT_ID = 'product_id'
CONF_REGEX = 'regex' CONF_LOCALE = 'locale'
ICON = 'mdi:coin' ICON = 'mdi:coin'
@ -31,13 +31,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_NAME): cv.string, vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_PRODUCT_ID): cv.positive_int, vol.Required(CONF_PRODUCT_ID): cv.positive_int,
vol.Optional(CONF_DESCRIPTION, default='Price'): cv.string, vol.Optional(CONF_DESCRIPTION, default='Price'): cv.string,
vol.Optional(CONF_DOMAIN, default='geizhals.de'): vol.In( vol.Optional(CONF_LOCALE, default='DE'): vol.In(
['geizhals.at', ['AT',
'geizhals.eu', 'EU',
'geizhals.de', 'DE',
'skinflint.co.uk', 'UK',
'cenowarka.pl']), 'PL']),
vol.Optional(CONF_REGEX, default=r'\D\s(\d*)[\,|\.](\d*)'): cv.string,
}) })
@ -46,22 +45,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
description = config.get(CONF_DESCRIPTION) description = config.get(CONF_DESCRIPTION)
product_id = config.get(CONF_PRODUCT_ID) product_id = config.get(CONF_PRODUCT_ID)
domain = config.get(CONF_DOMAIN) domain = config.get(CONF_LOCALE)
regex = config.get(CONF_REGEX)
add_entities([Geizwatch(name, description, product_id, domain, regex)], add_entities([Geizwatch(name, description, product_id, domain)],
True) True)
class Geizwatch(Entity): class Geizwatch(Entity):
"""Implementation of Geizwatch.""" """Implementation of Geizwatch."""
def __init__(self, name, description, product_id, domain, regex): def __init__(self, name, description, product_id, domain):
"""Initialize the sensor.""" """Initialize the sensor."""
from geizhals import Device, Geizhals
# internal
self._name = name self._name = name
self._geizhals = Geizhals(product_id, domain)
self._device = Device()
# external
self.description = description self.description = description
self.data = GeizParser(product_id, domain, regex) self.product_id = product_id
self._state = None
@property @property
def name(self): def name(self):
@ -76,73 +80,24 @@ class Geizwatch(Entity):
@property @property
def state(self): def state(self):
"""Return the best price of the selected product.""" """Return the best price of the selected product."""
return self._state return self._device.prices[0]
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
while len(self.data.prices) < 4: while len(self._device.prices) < 4:
self.data.prices.append("None") self._device.prices.append('None')
attrs = {'device_name': self.data.device_name, attrs = {'device_name': self._device.name,
'description': self.description, 'description': self.description,
'unit_of_measurement': self.data.unit_of_measurement, 'unit_of_measurement': self._device.price_currency,
'product_id': self.data.product_id, 'product_id': self.product_id,
'price1': self.data.prices[0], 'price1': self._device.prices[0],
'price2': self.data.prices[1], 'price2': self._device.prices[1],
'price3': self.data.prices[2], 'price3': self._device.prices[2],
'price4': self.data.prices[3]} 'price4': self._device.prices[3]}
return attrs return attrs
def update(self):
"""Get the latest price from geizhals and updates the state."""
self.data.update()
self._state = self.data.prices[0]
class GeizParser:
"""Pull data from the geizhals website."""
def __init__(self, product_id, domain, regex):
"""Initialize the sensor."""
# parse input arguments
self.product_id = product_id
self.domain = domain
self.regex = regex
# set some empty default values
self.device_name = ''
self.prices = [None, None, None, None]
self.unit_of_measurement = ''
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self): def update(self):
"""Update the device prices.""" """Get the latest price from geizhals and updates the state."""
import bs4 self._device = self._geizhals.parse()
import requests
import re
sess = requests.session()
request = sess.get('https://{}/{}'.format(self.domain,
self.product_id),
allow_redirects=True,
timeout=1)
soup = bs4.BeautifulSoup(request.text, 'html.parser')
# parse name
raw = soup.find_all('span', attrs={'itemprop': 'name'})
self.device_name = raw[1].string
# parse prices
prices = []
for tmp in soup.find_all('span', attrs={'class': 'gh_price'}):
matches = re.search(self.regex, tmp.string)
raw = '{}.{}'.format(matches.group(1),
matches.group(2))
prices += [float(raw)]
prices.sort()
self.prices = prices[1:]
# parse unit
price_match = soup.find('span', attrs={'class': 'gh_price'})
matches = re.search(r'€|£|PLN', price_match.string)
self.unit_of_measurement = matches.group()

View File

@ -163,7 +163,6 @@ batinfo==0.4.2
# beacontools[scan]==1.2.3 # beacontools[scan]==1.2.3
# homeassistant.components.device_tracker.linksys_ap # homeassistant.components.device_tracker.linksys_ap
# homeassistant.components.sensor.geizhals
# homeassistant.components.sensor.scrape # homeassistant.components.sensor.scrape
# homeassistant.components.sensor.sytadin # homeassistant.components.sensor.sytadin
beautifulsoup4==4.6.3 beautifulsoup4==4.6.3
@ -387,6 +386,9 @@ gTTS-token==1.1.1
# homeassistant.components.sensor.gearbest # homeassistant.components.sensor.gearbest
gearbest_parser==1.0.7 gearbest_parser==1.0.7
# homeassistant.components.sensor.geizhals
geizhals==0.0.7
# homeassistant.components.sensor.gitter # homeassistant.components.sensor.gitter
gitterpy==0.1.7 gitterpy==0.1.7