Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b41c566
Add Islamic Prayer Times config_flow
engrbm87 Nov 13, 2019
211b60a
Add Islamic Prayer Times config_flow
engrbm87 Nov 13, 2019
a98ee58
Merge branch 'islamic-prayer-config-flow' of https://github.com/engrb…
engrbm87 Feb 5, 2020
4fd2cea
handle options update and fix tests
engrbm87 Feb 7, 2020
87460d0
fix sensor update handling
engrbm87 Feb 7, 2020
8336df4
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
engrbm87 Feb 11, 2020
0e544e5
fix pylint
engrbm87 Feb 13, 2020
d412176
fix scheduled update and add test
engrbm87 Feb 15, 2020
8f9d01b
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
engrbm87 Mar 16, 2020
2a61a7d
update test_init
engrbm87 Mar 18, 2020
d5e6dfb
update flow options to show drop list
engrbm87 Mar 19, 2020
da7f34c
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
engrbm87 Apr 1, 2020
33e757e
Merge branch 'dev' into islamic-prayer-config-flow
engrbm87 Apr 10, 2020
25547a3
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
engrbm87 Apr 10, 2020
ee453f6
clean up code
engrbm87 Apr 13, 2020
3c75168
Merge branch 'islamic-prayer-config-flow' of https://github.com/engrb…
engrbm87 Apr 13, 2020
92ca24c
async scheduling and revert state to timestamp
engrbm87 Apr 13, 2020
d2b9782
fix update retry method
engrbm87 Apr 15, 2020
a1c57a9
Merge branch 'dev' of https://github.com/home-assistant/home-assistan…
engrbm87 Apr 20, 2020
d8d3df0
update strings
engrbm87 Apr 20, 2020
f3dc053
keep title as root key
engrbm87 Apr 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions homeassistant/components/islamic_prayer_times/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.event import track_point_in_time
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_point_in_time
import homeassistant.util.dt as dt_util

from .const import (
Expand Down Expand Up @@ -61,9 +61,9 @@ async def async_setup_entry(hass, config_entry):
async def async_unload_entry(hass, config_entry):
"""Unload Islamic Prayer entry from config_entry."""
if hass.data[DOMAIN].event_unsub:
await hass.async_add_executor_job(hass.data[DOMAIN].event_unsub)
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
hass.data[DOMAIN].event_unsub()
hass.data.pop(DOMAIN)
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")

return True

Expand Down Expand Up @@ -94,7 +94,7 @@ def get_new_prayer_times(self):
)
return calc.fetch_prayer_times()

