diff --git a/setup.py b/setup.py index 56af243..35fa896 100644 --- a/setup.py +++ b/setup.py @@ -2,13 +2,13 @@ setup( name = 'stookalert', packages = ['stookalert'], - version = '0.1.1', + version = '0.1.2', license='MIT', description = 'Stookalert package', author = 'fwestenberg', author_email = '', url = 'https://github.com/fwestenberg/stookalert', - download_url = 'https://github.com/fwestenberg/stookalert/archive/v_011.tar.gz', + download_url = 'https://github.com/fwestenberg/stookalert/archive/v_012.tar.gz', keywords = ['Stookalert', 'Home-Assistant'], install_requires=[ 'requests', diff --git a/stookalert/rivm_stookalert.py b/stookalert/rivm_stookalert.py index 9526033..750295b 100644 --- a/stookalert/rivm_stookalert.py +++ b/stookalert/rivm_stookalert.py @@ -1,11 +1,13 @@ import json -import requests -from datetime import datetime, timedelta import logging +from datetime import datetime, timedelta + +import requests -PROVINCES = ["groningen", "friesland", "drenthe", "overijssel", "gelderland", "utrecht", "noord-holland", "zuid-holland", "zeeland", "noord-brabant", "limburg", "flevoland"] UPDATEHOUR = 12 -RESETHOUR = 3 +RESETHOUR = 3 +NAME = "naam" +VALUE = "waarde" _LOGGER = logging.getLogger(__name__) class stookalert(object): @@ -13,13 +15,13 @@ class stookalert(object): def __init__(self, province): self._state = None self._alerts = {} - self._province = self.check_province(province) + self._province = province.lower() self._last_updated = None if self._province is not None: _LOGGER.info(f"Setting up Stookalert for province {province}") else: - _LOGGER.info(f"Invalid province {province}. Please select one of the following: {PROVINCES}") + _LOGGER.info("Please provide a province name") def get_alert(self): alerts = self.request() @@ -28,11 +30,8 @@ def get_alert(self): return for a in alerts: - province = a.get("naam", "").lower() - value = a.get("waarde", None) - - if not self.check_province(province): - continue + province = a.get(NAME, "").lower() + value = a.get(VALUE, None) self._alerts[province] = value @@ -41,12 +40,12 @@ def get_alert(self): def request(self): try: - response = requests.get(self.get_url()) + response = requests.get(self.get_url(), timeout=10) self._last_updated = datetime.now() json_response = json.loads(response.text) - return sorted(json_response, key = lambda i: i['naam']) + return sorted(json_response, key = lambda i: i[NAME]) except requests.exceptions.RequestException: _LOGGER.error("Error getting Stookalert data") @@ -59,11 +58,3 @@ def get_url(self): return f"https://www.rivm.nl/media/lml/stookalert/stookalert_noalert.json" return f"https://www.rivm.nl/media/lml/stookalert/stookalert_{updateDay.strftime('%Y%m%d')}.json" - - def check_province(self, province): - find_province = province.lower() - - if find_province in PROVINCES: - return find_province - else: - return None \ No newline at end of file