Skip to content

Commit

Permalink
fix: Handle absent astronomy values
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Dec 26, 2022
1 parent ee6e1bb commit 8415208
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 93 deletions.
42 changes: 16 additions & 26 deletions custom_components/here_weather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,26 @@ def parse_time_as_utc(

def astronomy_data(data: dict[str, Any]) -> list[dict[str, str | datetime | None]]:
"""Restructure data for this integration."""
ammended_data: list[dict[str, str]] = copy.deepcopy(data["forecasts"])
ammended_data: list[dict[str, str | datetime | None]] = copy.deepcopy(
data["forecasts"]
)
for element in ammended_data:
element["city"] = data["place"]["address"]["city"]
element["latitude"] = data["place"]["location"]["lat"]
element["longitude"] = data["place"]["location"]["lng"]
return astronomy_data_with_utc(ammended_data)
element["sunRise"] = astronomy_data_with_utc("sunRise", element)
element["sunSet"] = astronomy_data_with_utc("sunSet", element)
element["moonRise"] = astronomy_data_with_utc("moonRise", element)
element["moonSet"] = astronomy_data_with_utc("moonSet", element)
return ammended_data


def astronomy_data_with_utc(
data: list[dict[str, str]]
) -> list[dict[str, str | datetime | None]]:
"""Amend astronomy data with utc fields."""
ammended_data: list[dict[str, str | datetime | None]] = []
for element in data:
ammended_element: dict[str, str | datetime | None] = {}
ammended_element["moonPhase"] = element["moonPhase"]
ammended_element["moonPhaseDescription"] = element["moonPhaseDescription"]
ammended_element["iconName"] = element["iconName"]
ammended_element["time"] = element["time"]
ammended_element["sunRise"] = combine_utc_and_local(
element["sunRise"], element["time"]
)
ammended_element["sunSet"] = combine_utc_and_local(
element["sunSet"], element["time"]
)
ammended_element["moonRise"] = combine_utc_and_local(
element["moonRise"], element["time"]
)
ammended_element["moonSet"] = combine_utc_and_local(
element["moonSet"], element["time"]
)
ammended_data.append(ammended_element)
return ammended_data
key: str, data: dict[str, str | datetime | None]
) -> str | datetime | None:
"""Transform astronomy data to utc fields."""
if (value := data.get(key)) is not None:
assert isinstance(value, str)
assert isinstance(data["time"], str)
return combine_utc_and_local(value, data["time"])
return None
127 changes: 63 additions & 64 deletions tests/fixtures/astronomy.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,83 @@
},
"forecasts": [
{
"sunRise": "07:13:00",
"sunSet": "16:21:00",
"moonRise": "01:37:00",
"moonSet": "13:06:00",
"moonPhase": "-0.338",
"moonPhaseDescription": "Waning crescent",
"iconName": "cw_waning_crescent",
"time": "2022-12-18T00:00:00.000-06:00"
"sunRise": "08:24:00",
"sunSet": "16:29:00",
"moonRise": "10:46:00",
"moonSet": "18:45:00",
"moonPhase": "0.039",
"moonPhaseDescription": "Waxing crescent",
"iconName": "cw_waxing_crescent",
"time": "2022-12-25T00:00:00.000+01:00"
},
{
"sunRise": "07:13:00",
"sunSet": "16:21:00",
"moonRise": "02:46:00",
"moonSet": "13:31:00",
"moonPhase": "-0.243",
"moonPhaseDescription": "Waning crescent",
"iconName": "cw_waning_crescent",
"time": "2022-12-19T00:00:00.000-06:00"
"sunRise": "08:25:00",
"sunSet": "16:30:00",
"moonRise": "11:19:00",
"moonSet": "20:15:00",
"moonPhase": "0.099",
"moonPhaseDescription": "Waxing crescent",
"iconName": "cw_waxing_crescent",
"time": "2022-12-26T00:00:00.000+01:00"
},
{
"sunRise": "07:14:00",
"sunSet": "16:22:00",
"moonRise": "04:00:00",
"moonSet": "14:01:00",
"moonPhase": "-0.155",
"moonPhaseDescription": "Waning crescent",
"iconName": "cw_waning_crescent",
"time": "2022-12-20T00:00:00.000-06:00"
"sunRise": "08:25:00",
"sunSet": "16:30:00",
"moonRise": "11:43:00",
"moonSet": "21:43:00",
"moonPhase": "0.181",
"moonPhaseDescription": "Waxing crescent",
"iconName": "cw_waxing_crescent",
"time": "2022-12-27T00:00:00.000+01:00"
},
{
"sunRise": "07:14:00",
"sunSet": "16:22:00",
"moonRise": "05:18:00",
"moonSet": "14:39:00",
"moonPhase": "-0.083",
"moonPhaseDescription": "Waning crescent",
"iconName": "cw_waning_crescent",
"time": "2022-12-21T00:00:00.000-06:00"
"sunRise": "08:25:00",
"sunSet": "16:31:00",
"moonRise": "12:02:00",
"moonSet": "23:06:00",
"moonPhase": "0.279",
"moonPhaseDescription": "Waxing crescent",
"iconName": "cw_waxing_crescent",
"time": "2022-12-28T00:00:00.000+01:00"
},
{
"sunRise": "07:15:00",
"sunSet": "16:23:00",
"moonRise": "06:37:00",
"moonSet": "15:29:00",
"moonPhase": "-0.030",
"moonPhaseDescription": "Waning crescent",
"iconName": "cw_waning_crescent",
"time": "2022-12-22T00:00:00.000-06:00"
"sunRise": "08:25:00",
"sunSet": "16:32:00",
"moonRise": "12:17:00",
"moonPhase": "0.385",
"moonPhaseDescription": "Waxing crescent",
"iconName": "cw_waxing_crescent",
"time": "2022-12-29T00:00:00.000+01:00"
},
{
"sunRise": "07:15:00",
"sunSet": "16:23:00",
"moonRise": "07:51:00",
"moonSet": "16:31:00",
"moonPhase": "-0.004",
"moonPhaseDescription": "New moon",
"iconName": "cw_new_moon",
"time": "2022-12-23T00:00:00.000-06:00"
"sunRise": "08:25:00",
"sunSet": "16:33:00",
"moonRise": "12:31:00",
"moonSet": "00:25:00",
"moonPhase": "0.494",
"moonPhaseDescription": "First Quarter",
"iconName": "cw_first_qtr",
"time": "2022-12-30T00:00:00.000+01:00"
},
{
"sunRise": "07:16:00",
"sunSet": "16:24:00",
"moonRise": "08:54:00",
"moonSet": "17:46:00",
"moonPhase": "0.006",
"moonPhaseDescription": "New moon",
"iconName": "cw_new_moon",
"time": "2022-12-24T00:00:00.000-06:00"
"sunRise": "08:25:00",
"sunSet": "16:34:00",
"moonRise": "12:46:00",
"moonSet": "01:41:00",
"moonPhase": "0.600",
"moonPhaseDescription": "Waxing gibbous",
"iconName": "cw_waxing_gibbous",
"time": "2022-12-31T00:00:00.000+01:00"
},
{
"sunRise": "07:16:00",
"sunSet": "16:25:00",
"moonRise": "09:45:00",
"moonSet": "19:06:00",
"moonPhase": "0.039",
"moonPhaseDescription": "Waxing crescent",
"iconName": "cw_waxing_crescent",
"time": "2022-12-25T00:00:00.000-06:00"
"sunRise": "08:26:00",
"sunSet": "16:35:00",
"moonRise": "13:02:00",
"moonSet": "02:56:00",
"moonPhase": "0.698",
"moonPhaseDescription": "Waxing gibbous",
"iconName": "cw_waxing_gibbous",
"time": "2023-01-01T00:00:00.000+01:00"
}
]
}
Expand Down
6 changes: 3 additions & 3 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ async def test_forecast_astronomy(hass):
await hass.async_block_till_done()

sunrise = hass.states.get("sensor.here_weather_forecast_astronomy_sunrise_0")
assert sunrise.state == "2022-12-18T13:13:00+00:00"
assert sunrise.state == "2022-12-25T07:24:00+00:00"
sunset = hass.states.get("sensor.here_weather_forecast_astronomy_sunset_0")
assert sunset.state == "2022-12-18T22:21:00+00:00"
assert sunset.state == "2022-12-25T15:29:00+00:00"
utc_time = hass.states.get("sensor.here_weather_forecast_astronomy_utc_time_0")
assert utc_time.state == "2022-12-18T06:00:00+00:00"
assert utc_time.state == "2022-12-24T23:00:00+00:00"

0 comments on commit 8415208

Please sign in to comment.