Skip to content

Commit

Permalink
Use iso8601 format when fetching prayer times (home-assistant#104458)
Browse files Browse the repository at this point in the history
  • Loading branch information
engrbm87 authored Nov 25, 2023
1 parent 17cef89 commit dd02822
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
6 changes: 5 additions & 1 deletion homeassistant/components/islamic_prayer_times/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def get_new_prayer_times(self) -> dict[str, Any]:
midnightMode=self.midnight_mode,
school=self.school,
date=str(dt_util.now().date()),
iso8601=True,
)
return cast(dict[str, Any], calc.fetch_prayer_times())

Expand Down Expand Up @@ -145,9 +146,12 @@ async def _async_update_data(self) -> dict[str, datetime]:
async_call_later(self.hass, 60, self.async_request_update)
raise UpdateFailed from err

# introduced in prayer-times-calculator 0.0.8
prayer_times.pop("date", None)

prayer_times_info: dict[str, datetime] = {}
for prayer, time in prayer_times.items():
if prayer_time := dt_util.parse_datetime(f"{dt_util.now().date()} {time}"):
if prayer_time := dt_util.parse_datetime(time):
prayer_times_info[prayer] = dt_util.as_utc(prayer_time)

self.async_schedule_future_update(prayer_times_info["Midnight"])
Expand Down
48 changes: 14 additions & 34 deletions tests/components/islamic_prayer_times/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,23 @@
import homeassistant.util.dt as dt_util

PRAYER_TIMES = {
"Fajr": "06:10",
"Sunrise": "07:25",
"Dhuhr": "12:30",
"Asr": "15:32",
"Maghrib": "17:35",
"Isha": "18:53",
"Midnight": "00:45",
}

PRAYER_TIMES_TIMESTAMPS = {
"Fajr": datetime(2020, 1, 1, 6, 10, 0, tzinfo=dt_util.UTC),
"Sunrise": datetime(2020, 1, 1, 7, 25, 0, tzinfo=dt_util.UTC),
"Dhuhr": datetime(2020, 1, 1, 12, 30, 0, tzinfo=dt_util.UTC),
"Asr": datetime(2020, 1, 1, 15, 32, 0, tzinfo=dt_util.UTC),
"Maghrib": datetime(2020, 1, 1, 17, 35, 0, tzinfo=dt_util.UTC),
"Isha": datetime(2020, 1, 1, 18, 53, 0, tzinfo=dt_util.UTC),
"Midnight": datetime(2020, 1, 1, 00, 45, 0, tzinfo=dt_util.UTC),
"Fajr": "2020-01-01T06:10:00+00:00",
"Sunrise": "2020-01-01T07:25:00+00:00",
"Dhuhr": "2020-01-01T12:30:00+00:00",
"Asr": "2020-01-01T15:32:00+00:00",
"Maghrib": "2020-01-01T17:35:00+00:00",
"Isha": "2020-01-01T18:53:00+00:00",
"Midnight": "2020-01-01T00:45:00+00:00",
}

NEW_PRAYER_TIMES = {
"Fajr": "06:00",
"Sunrise": "07:25",
"Dhuhr": "12:30",
"Asr": "15:32",
"Maghrib": "17:45",
"Isha": "18:53",
"Midnight": "00:43",
}

NEW_PRAYER_TIMES_TIMESTAMPS = {
"Fajr": datetime(2020, 1, 2, 6, 00, 0, tzinfo=dt_util.UTC),
"Sunrise": datetime(2020, 1, 2, 7, 25, 0, tzinfo=dt_util.UTC),
"Dhuhr": datetime(2020, 1, 2, 12, 30, 0, tzinfo=dt_util.UTC),
"Asr": datetime(2020, 1, 2, 15, 32, 0, tzinfo=dt_util.UTC),
"Maghrib": datetime(2020, 1, 2, 17, 45, 0, tzinfo=dt_util.UTC),
"Isha": datetime(2020, 1, 2, 18, 53, 0, tzinfo=dt_util.UTC),
"Midnight": datetime(2020, 1, 2, 00, 43, 0, tzinfo=dt_util.UTC),
"Fajr": "2020-01-02T06:00:00+00:00",
"Sunrise": "2020-01-02T07:25:00+00:00",
"Dhuhr": "2020-01-02T12:30:00+00:00",
"Asr": "2020-01-02T15:32:00+00:00",
"Maghrib": "2020-01-02T17:45:00+00:00",
"Isha": "2020-01-02T18:53:00+00:00",
"Midnight": "2020-01-02T00:43:00+00:00",
}

NOW = datetime(2020, 1, 1, 00, 00, 0, tzinfo=dt_util.UTC)
9 changes: 6 additions & 3 deletions tests/components/islamic_prayer_times/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util

from . import NEW_PRAYER_TIMES, NOW, PRAYER_TIMES, PRAYER_TIMES_TIMESTAMPS
from . import NEW_PRAYER_TIMES, NOW, PRAYER_TIMES

from tests.common import MockConfigEntry, async_fire_time_changed

Expand Down Expand Up @@ -90,7 +91,7 @@ async def test_options_listener(hass: HomeAssistant) -> None:
with patch(
"prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times",
return_value=PRAYER_TIMES,
) as mock_fetch_prayer_times:
) as mock_fetch_prayer_times, freeze_time(NOW):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert mock_fetch_prayer_times.call_count == 1
Expand Down Expand Up @@ -123,7 +124,9 @@ async def test_update_failed(hass: HomeAssistant) -> None:
InvalidResponseError,
NEW_PRAYER_TIMES,
]
future = PRAYER_TIMES_TIMESTAMPS["Midnight"] + timedelta(days=1, minutes=1)
midnight_time = dt_util.parse_datetime(PRAYER_TIMES["Midnight"])
assert midnight_time
future = midnight_time + timedelta(days=1, minutes=1)
with freeze_time(future):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
Expand Down
8 changes: 2 additions & 6 deletions tests/components/islamic_prayer_times/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

from homeassistant.components.islamic_prayer_times.const import DOMAIN
from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util

from . import NOW, PRAYER_TIMES, PRAYER_TIMES_TIMESTAMPS
from . import NOW, PRAYER_TIMES

from tests.common import MockConfigEntry

Expand Down Expand Up @@ -44,7 +43,4 @@ async def test_islamic_prayer_times_sensors(
), freeze_time(NOW):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert (
hass.states.get(sensor_name).state
== PRAYER_TIMES_TIMESTAMPS[key].astimezone(dt_util.UTC).isoformat()
)
assert hass.states.get(sensor_name).state == PRAYER_TIMES[key]

0 comments on commit dd02822

Please sign in to comment.