Skip to content
Snippets Groups Projects
Commit f37c967d authored by Robert Goldmann's avatar Robert Goldmann
Browse files

Fixed #31 - weather icons and color not correct

parent b9b5944f
No related branches found
No related tags found
No related merge requests found
from datetime import datetime from datetime import datetime
import pytz
from logic import Constants from logic import Constants
...@@ -50,12 +52,12 @@ def determine_color_for_weather_icon(iconId: int, isDayTime: bool): ...@@ -50,12 +52,12 @@ def determine_color_for_weather_icon(iconId: int, isDayTime: bool):
return Constants.WHITE.to_rgba() return Constants.WHITE.to_rgba()
def is_dayTime(sunrise: int, sunset: int, currentTimestamp: int = None) -> bool: def is_dayTime(sunrise: datetime, sunset: datetime, currentTimestamp: datetime) -> bool:
if not currentTimestamp: if not currentTimestamp:
currentTimestamp = int(datetime.now().timestamp()) currentTimestamp = datetime.now()
return sunrise < currentTimestamp < sunset return sunrise < currentTimestamp < sunset
def timestamp_to_timezone(timestamp: int, timeZone: datetime.tzinfo): def timestamp_to_timezone(timestamp: int, timeZone: datetime.tzinfo) -> datetime:
timestamp = datetime.utcfromtimestamp(timestamp) timestamp = datetime.fromtimestamp(timestamp, pytz.timezone('UTC'))
return timeZone.fromutc(timestamp) return timestamp.astimezone(timeZone)
...@@ -35,9 +35,9 @@ class CurrentWeatherTile(Tile): ...@@ -35,9 +35,9 @@ class CurrentWeatherTile(Tile):
feelsLike = currentWeather['feels_like'] feelsLike = currentWeather['feels_like']
windSpeed = currentWeather['wind_speed'] * 3.6 windSpeed = currentWeather['wind_speed'] * 3.6
icon = currentWeather['weather'][0]['id'] icon = currentWeather['weather'][0]['id']
sunrise = Helpers.timestamp_to_timezone(currentWeather['sunrise'], timeZone).timestamp() sunrise = Helpers.timestamp_to_timezone(currentWeather['sunrise'], timeZone)
sunset = Helpers.timestamp_to_timezone(currentWeather['sunset'], timeZone).timestamp() sunset = Helpers.timestamp_to_timezone(currentWeather['sunset'], timeZone)
isDayTime = Helpers.is_dayTime(sunrise, sunset) isDayTime = Helpers.is_dayTime(sunrise, sunset, datetime.now(tz=timeZone))
return { return {
'temperature': Helpers.round_to_decimals(currentTemperature, 1), 'temperature': Helpers.round_to_decimals(currentTemperature, 1),
......
...@@ -31,8 +31,8 @@ class HourlyForecastTile(Tile): ...@@ -31,8 +31,8 @@ class HourlyForecastTile(Tile):
# cache key will be determined in service # cache key will be determined in service
weatherData = weatherService.get_data('', fetchIntervalInSeconds, self._settings) weatherData = weatherService.get_data('', fetchIntervalInSeconds, self._settings)
sunrise = Helpers.timestamp_to_timezone(weatherData['current']['sunrise'], timeZone).timestamp() sunrise = Helpers.timestamp_to_timezone(weatherData['current']['sunrise'], timeZone)
sunset = Helpers.timestamp_to_timezone(weatherData['current']['sunset'], timeZone).timestamp() sunset = Helpers.timestamp_to_timezone(weatherData['current']['sunset'], timeZone)
hourData = [] hourData = []
hourlyForecast = weatherData['hourly'] hourlyForecast = weatherData['hourly']
...@@ -44,7 +44,7 @@ class HourlyForecastTile(Tile): ...@@ -44,7 +44,7 @@ class HourlyForecastTile(Tile):
rainProbability = round(entry['pop'] * 100, -1) # -1 rounds to the next ten rainProbability = round(entry['pop'] * 100, -1) # -1 rounds to the next ten
windSpeed = entry['wind_speed'] * 3.6 windSpeed = entry['wind_speed'] * 3.6
isDayTime = Helpers.is_dayTime(sunrise, sunset, currentTimestamp=timestamp.timestamp()) isDayTime = Helpers.is_dayTime(sunrise, sunset, currentTimestamp=timestamp)
hourData.append({ hourData.append({
'hour': timestamp.strftime('%H'), 'hour': timestamp.strftime('%H'),
......
import unittest import unittest
from datetime import datetime from datetime import datetime
import pytz
from logic import Helpers from logic import Helpers
...@@ -15,25 +17,56 @@ class HelpersTest(unittest.TestCase): ...@@ -15,25 +17,56 @@ class HelpersTest(unittest.TestCase):
self.assertEqual('0', Helpers.round_to_decimals(0.428, 0)) self.assertEqual('0', Helpers.round_to_decimals(0.428, 0))
def test_is_dayTime_true(self): def test_is_dayTime_true(self):
sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0).timestamp() sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0)
sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0).timestamp() sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0)
currentTimestamp = datetime(year=2020, month=11, day=1, hour=12, minute=0, second=0).timestamp() currentTimestamp = datetime(year=2020, month=11, day=1, hour=12, minute=0, second=0)
self.assertTrue(Helpers.is_dayTime(sunrise, sunset, currentTimestamp)) self.assertTrue(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
def test_is_dayTime_false_before(self): def test_is_dayTime_false_before(self):
sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0).timestamp() sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0)
sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0).timestamp() sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0)
currentTimestamp = datetime(year=2020, month=11, day=1, hour=4, minute=0, second=0).timestamp() currentTimestamp = datetime(year=2020, month=11, day=1, hour=4, minute=0, second=0)
self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp)) self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
def test_is_dayTime_false_after(self): def test_is_dayTime_false_after(self):
sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0).timestamp() sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0)
sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0).timestamp() sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0)
currentTimestamp = datetime(year=2020, month=11, day=1, hour=18, minute=0, second=0).timestamp() currentTimestamp = datetime(year=2020, month=11, day=1, hour=18, minute=0, second=0)
self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp)) self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
def test_is_dayTime_true_complex(self):
timeZone = pytz.timezone('Europe/Berlin')
sunrise = Helpers.timestamp_to_timezone(1604296782, timeZone)
sunset = Helpers.timestamp_to_timezone(1604331478, timeZone)
now = timeZone.localize(datetime(year=2020, month=11, day=2, hour=15, minute=0, second=0))
self.assertTrue(Helpers.is_dayTime(sunrise, sunset, now))
def test_timestamp_to_timezone_berlin(self):
timestamp = 1604331478
timeZone = pytz.timezone('Europe/Berlin')
expected = datetime(year=2020, month=11, day=2, hour=16, minute=37, second=58)
self.__compareDates(expected, Helpers.timestamp_to_timezone(timestamp, timeZone))
def test_timestamp_to_timezone_london(self):
timestamp = 1604331478
timeZone = pytz.timezone('Europe/London')
expected = datetime(year=2020, month=11, day=2, hour=15, minute=37, second=58, tzinfo=timeZone)
self.__compareDates(expected, Helpers.timestamp_to_timezone(timestamp, timeZone))
def __compareDates(self, a: datetime, b: datetime):
self.assertEqual(a.year, b.year)
self.assertEqual(a.month, b.month)
self.assertEqual(a.day, b.day)
self.assertEqual(a.hour, b.hour)
self.assertEqual(a.minute, b.minute)
self.assertEqual(a.second, b.second)
self.assertEqual(a.microsecond, b.microsecond)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment