@@ -45,6 +45,87 @@ function buildConfig() {
4545 } ;
4646}
4747
48+ async function fetchPrayerTimes ( { city, country } ) {
49+ const url =
50+ "https://api.aladhan.com/v1/timingsByCity?method=2" +
51+ `&city=${ encodeURIComponent ( city ) } ` +
52+ `&country=${ encodeURIComponent ( country ) } ` ;
53+
54+ const res = await fetch ( url , { cache : "no-store" } ) ;
55+ if ( ! res . ok ) throw new Error ( `Prayer API HTTP ${ res . status } ` ) ;
56+ const json = await res . json ( ) ;
57+
58+ const t = json ?. data ?. timings || { } ;
59+ // Normaliseer keys die veel kaarten verwachten
60+ return {
61+ fajr : t . Fajr ,
62+ sunrise : t . Sunrise ,
63+ dhuhr : t . Dhuhr ,
64+ asr : t . Asr ,
65+ maghrib : t . Maghrib ,
66+ isha : t . Isha ,
67+ } ;
68+ }
69+
70+ function computeNextPrayer ( times ) {
71+ const order = [ "fajr" , "sunrise" , "dhuhr" , "asr" , "maghrib" , "isha" ] ;
72+ const now = new Date ( ) ;
73+ const today = now . toISOString ( ) . slice ( 0 , 10 ) ;
74+
75+ function toDate ( hhmm ) {
76+ // hh:mm (soms "05:12 (CET)" -> pak eerste 5)
77+ const v = ( hhmm || "" ) . slice ( 0 , 5 ) ;
78+ const [ h , m ] = v . split ( ":" ) . map ( ( x ) => parseInt ( x , 10 ) ) ;
79+ const d = new Date ( `${ today } T00:00:00` ) ;
80+ d . setHours ( h || 0 , m || 0 , 0 , 0 ) ;
81+ return d ;
82+ }
83+
84+ for ( const key of order ) {
85+ const d = toDate ( times [ key ] ) ;
86+ if ( d > now ) return { name : key , at : times [ key ] } ;
87+ }
88+ // anders: volgende dag fajr
89+ return { name : "fajr" , at : times . fajr } ;
90+ }
91+
92+ function makeStatesFromTimes ( times ) {
93+ const next = computeNextPrayer ( times ) ;
94+ const labelMap = {
95+ fajr : "Fajr" ,
96+ sunrise : "Sunrise" ,
97+ dhuhr : "Dhuhr" ,
98+ asr : "Asr" ,
99+ maghrib : "Maghrib" ,
100+ isha : "Isha" ,
101+ } ;
102+
103+ return {
104+ // algemene “container” sensor met alle tijden
105+ "sensor.nida_prayer_times" : {
106+ state : "ok" ,
107+ attributes : {
108+ fajr : times . fajr ,
109+ sunrise : times . sunrise ,
110+ dhuhr : times . dhuhr ,
111+ asr : times . asr ,
112+ maghrib : times . maghrib ,
113+ isha : times . isha ,
114+ } ,
115+ } ,
116+
117+ // next prayer
118+ "sensor.nida_next_prayer" : {
119+ state : labelMap [ next . name ] || next . name ,
120+ attributes : { friendly_name : "Next prayer" } ,
121+ } ,
122+ "sensor.nida_next_prayer_time" : {
123+ state : next . at ,
124+ attributes : { friendly_name : "Next prayer time" } ,
125+ } ,
126+ } ;
127+ }
128+
48129function mountCard ( ) {
49130 mount . innerHTML = "" ;
50131
0 commit comments