diff --git a/homeassistant/components/device_tracker/legacy.py b/homeassistant/components/device_tracker/legacy.py index 8871a5d0eaf..e27ff57f03f 100644 --- a/homeassistant/components/device_tracker/legacy.py +++ b/homeassistant/components/device_tracker/legacy.py @@ -25,10 +25,11 @@ from homeassistant.const import ( CONF_MAC, CONF_NAME, DEVICE_DEFAULT_NAME, + EVENT_HOMEASSISTANT_STOP, STATE_HOME, STATE_NOT_HOME, ) -from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.core import Event, HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import ( config_per_platform, @@ -216,7 +217,7 @@ async def async_setup_integration(hass: HomeAssistant, config: ConfigType) -> No discovery.async_listen_platform(hass, DOMAIN, async_platform_discovered) # Clean up stale devices - async_track_utc_time_change( + cancel_update_stale = async_track_utc_time_change( hass, tracker.async_update_stale, second=range(0, 60, 5) ) @@ -235,6 +236,16 @@ async def async_setup_integration(hass: HomeAssistant, config: ConfigType) -> No # restore await tracker.async_setup_tracked_device() + @callback + def _on_hass_stop(_: Event) -> None: + """Cleanup when Home Assistant stops. + + Cancel the async_update_stale schedule. + """ + cancel_update_stale() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _on_hass_stop) + @attr.s class DeviceTrackerPlatform: @@ -471,7 +482,7 @@ def async_setup_scanner_platform( hass.async_create_task(async_see_device(**kwargs)) - async_track_time_interval( + cancel_legacy_scan = async_track_time_interval( hass, async_device_tracker_scan, interval, @@ -479,6 +490,16 @@ def async_setup_scanner_platform( ) hass.async_create_task(async_device_tracker_scan(None)) + @callback + def _on_hass_stop(_: Event) -> None: + """Cleanup when Home Assistant stops. + + Cancel the legacy scan. + """ + cancel_legacy_scan() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _on_hass_stop) + async def get_tracker(hass: HomeAssistant, config: ConfigType) -> DeviceTracker: """Create a tracker."""