From b809f68c8511a8d2d103fefbca34ad7886fb1e2e Mon Sep 17 00:00:00 2001 From: Erik Kastelec Date: Mon, 22 Feb 2021 16:18:45 +0100 Subject: [PATCH] Minor changes in WemPortalSpider and logging, increased scan interval --- README.md | 3 +- custom_components/wemportal/manifest.json | 2 +- custom_components/wemportal/sensor.py | 7 ++-- custom_components/wemportal/wemportalapi.py | 45 ++++++++++++++++----- info.md | 8 +++- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8b244b4..9e7c800 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ Make sure to copy the exact name of the entity from the config example below, as Configuration variables: - `username`: Email address used for login into WEM Portal - `password`: : Email address used for login into WEM Portal -- `scan_interval (Optional)`: Defines update frequency. Optional and in seconds (defaults to 15 min, minimum value is 5 min). +- `scan_interval (Optional)`: Defines update frequency. Optional and in seconds (defaults to 30 min, minimum value is 15 + min). - `resources`: list of entities, which will be added to home-assistant Add the following to your `configuration.yaml` file: diff --git a/custom_components/wemportal/manifest.json b/custom_components/wemportal/manifest.json index 947e99b..3aa0960 100644 --- a/custom_components/wemportal/manifest.json +++ b/custom_components/wemportal/manifest.json @@ -4,7 +4,7 @@ "documentation": "https://github.com/erikkastelec/hass-WEM-Portal", "issue_tracker": "https://github.com/erikkastelec/hass-WEM-Portal/issues", "dependencies": [], - "version": "1.0.1", + "version": "1.0.2", "codeowners": [ "@erikkastelec" ], diff --git a/custom_components/wemportal/sensor.py b/custom_components/wemportal/sensor.py index 2b841af..9c60fdf 100644 --- a/custom_components/wemportal/sensor.py +++ b/custom_components/wemportal/sensor.py @@ -6,7 +6,7 @@ Configuration for this platform: sensor: - platform: wemportal - scan_interval: 900 + scan_interval: 1800 username: username password: password resources: @@ -364,7 +364,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Optional(CONF_NAME, default=DEFAULT_NAME): config_validation.string, - vol.Optional(CONF_SCAN_INTERVAL, default=timedelta(minutes=15)): config_validation.time_period, + vol.Optional(CONF_SCAN_INTERVAL, default=timedelta(minutes=30)): config_validation.time_period, vol.Required(CONF_USERNAME): config_validation.string, vol.Required(CONF_PASSWORD): config_validation.string, vol.Required(CONF_RESOURCES, default=[]): vol.All( @@ -391,7 +391,7 @@ async def async_update_data(): name="wem_portal_sensor", update_method=async_update_data, # Polling interval. Will only be polled if there are subscribers. - update_interval=max(config.get(CONF_SCAN_INTERVAL), timedelta(minutes=5)), + update_interval=max(config.get(CONF_SCAN_INTERVAL), timedelta(minutes=15)), ) # Fetch initial data so we have data when entities subscribe @@ -422,7 +422,6 @@ def __init__(self, coordinator, sensor_type, sensor_prefix): self._sensor_prefix = sensor_prefix self._entity_type = SENSOR_TYPES[self.type][0] self._name = SENSOR_TYPES[self.type][0] - # "{} {}".format(sensor_prefix, SENSOR_TYPES[self.type][0]) self._unit = SENSOR_TYPES[self.type][1] self._icon = SENSOR_TYPES[self.type][2] self._state = self.state diff --git a/custom_components/wemportal/wemportalapi.py b/custom_components/wemportal/wemportalapi.py index 86a9325..6e5c6e8 100644 --- a/custom_components/wemportal/wemportalapi.py +++ b/custom_components/wemportal/wemportalapi.py @@ -8,7 +8,7 @@ from scrapy import FormRequest, Spider from .const import ( - START_URLS + START_URLS, _LOGGER ) @@ -24,20 +24,30 @@ def fetch_data(self): """Call spider to crawl WEM Portal""" wemportalJob = scrapyscript.Job(WemPortalSpider, self.username, self.password) processor = scrapyscript.Processor(settings=None) - data = processor.run([wemportalJob])[0] + data = None + try: + data = processor.run([wemportalJob])[0] + except IndexError: + _LOGGER.exception('There was a problem with getting data from WEM Portal. If this problem persists, ' + 'open an issue at https://github.com/erikkastelec/hass-WEM-Portal/issues') + try: + if data['authErrorFlag']: + _LOGGER.exception('AuthenticationError: Could not login with provided username and password. Check if ' + 'your config contains the right credentials') + except KeyError: + """If authentication was successful""" + pass return data class WemPortalSpider(Spider): name = 'WemPortalSpider' start_urls = START_URLS - custom_settings = { - - } def __init__(self, username, password, **kw): self.username = username self.password = password + self.authErrorFlag = False super().__init__(**kw) def parse(self, response): @@ -45,20 +55,28 @@ def parse(self, response): return FormRequest.from_response(response, formdata={ 'ctl00$content$tbxUserName': self.username, - 'ctl00$content$tbxPassword': self.password + 'ctl00$content$tbxPassword': self.password, + 'Accept-Language': 'en-US,en;q=0.5' }, callback=self.navigate_to_expert_page) def navigate_to_expert_page(self, response): - form_data = self.generate_form_data(response) + if response.url == 'https://www.wemportal.com/Web/login.aspx?AspxAutoDetectCookieSupport=1': + _LOGGER.debug("Authhentication failed") + self.authErrorFlag = True + form_data = {} + else: + _LOGGER.debug("Authhentication successful") + form_data = self.generate_form_data(response) + _LOGGER.debug("Form data processed") return FormRequest(url='https://www.wemportal.com/Web/default.aspx', formdata=form_data, headers={ 'Content-Type': 'application/x-www-form-urlencoded', - 'Cookie': response.request.headers.getlist('Cookie')[0].decode("utf-8") + 'Cookie': response.request.headers.getlist('Cookie')[0].decode("utf-8"), + 'Accept-Language': 'en-US,en;q=0.5' }, method='POST', - callback=self.scrape_pages) def generate_form_data(self, response): @@ -84,6 +102,9 @@ def generate_form_data(self, response): } def scrape_pages(self, response): + if self.authErrorFlag: + yield {'authErrorFlag': True} + _LOGGER.debug("Scraping page") output = {} for div in response.xpath('//div[@class="RadPanelBar RadPanelBar_Default rpbSimpleData"]'): spans = div.xpath('.//span/text()').extract() @@ -94,5 +115,9 @@ def scrape_pages(self, response): output[index] = int(spans[i + 1].split(' ')[0]) except ValueError: output[index] = spans[i + 1].split(' ')[0] - + """Edge cases""" + if ( + index == 'heat_pump-power_requirement' or index == 'heat_pump_frequency_compressor' or index == 'heat_pump_frequency_compressor') and \ + output[index] == 'off': + output[index] = 0 yield output diff --git a/info.md b/info.md index ed8e802..a7c6050 100644 --- a/info.md +++ b/info.md @@ -6,6 +6,10 @@ Custom component for retrieving sensor information from Weishaupt WEM Portal. Component uses webscraping to get all the sensor data from the Weishaupt WEM Portal (Expert view) and makes it available in [Home Assistant](https://home-assistant.io/). +## Installation + +Full restart of the Home Assistant is required. Restarting from GUI won't work, due to missing dependencies. + ## Configuration Entities specified under resources are added to home-assistant. @@ -16,7 +20,7 @@ Configuration variables: - `username`: Email address used for login into WEM Portal - `password`: : Email address used for login into WEM Portal -- `scan_interval (Optional)`: Defines update frequency. Optional and in seconds (defaults to 15 min, minimum value is 5 +- `scan_interval (Optional)`: Defines update frequency. Optional and in seconds (defaults to 30 min, minimum value is 15 min). - `resources`: list of entities, which will be added to home-assistant @@ -26,7 +30,7 @@ Add the following to your `configuration.yaml` file: # Example configuration.yaml entry sensor: - platform: wemportal - #scan_interval: 900 + #scan_interval: 1800 username: your_username password: your_password resources: