1
1
"""Platform to retrieve Mawaqit prayer times information for Home Assistant."""
2
2
3
- from homeassistant .components .sensor import SensorEntity
4
- from homeassistant .components .sensor import SensorDeviceClass
3
+ import logging
4
+ from typing import Any
5
+
6
+ from homeassistant .components .sensor import SensorDeviceClass , SensorEntity
7
+ from homeassistant .config_entries import ConfigEntry
8
+ from homeassistant .core import HomeAssistant
5
9
from homeassistant .helpers .dispatcher import async_dispatcher_connect
10
+ from homeassistant .helpers .entity_platform import AddEntitiesCallback
11
+ from homeassistant .helpers .storage import Store
6
12
import homeassistant .util .dt as dt_util
7
13
8
- from .const import DATA_UPDATED , DOMAIN , PRAYER_TIMES_ICON , SENSOR_TYPES
9
-
10
- import json
11
- import os
12
- import logging
13
-
14
+ from . import utils
15
+ from .const import (
16
+ DATA_UPDATED ,
17
+ DOMAIN ,
18
+ MAWAQIT_STORAGE_KEY ,
19
+ MAWAQIT_STORAGE_VERSION ,
20
+ PRAYER_TIMES_ICON ,
21
+ SENSOR_TYPES ,
22
+ )
14
23
15
24
_LOGGER = logging .getLogger (__name__ )
16
25
17
26
18
- async def async_setup_entry (hass , config_entry , async_add_entities ):
27
+ async def async_setup_entry (
28
+ hass : HomeAssistant ,
29
+ config_entry : ConfigEntry ,
30
+ async_add_entities : AddEntitiesCallback ,
31
+ ) -> None :
19
32
"""Set up the Mawaqit prayer times sensor platform."""
20
33
21
34
client = hass .data [DOMAIN ]
22
35
if not client :
23
- _LOGGER .error ("Error retrieving client object. " )
36
+ _LOGGER .error ("Error retrieving client object" )
24
37
25
38
entities = []
26
39
for sensor_type in SENSOR_TYPES :
@@ -32,7 +45,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
32
45
"Maghrib" ,
33
46
"Isha" ,
34
47
"Jumua" ,
35
- "Jumua 2" , # "Aid", "Aid 2",
48
+ "Jumua 2" , # "Aid" and "Aid 2",
36
49
"Fajr Iqama" ,
37
50
"Shurouq Iqama" ,
38
51
"Dhuhr Iqama" ,
@@ -55,38 +68,29 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
55
68
class MawaqitPrayerTimeSensor (SensorEntity ):
56
69
"""Representation of an Mawaqit prayer time sensor."""
57
70
58
- def __init__ (self , sensor_type , client ):
71
+ def __init__ (self , sensor_type , client ) -> None :
59
72
"""Initialize the Mawaqit prayer time sensor."""
60
73
self .sensor_type = sensor_type
61
74
self .client = client
62
75
63
76
@property
64
- def name (self ):
77
+ def name (self ) -> str :
65
78
"""Return the name of the sensor."""
66
79
return f"{ self .sensor_type } { SENSOR_TYPES [self .sensor_type ]} "
67
80
68
81
@property
69
- def unique_id (self ):
82
+ def unique_id (self ) -> str :
70
83
"""Return the unique id of the entity."""
71
84
return self .sensor_type
72
85
73
86
@property
74
- def icon (self ):
87
+ def icon (self ) -> str :
75
88
"""Icon to display in the front end."""
76
89
return PRAYER_TIMES_ICON
77
90
78
- # @property
79
- # def state(self):
80
- # """Return the state of the sensor."""
81
- # return (
82
- # self.client.prayer_times_info.get(self.sensor_type)
83
- # .astimezone(dt_util.UTC)
84
- # .isoformat()
85
- # )
86
-
87
91
@property
88
92
def native_value (self ):
89
- """Return the state of the sensor. .astimezone(dt_util.UTC)"""
93
+ """Return the state of the sensor. .astimezone(dt_util.UTC). """
90
94
if self .sensor_type in [
91
95
"Fajr" ,
92
96
"Shurouq" ,
@@ -106,21 +110,24 @@ def native_value(self):
106
110
"Next Salat Preparation" ,
107
111
]:
108
112
time = self .client .prayer_times_info .get (self .sensor_type )
113
+ _LOGGER .debug ("[;] before %s Time: %s" , self .sensor_type , time )
109
114
if time is not None :
115
+ _LOGGER .debug (
116
+ "[;] %s Time: %s" , self .sensor_type , time .astimezone (dt_util .UTC )
117
+ )
110
118
return time .astimezone (dt_util .UTC )
111
- else :
112
- return None
113
119
114
- else :
115
- return self .client .prayer_times_info .get (self .sensor_type )
120
+ return None
121
+
122
+ return self .client .prayer_times_info .get (self .sensor_type )
116
123
117
124
@property
118
- def should_poll (self ):
125
+ def should_poll (self ) -> bool :
119
126
"""Disable polling."""
120
127
return False
121
128
122
129
@property
123
- def device_class (self ):
130
+ def device_class (self ) -> SensorDeviceClass | None :
124
131
"""Return the device class."""
125
132
if self .sensor_type in [
126
133
"Fajr" ,
@@ -141,10 +148,9 @@ def device_class(self):
141
148
"Next Salat Preparation" ,
142
149
]:
143
150
return SensorDeviceClass .TIMESTAMP
144
- else :
145
- return None
151
+ return None
146
152
147
- async def async_added_to_hass (self ):
153
+ async def async_added_to_hass (self ) -> None :
148
154
"""Handle entity which will be added."""
149
155
self .async_on_remove (
150
156
async_dispatcher_connect (self .hass , DATA_UPDATED , self .async_write_ha_state )
@@ -154,49 +160,44 @@ async def async_added_to_hass(self):
154
160
class MyMosqueSensor (SensorEntity ):
155
161
"""Representation of a mosque sensor."""
156
162
157
- def __init__ (self , name , hass ) :
163
+ def __init__ (self , name , hass : HomeAssistant ) -> None :
158
164
"""Initialize the mosque sensor."""
159
165
self .hass = hass
160
- self ._attributes = {}
166
+ self ._attributes : dict [ str , Any ] = {}
161
167
self ._name = name
162
168
self ._state = None
163
169
latitude = self .hass .config .latitude
164
170
longitude = self .hass .config .longitude
165
171
self ._latitude = latitude
166
172
self ._longitude = longitude
173
+ self .store : Store = Store (
174
+ self .hass , MAWAQIT_STORAGE_VERSION , MAWAQIT_STORAGE_KEY
175
+ )
167
176
168
- # @Throttle(TIME_BETWEEN_UPDATES)
169
- async def async_update (self ):
177
+ async def async_update (self ) -> None :
170
178
"""Get the latest data from the Mawaqit API."""
171
- current_dir = os .path .dirname (os .path .realpath (__file__ ))
172
-
173
- def read ():
174
- with open ("{}/data/my_mosque_NN.txt" .format (current_dir ), "r" ) as f :
175
- data = json .load (f )
176
- return data
177
-
178
- data = await self .hass .async_add_executor_job (read )
179
+ data_my_mosque_NN = await utils .read_my_mosque_NN_file (self .store )
179
180
180
- for k , v in data .items ():
181
+ for k , v in data_my_mosque_NN .items ():
181
182
if str (k ) != "uuid" and str (k ) != "id" and str (k ) != "slug" :
182
183
self ._attributes [k ] = str (v )
183
184
184
185
@property
185
- def name (self ):
186
+ def name (self ) -> str | None :
186
187
"""Return the name of the sensor."""
187
188
return self ._name
188
189
189
190
@property
190
- def state (self ):
191
+ def native_value (self ) -> str | None :
191
192
"""Return the state of the sensor."""
192
193
return self ._attributes ["name" ]
193
194
194
195
@property
195
- def icon (self ):
196
+ def icon (self ) -> str | None :
196
197
"""Return the icon of the sensor."""
197
198
return "mdi:mosque"
198
199
199
200
@property
200
- def extra_state_attributes (self ):
201
+ def extra_state_attributes (self ) -> dict [ str , Any ] | None :
201
202
"""Return attributes for the sensor."""
202
203
return self ._attributes
0 commit comments