diff --git a/homeassistant/components/modbus/entity.py b/homeassistant/components/modbus/entity.py index 689d882a2f3..180495bd226 100644 --- a/homeassistant/components/modbus/entity.py +++ b/homeassistant/components/modbus/entity.py @@ -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() diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index f8604efdc2f..ad451254868 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -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()