@@ -63,9 +63,6 @@ function friendlyName(light: LightState): string {
6363 return light . attributes . friendly_name || light . entity_id ;
6464}
6565
66- function wait ( ms : number ) : Promise < void > {
67- return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
68- }
6966
7067function getLightAccessories ( light : LightState ) : { text : string } [ ] {
7168 const accessories : { text : string } [ ] = [ { text : light . state } ] ;
@@ -148,6 +145,8 @@ function HomeAssistantLightsContent({ fallbackText }: { fallbackText?: string })
148145 queryFn : ( ) => fetchLights ( preferences ) ,
149146 enabled : hasPreferences ,
150147 placeholderData : keepPreviousData ,
148+ refetchInterval : 1000 ,
149+ refetchIntervalInBackground : true ,
151150 } ) ;
152151
153152 const { data : favoriteLights = [ ] } = useQuery ( {
@@ -220,29 +219,34 @@ function HomeAssistantLightsContent({ fallbackText }: { fallbackText?: string })
220219 label : string ,
221220 ) : Promise < void > {
222221 try {
223- const expectedState =
224- service === "turn_on"
225- ? "on"
226- : service === "turn_off"
227- ? "off"
228- : light . state === "on"
229- ? "off"
230- : "on" ;
231222 await callLightService ( service , light . entity_id , preferences ) ;
223+ queryClient . setQueryData < LightState [ ] > (
224+ [ "home-assistant" , "lights" ] ,
225+ ( existing ) => {
226+ if ( ! existing ) return existing ;
227+ return existing . map ( ( item ) => {
228+ if ( item . entity_id !== light . entity_id ) return item ;
229+ const nextState =
230+ service === "turn_on"
231+ ? "on"
232+ : service === "turn_off"
233+ ? "off"
234+ : item . state === "on"
235+ ? "off"
236+ : "on" ;
237+ return {
238+ ...item ,
239+ state : nextState ,
240+ last_updated : new Date ( ) . toISOString ( ) ,
241+ } ;
242+ } ) ;
243+ } ,
244+ ) ;
232245 await showToast ( {
233246 style : Toast . Style . Success ,
234247 title : label ,
235248 message : friendlyName ( light ) ,
236249 } ) ;
237- for ( const delay of [ 0 , 200 , 500 , 900 ] ) {
238- if ( delay > 0 ) await wait ( delay ) ;
239- const updated = await fetchLights ( preferences ) ;
240- queryClient . setQueryData ( [ "home-assistant" , "lights" ] , updated ) ;
241- const matched = updated . find (
242- ( item ) => item . entity_id === light . entity_id ,
243- ) ;
244- if ( matched ?. state === expectedState ) break ;
245- }
246250 } catch ( requestError ) {
247251 await showToast ( {
248252 style : Toast . Style . Failure ,
0 commit comments