@@ -112,14 +112,16 @@ def get_optional_numeric_config(config_name: str) -> int | float | None:
112112 async def _async_update (self ) -> None :
113113 """Virtual function to be overwritten."""
114114
115- async def async_update (self ) -> None :
115+ async def async_update (self , now : datetime | None = None ) -> None :
116116 """Update the entity state."""
117- if self ._cancel_call :
118- self ._cancel_call ()
119- await self .async_local_update ()
117+ await self .async_local_update (cancel_pending_update = True )
120118
121- async def async_local_update (self , now : datetime | None = None ) -> None :
119+ async def async_local_update (
120+ self , now : datetime | None = None , cancel_pending_update : bool = False
121+ ) -> None :
122122 """Update the entity state."""
123+ if cancel_pending_update and self ._cancel_call :
124+ self ._cancel_call ()
123125 await self ._async_update ()
124126 self .async_write_ha_state ()
125127 if self ._scan_interval > 0 :
@@ -131,62 +133,22 @@ async def async_local_update(self, now: datetime | None = None) -> None:
131133
132134 async def async_will_remove_from_hass (self ) -> None :
133135 """Remove entity from hass."""
134- _LOGGER .debug (f"Removing entity { self ._attr_name } " )
135- if self ._cancel_call :
136- self ._cancel_call ()
137- self ._cancel_call = None
136+ self .async_disable ()
138137
139138 @callback
140- def async_hold (self ) -> None :
139+ def async_disable (self ) -> None :
141140 """Remote stop entity."""
142- _LOGGER .debug (f"hold entity { self ._attr_name } " )
143- self ._async_cancel_future_pending_update ()
144- self ._attr_available = False
145- self .async_write_ha_state ()
146-
147- async def _async_update_write_state (self ) -> None :
148- """Update the entity state and write it to the state machine."""
141+ _LOGGER .info (f"hold entity { self ._attr_name } " )
149142 if self ._cancel_call :
150143 self ._cancel_call ()
151144 self ._cancel_call = None
152- await self .async_local_update ()
153-
154- async def _async_update_if_not_in_progress (
155- self , now : datetime | None = None
156- ) -> None :
157- """Update the entity state if not already in progress."""
158- await self ._async_update_write_state ()
159-
160- @callback
161- def async_run (self ) -> None :
162- """Remote start entity."""
163- _LOGGER .info (f"start entity { self ._attr_name } " )
164- self ._async_schedule_future_update (0.1 )
165- self ._cancel_call = async_call_later (
166- self .hass , timedelta (seconds = 0.1 ), self .async_local_update
167- )
168- self ._attr_available = True
145+ self ._attr_available = False
169146 self .async_write_ha_state ()
170147
171- @callback
172- def _async_schedule_future_update (self , delay : float ) -> None :
173- """Schedule an update in the future."""
174- self ._async_cancel_future_pending_update ()
175- self ._cancel_call = async_call_later (
176- self .hass , delay , self ._async_update_if_not_in_progress
177- )
178-
179- @callback
180- def _async_cancel_future_pending_update (self ) -> None :
181- """Cancel a future pending update."""
182- if self ._cancel_call :
183- self ._cancel_call ()
184- self ._cancel_call = None
185-
186148 async def async_await_connection (self , _now : Any ) -> None :
187149 """Wait for first connect."""
188150 await self ._hub .event_connected .wait ()
189- self .async_run ( )
151+ await self .async_local_update ( cancel_pending_update = True )
190152
191153 async def async_base_added_to_hass (self ) -> None :
192154 """Handle entity which will be added."""
@@ -198,10 +160,12 @@ async def async_base_added_to_hass(self) -> None:
198160 )
199161 )
200162 self .async_on_remove (
201- async_dispatcher_connect (self .hass , SIGNAL_STOP_ENTITY , self .async_hold )
163+ async_dispatcher_connect (self .hass , SIGNAL_STOP_ENTITY , self .async_disable )
202164 )
203165 self .async_on_remove (
204- async_dispatcher_connect (self .hass , SIGNAL_START_ENTITY , self .async_run )
166+ async_dispatcher_connect (
167+ self .hass , SIGNAL_START_ENTITY , self .async_local_update
168+ )
205169 )
206170
207171
@@ -388,10 +352,15 @@ async def async_turn(self, command: int) -> None:
388352 return
389353
390354 if self ._verify_delay :
391- self ._async_schedule_future_update (self ._verify_delay )
355+ assert self ._verify_delay == 1
356+ if self ._cancel_call :
357+ self ._cancel_call ()
358+ self ._cancel_call = None
359+ self ._cancel_call = async_call_later (
360+ self .hass , self ._verify_delay , self .async_update
361+ )
392362 return
393-
394- await self ._async_update_write_state ()
363+ await self .async_local_update (cancel_pending_update = True )
395364
396365 async def async_turn_off (self , ** kwargs : Any ) -> None :
397366 """Set switch off."""
0 commit comments