Skip to content

Commit

Permalink
Removed province filter, added timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
fwestenberg committed Dec 31, 2019
1 parent fd43b04 commit 5b5177a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 12 additions & 21 deletions stookalert/rivm_stookalert.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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):

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()
Expand All @@ -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

Expand All @@ -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")

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

0 comments on commit 5b5177a

Please sign in to comment.