def schedule_future_update(self):
async def async_schedule_future_update(self):
"""Schedule future update for sensors.

Midnight is a calculated time. The specifics of the calculation
Expand Down Expand Up @@ -142,29 +142,33 @@ def schedule_future_update(self):

_LOGGER.info("Next update scheduled for: %s", next_update_at)

self.event_unsub = track_point_in_time(self.hass, self.update, next_update_at)
self.event_unsub = async_track_point_in_time(
self.hass, self.async_update, next_update_at
)

def update(self, *_):
async def async_update(self, *_):
"""Update sensors with new prayer times."""
try:
prayer_times = self.get_new_prayer_times()
prayer_times = await self.hass.async_add_executor_job(
self.get_new_prayer_times
)
self.available = True
except (exceptions.InvalidResponseError, ConnError):
self.available = False
_LOGGER.debug("Error retrieving prayer times.")
track_point_in_time(
self.hass, self.update, dt_util.utcnow() + timedelta(minutes=1)
async_track_point_in_time(
self.hass, self.async_update, dt_util.utcnow() + timedelta(minutes=1)
)
return

for prayer, time in prayer_times.items():
self.prayer_times_info[prayer] = dt_util.parse_datetime(
f"{dt_util.now().date()} {time}"
)
self.schedule_future_update()
await self.async_schedule_future_update()

_LOGGER.debug("New prayer times retrieved. Updating sensors.")
dispatcher_send(self.hass, DATA_UPDATED)
async_dispatcher_send(self.hass, DATA_UPDATED)

async def async_setup(self):
"""Set up the Islamic prayer client."""
Expand All @@ -175,7 +179,7 @@ async def async_setup(self):
except (exceptions.InvalidResponseError, ConnError):
raise ConfigEntryNotReady

await self.hass.async_add_executor_job(self.update)
await self.async_update()
self.config_entry.add_update_listener(self.async_options_updated)

self.hass.async_create_task(
Expand All @@ -200,5 +204,5 @@ async def async_add_options(self):
async def async_options_updated(hass, entry):
"""Triggered by config entry options updates."""
if hass.data[DOMAIN].event_unsub:
await hass.async_add_executor_job(hass.data[DOMAIN].event_unsub)
await hass.async_add_executor_job(hass.data[DOMAIN].update)
hass.data[DOMAIN].event_unsub()
await hass.data[DOMAIN].async_update()
3 changes: 0 additions & 3 deletions homeassistant/components/islamic_prayer_times/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
NAME = "Islamic Prayer Times"
SENSOR_SUFFIX = "Prayer"
PRAYER_TIMES_ICON = "mdi:calendar-clock"
ATTR_TIMESTAMP = "timestamp"
TIME_STR_FORMAT = "%H:%M"

SENSOR_TYPES = ["Fajr", "Sunrise", "Dhuhr", "Asr", "Maghrib", "Isha", "Midnight"]

CONF_CALC_METHOD = "calc_method"
CONF_SENSORS = "sensors"

CALC_METHODS = ["isna", "karachi", "mwl", "makkah"]
DEFAULT_CALC_METHOD = "isna"
Expand Down
24 changes: 7 additions & 17 deletions homeassistant/components/islamic_prayer_times/sensor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
"""Platform to retrieve Islamic prayer times information for Home Assistant."""
import logging

from homeassistant.const import DEVICE_CLASS_TIMESTAMP
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity

from .const import (
ATTR_TIMESTAMP,
DATA_UPDATED,
DOMAIN,
PRAYER_TIMES_ICON,
SENSOR_SUFFIX,
SENSOR_TYPES,
TIME_STR_FORMAT,
)
from .const import DATA_UPDATED, DOMAIN, PRAYER_TIMES_ICON, SENSOR_SUFFIX, SENSOR_TYPES

_LOGGER = logging.getLogger(__name__)

Expand All @@ -36,7 +29,6 @@ def __init__(self, sensor_type, client):
"""Initialize the Islamic prayer time sensor."""
self.sensor_type = sensor_type
self.client = client
self._state = self.client.prayer_times_info.get(self.sensor_type)

@property
def name(self):
Expand All @@ -46,7 +38,7 @@ def name(self):
@property
def unique_id(self):
"""Return the unique id of the entity."""
return f"{DOMAIN}_{self.sensor_type}"
return self.sensor_type

@property
def icon(self):
Expand All @@ -56,19 +48,17 @@ def icon(self):
@property
def state(self):
"""Return the state of the sensor."""
return self._state.time().strftime(TIME_STR_FORMAT) if self._state else None
return self.client.prayer_times_info.get(self.sensor_type).isoformat()

@property
def should_poll(self):
"""Disable polling."""
return False

@property
def state_attributes(self):
"""Return datetime as attribute."""
if self._state:
return {ATTR_TIMESTAMP: self._state}
return None
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_TIMESTAMP

async def async_added_to_hass(self):
"""Handle entity which will be added."""
Expand Down
4 changes: 3 additions & 1 deletion tests/components/islamic_prayer_times/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ async def test_unload_entry(hass):
):
await hass.config_entries.async_setup(entry.entry_id)

assert await islamic_prayer_times.async_unload_entry(hass, entry)
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
assert islamic_prayer_times.DOMAIN not in hass.data


Expand Down
7 changes: 3 additions & 4 deletions tests/components/islamic_prayer_times/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ async def test_islamic_prayer_times_sensors(hass):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

for prayer, time in PRAYER_TIMES.items():
assert hass.states.get(f"sensor.{prayer}_prayer").state == time
for prayer in PRAYER_TIMES:
assert (
hass.states.get(f"sensor.{prayer}_prayer").attributes.get("timestamp")
== PRAYER_TIMES_TIMESTAMPS[prayer]
hass.states.get(f"sensor.{prayer}_prayer").state
== PRAYER_TIMES_TIMESTAMPS[prayer].isoformat()
)