diff --git a/homeassistant/components/brother/__init__.py b/homeassistant/components/brother/__init__.py index d4cd5d4a2b5..f7e5fbdb0d1 100644 --- a/homeassistant/components/brother/__init__.py +++ b/homeassistant/components/brother/__init__.py @@ -6,7 +6,7 @@ import logging from brother import Brother, SnmpError, UnsupportedModel from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST, CONF_TYPE +from homeassistant.const import CONF_HOST, CONF_TYPE, EVENT_HOMEASSISTANT_STOP from homeassistant.core import Config, HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -58,7 +58,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): ) ) if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) + hass.data[DOMAIN].pop(entry.entry_id).shutdown() return unload_ok @@ -69,6 +69,9 @@ class BrotherDataUpdateCoordinator(DataUpdateCoordinator): def __init__(self, hass, host, kind): """Initialize.""" self.brother = Brother(host, kind=kind) + self._unsub_stop = hass.bus.async_listen( + EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop + ) super().__init__( hass, @@ -79,8 +82,22 @@ class BrotherDataUpdateCoordinator(DataUpdateCoordinator): async def _async_update_data(self): """Update data via library.""" + # Race condition on shutdown. Stop all the fetches. + if self._unsub_stop is None: + return None + try: await self.brother.async_update() except (ConnectionError, SnmpError, UnsupportedModel) as error: raise UpdateFailed(error) from error return self.brother.data + + def shutdown(self): + """Shutdown the Brother coordinator.""" + self._unsub_stop() + self._unsub_stop = None + self.brother.shutdown() + + def _handle_ha_stop(self, _): + """Handle Home Assistant stopping.""" + self.shutdown() diff --git a/homeassistant/components/brother/config_flow.py b/homeassistant/components/brother/config_flow.py index 306945b0424..aa9d7ce53a3 100644 --- a/homeassistant/components/brother/config_flow.py +++ b/homeassistant/components/brother/config_flow.py @@ -50,6 +50,7 @@ class BrotherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): brother = Brother(user_input[CONF_HOST]) await brother.async_update() + brother.shutdown() await self.async_set_unique_id(brother.serial.lower()) self._abort_if_unique_id_configured() diff --git a/homeassistant/components/brother/manifest.json b/homeassistant/components/brother/manifest.json index 2e73f9b8450..3c40da60097 100644 --- a/homeassistant/components/brother/manifest.json +++ b/homeassistant/components/brother/manifest.json @@ -3,7 +3,7 @@ "name": "Brother Printer", "documentation": "https://www.home-assistant.io/integrations/brother", "codeowners": ["@bieniu"], - "requirements": ["brother==0.1.17"], + "requirements": ["brother==0.1.18"], "zeroconf": [{"type": "_printer._tcp.local.", "name":"brother*"}], "config_flow": true, "quality_scale": "platinum" diff --git a/requirements_all.txt b/requirements_all.txt index 83225c39642..58a9b8138ba 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -386,7 +386,7 @@ bravia-tv==1.0.6 broadlink==0.15.0 # homeassistant.components.brother -brother==0.1.17 +brother==0.1.18 # homeassistant.components.brottsplatskartan brottsplatskartan==0.0.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index d17dffacbb4..d8895391d46 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -207,7 +207,7 @@ bravia-tv==1.0.6 broadlink==0.15.0 # homeassistant.components.brother -brother==0.1.17 +brother==0.1.18 # homeassistant.components.bsblan bsblan==0.3.7