Skip to content

Commit

Permalink
Minor changes in WemPortalSpider and logging, increased scan interval
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkastelec committed Feb 22, 2021
1 parent f6fa63f commit b809f68
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wemportal/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
7 changes: 3 additions & 4 deletions custom_components/wemportal/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Configuration for this platform:
sensor:
- platform: wemportal
scan_interval: 900
scan_interval: 1800
username: username
password: password
resources:
Expand Down Expand Up @@ -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(
Expand 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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 35 additions & 10 deletions custom_components/wemportal/wemportalapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from scrapy import FormRequest, Spider

from .const import (
START_URLS
START_URLS, _LOGGER
)


Expand All @@ -24,41 +24,59 @@ 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):
"""Login to WEM Portal"""
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):
Expand All @@ -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()
Expand All @@ -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
8 changes: 6 additions & 2 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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:
Expand Down

0 comments on commit b809f68

Please sign in to comment.