modbus: add async_will_remove_from_hass() to do cleanup. (#150906)

This commit is contained in:
jan iversen
2025-08-26 23:50:11 +02:00
committed by GitHub
parent d278d21561
commit 0139407f52
2 changed files with 17 additions and 16 deletions

View File

@@ -89,7 +89,6 @@ class BasePlatform(Entity):
self._address = int(entry[CONF_ADDRESS])
self._input_type = entry[CONF_INPUT_TYPE]
self._scan_interval = int(entry[CONF_SCAN_INTERVAL])
self._cancel_timer: Callable[[], None] | None = None
self._cancel_call: Callable[[], None] | None = None
self._attr_unique_id = entry.get(CONF_UNIQUE_ID)
self._attr_name = entry[CONF_NAME]
@@ -129,6 +128,21 @@ class BasePlatform(Entity):
self.async_local_update,
)
async def async_will_remove_from_hass(self) -> None:
"""Remove entity from hass."""
_LOGGER.debug(f"Removing entity {self._attr_name}")
if self._cancel_call:
self._cancel_call()
self._cancel_call = None
@callback
def async_hold(self) -> None:
"""Remote stop entity."""
_LOGGER.debug(f"hold entity {self._attr_name}")
self._async_cancel_future_pending_update()
self._attr_available = False
self.async_write_ha_state()
async def _async_update_write_state(self) -> None:
"""Update the entity state and write it to the state machine."""
if self._cancel_call:
@@ -145,7 +159,7 @@ class BasePlatform(Entity):
@callback
def async_run(self) -> None:
"""Remote start entity."""
self._async_cancel_update_polling()
_LOGGER.info(f"start entity {self._attr_name}")
self._async_schedule_future_update(0.1)
self._cancel_call = async_call_later(
self.hass, timedelta(seconds=0.1), self.async_local_update
@@ -168,20 +182,6 @@ class BasePlatform(Entity):
self._cancel_call()
self._cancel_call = None
def _async_cancel_update_polling(self) -> None:
"""Cancel the polling."""
if self._cancel_timer:
self._cancel_timer()
self._cancel_timer = None
@callback
def async_hold(self) -> None:
"""Remote stop entity."""
self._async_cancel_future_pending_update()
self._async_cancel_update_polling()
self._attr_available = False
self.async_write_ha_state()
async def async_await_connection(self, _now: Any) -> None:
"""Wait for first connect."""
await self._hub.event_connected.wait()

View File

@@ -359,6 +359,7 @@ class ModbusHub:
async def async_close(self) -> None:
"""Disconnect client."""
self.event_connected.set()
if not self._connect_task.done():
self._connect_task.cancel()