Skip to content

Commit

Permalink
Forecasted condition is based on prevailing conditions.
Browse files Browse the repository at this point in the history
Prior to this the worst condition was the reported condition.
  • Loading branch information
FL550 committed May 23, 2021
1 parent 25f34cc commit 262c85c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
47 changes: 47 additions & 0 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,61 @@ def get_daily_condition(self, timestamp: datetime, shouldUpdate=True):
return None

def get_condition(self, weather_data):

if len(weather_data) == 0:
return None
priority = 99
condition_text = ""
sunny_counter = 1
cloudy_counter = 1
rainy_counter = 1
snowy_counter = 1
thunder_counter = 1
fog_counter = 1

for item in weather_data:
if item[WeatherDataType.CONDITION.value] != "-":
condition = self.weather_codes[item[WeatherDataType.CONDITION.value]]
if condition[0] == "sunny":
sunny_counter += 1
if condition[0] == "cloudy":
cloudy_counter += 1
if condition[0] == "fog":
fog_counter += 1
if condition[0] == "rainy":
rainy_counter += 1
if condition[0] == "snowy":
snowy_counter += 1
if condition[0] == "lightning-rainy":
thunder_counter += 1

if condition[1] < priority:
priority = condition[1]
condition_text = condition[0]

if cloudy_counter / sunny_counter > 0.7:
condition_text = "cloudy"
elif cloudy_counter / sunny_counter > 0.2:
condition_text = "partlycloudy"
else:
condition_text = "sunny"
if fog_counter / len(weather_data) > 0.5:
condition_text = "fog"

print(snowy_counter / len(weather_data))
if snowy_counter / len(weather_data) > 0.2:
condition_text = "snowy"

print(rainy_counter / len(weather_data))
if rainy_counter / len(weather_data) > 0.2:
if condition_text == "snowy":
condition_text = "snowy-rainy"
else:
condition_text = "rainy"

if thunder_counter > 1:
condition_text = "lightning-rainy"

return str(condition_text)

def get_timeframe_max(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_get_daily_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def test_49_max(self, _):
test_time = datetime(2020, 11, 6, 10, 0)
self.assertEqual(
self.dwd_weather.get_daily_condition(test_time),
"fog",
"sunny",
)

@patch("simple_dwd_weatherforecast.dwdforecast.Weather.update", return_value=None)
def test_75_max(self, _):
test_time = datetime(2020, 11, 7, 10, 0)
self.assertEqual(
self.dwd_weather.get_daily_condition(test_time),
"snowy",
"sunny",
)

@patch("simple_dwd_weatherforecast.dwdforecast.Weather.update", return_value=None)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_get_timeframe_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ def test_part_in_timerange(self, _):
test_time = datetime(2020, 11, 6, 1, 0)
self.assertEqual(
self.dwd_weather.get_timeframe_condition(test_time, 6),
"cloudy",
"snowy-rainy",
)

@patch("simple_dwd_weatherforecast.dwdforecast.Weather.update", return_value=None)
def test_49_max(self, _):
test_time = datetime(2020, 11, 6, 10, 0)
self.assertEqual(
self.dwd_weather.get_timeframe_condition(test_time, 3),
"sunny",
"snowy-rainy",
)

@patch("simple_dwd_weatherforecast.dwdforecast.Weather.update", return_value=None)
def test_75_max(self, _):
test_time = datetime(2020, 11, 7, 10, 0)
self.assertEqual(
self.dwd_weather.get_timeframe_condition(test_time, 3),
self.dwd_weather.get_timeframe_condition(test_time, 6),
"sunny",
)

Expand All @@ -56,5 +56,5 @@ def test_3_max(self, _):
test_time = datetime(2020, 11, 16, 9, 0)
self.assertEqual(
self.dwd_weather.get_timeframe_condition(test_time, 3),
"cloudy",
"snowy-rainy",
)

0 comments on commit 262c85c

Please sign in to comment.