Skip to content

Commit 2d7f949

Browse files
JohannJohann
authored andcommitted
feat: load live prayer times and feed nida sensors
1 parent be7fcef commit 2d7f949

1 file changed

Lines changed: 47 additions & 41 deletions

File tree

docs/demo.js

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ function buildConfig() {
4444
};
4545
}
4646

47+
async function fetchPrayerTimes({ city, country }) {
48+
const url =
49+
"https://api.aladhan.com/v1/timingsByCity?method=2" +
50+
`&city=${encodeURIComponent(city)}` +
51+
`&country=${encodeURIComponent(country)}`;
52+
53+
const res = await fetch(url, { cache: "no-store" });
54+
if (!res.ok) throw new Error(`Prayer API HTTP ${res.status}`);
55+
const json = await res.json();
56+
const t = json?.data?.timings || {};
57+
58+
// "HH:MM" strings
59+
return {
60+
fajr: (t.Fajr || "").slice(0, 5),
61+
dhuhr: (t.Dhuhr || "").slice(0, 5),
62+
asr: (t.Asr || "").slice(0, 5),
63+
maghrib: (t.Maghrib || "").slice(0, 5),
64+
isha: (t.Isha || "").slice(0, 5),
65+
};
66+
}
67+
68+
function makeStatesForNida(times) {
69+
const mk = (state, name) => ({ state, attributes: { friendly_name: name } });
70+
71+
return {
72+
"sensor.02_fajr_readable": mk(times.fajr, "Fajr"),
73+
"sensor.04_dhuhr_readable": mk(times.dhuhr, "Dhuhr"),
74+
"sensor.05_asr_readable": mk(times.asr, "Asr"),
75+
"sensor.07_maghrib_readable": mk(times.maghrib, "Maghrib"),
76+
"sensor.08_isha_readable": mk(times.isha, "Isha"),
77+
};
78+
}
79+
4780
/* ===============================
4881
LIVE PRAYER TIMES (AlAdhan)
4982
================================= */
@@ -90,40 +123,22 @@ function computeNextPrayer(times) {
90123
}
91124

92125
function makeStatesFromTimes(times) {
93-
const next = computeNextPrayer(times);
94-
95-
const labelMap = {
96-
fajr: "Fajr",
97-
sunrise: "Sunrise",
98-
dhuhr: "Dhuhr",
99-
asr: "Asr",
100-
maghrib: "Maghrib",
101-
isha: "Isha",
102-
};
103-
104-
return {
105-
// all times
106-
"sensor.nida_prayer_times": {
107-
state: "ok",
126+
// times: { fajr, dhuhr, asr, maghrib, isha } -> "HH:MM"
127+
function stateObj(state, friendly) {
128+
return {
129+
state,
108130
attributes: {
109-
fajr: times.fajr,
110-
sunrise: times.sunrise,
111-
dhuhr: times.dhuhr,
112-
asr: times.asr,
113-
maghrib: times.maghrib,
114-
isha: times.isha,
131+
friendly_name: friendly,
115132
},
116-
},
133+
};
134+
}
117135

118-
// next prayer label + time
119-
"sensor.nida_next_prayer": {
120-
state: labelMap[next.name] || next.name,
121-
attributes: { friendly_name: "Next prayer" },
122-
},
123-
"sensor.nida_next_prayer_time": {
124-
state: next.at,
125-
attributes: { friendly_name: "Next prayer time" },
126-
},
136+
return {
137+
"sensor.02_fajr_readable": stateObj(times.fajr, "Fajr"),
138+
"sensor.04_dhuhr_readable": stateObj(times.dhuhr, "Dhuhr"),
139+
"sensor.05_asr_readable": stateObj(times.asr, "Asr"),
140+
"sensor.07_maghrib_readable": stateObj(times.maghrib, "Maghrib"),
141+
"sensor.08_isha_readable": stateObj(times.isha, "Isha"),
127142
};
128143
}
129144

@@ -143,21 +158,12 @@ async function mountCard() {
143158
const hass = makeFakeHass();
144159
el.hass = hass;
145160

146-
// fetch live times and inject as HA-like sensors
147161
try {
148162
const times = await fetchPrayerTimes({ city: cfg.city, country: cfg.country });
149-
hass.states = { ...hass.states, ...makeStatesFromTimes(times) };
163+
hass.states = { ...hass.states, ...makeStatesForNida(times) };
150164
el.hass = { ...hass }; // trigger update
151165
} catch (e) {
152166
console.error(e);
153-
hass.states = {
154-
...hass.states,
155-
"sensor.nida_prayer_times": {
156-
state: "error",
157-
attributes: { message: String(e) },
158-
},
159-
};
160-
el.hass = { ...hass };
161167
}
162168

163169
if (!el._config) el._config = cfg;

0 commit comments

Comments
 (0)