Skip to content

Commit

Permalink
Reworked weather error handling #1310
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Mar 7, 2021
1 parent b0c311d commit 654828a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
Binary file modified docs/locale/nl/LC_MESSAGES/faq.mo
Binary file not shown.
Binary file modified docs/locale/nl/LC_MESSAGES/plugins.mo
Binary file not shown.
13 changes: 5 additions & 8 deletions dsmr_weather/services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from decimal import Decimal
import logging

from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from django.conf import settings
import requests
Expand All @@ -18,7 +17,7 @@ def run(scheduled_process):
try:
temperature_reading = get_temperature_from_api()
except Exception as error:
logger.exception(error)
logger.error('Buienradar: {}'.format(error))

scheduled_process.delay(timezone.timedelta(hours=1))
return
Expand All @@ -33,22 +32,20 @@ def get_temperature_from_api():
try:
response = requests.get(settings.DSMRREADER_BUIENRADAR_API_URL)
except Exception as error:
logger.exception(error)
raise AssertionError(_('Failed to connect to or read from Buienradar API'))
raise RuntimeError('Failed to read API: {}'.format(error))

if response.status_code != 200:
logger.error('Buienradar: Failed reading temperature: HTTP %s', response.status_code)
raise AssertionError(_('Unexpected status code received'))
raise RuntimeError('Unexpected status code received: HTTP {}'.format(response.status_code))

# Find our selected station.
station_id = WeatherSettings.get_solo().buienradar_station
station_data = [x for x in response.json()['actual']['stationmeasurements'] if x['stationid'] == station_id]

if not station_data:
raise AssertionError(_('Selected station info not found'))
raise RuntimeError('Selected station info not found: {}'.format(station_id))

temperature = station_data[0]['groundtemperature']
logger.debug('Buienradar: Read temperature: %s', temperature)
logger.debug('Buienradar: Storing temperature read: %s', temperature)

hour_mark = timezone.now().replace(minute=0, second=0, microsecond=0)
return TemperatureReading.objects.create(read_at=hour_mark, degrees_celcius=Decimal(temperature))
6 changes: 3 additions & 3 deletions dsmr_weather/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_fail_http_connection(self, now_mock, requests_mock):
now_mock.return_value = timezone.make_aware(timezone.datetime(2017, 1, 1))
requests_mock.side_effect = IOError('Failed to connect') # Any error is fine.

with self.assertRaises(AssertionError):
with self.assertRaises(RuntimeError):
dsmr_weather.services.get_temperature_from_api()

@mock.patch('requests.get')
Expand All @@ -73,7 +73,7 @@ def test_fail_http_response(self, now_mock, requests_mock):
type(response_mock).status_code = mock.PropertyMock(return_value=500)
requests_mock.return_value = response_mock

with self.assertRaises(AssertionError):
with self.assertRaises(RuntimeError):
dsmr_weather.services.get_temperature_from_api()

@mock.patch('requests.get')
Expand All @@ -92,5 +92,5 @@ def test_fail_station(self, now_mock, requests_mock):
type(response_mock).status_code = mock.PropertyMock(return_value=200)
requests_mock.return_value = response_mock

with self.assertRaises(AssertionError):
with self.assertRaises(RuntimeError):
dsmr_weather.services.get_temperature_from_api()
Binary file modified dsmrreader/locales/nl/LC_MESSAGES/django.mo
Binary file not shown.
12 changes: 6 additions & 6 deletions dsmrreader/locales/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2627,18 +2627,18 @@ msgstr "Het weerstation dat gebruikt wordt om buitentemperaturen te meten en op
msgid "Weather configuration"
msgstr "Weergegevensconfiguratie"

msgid "Failed to connect to or read from Buienradar API"
msgstr "Kan niet verbinden naar of lezen van de Buienradar API"

msgid "Selected station info not found"
msgstr "Geselecteerd weerstation niet gevonden"

msgid "Dutch"
msgstr "Nederlands"

msgid "English"
msgstr "Engels"

#~ msgid "Failed to connect to or read from Buienradar API"
#~ msgstr "Kan niet verbinden naar of lezen van de Buienradar API"

#~ msgid "Selected station info not found"
#~ msgstr "Geselecteerd weerstation niet gevonden"

#~ msgid "Tariffs / Energy supplier prices"
#~ msgstr "Tarieven / Energieleverancierprijzen"

Expand Down

0 comments on commit 654828a

Please sign in to comment.