From da42a8e1c69a5e7f90a02188dbd60847bc4dfced Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Sun, 19 May 2024 11:33:21 +0200 Subject: [PATCH] Use SnmpEngine stored in hass.data by singleton in Brother integration (#117043) --- homeassistant/components/brother/__init__.py | 7 ++++--- homeassistant/components/brother/const.py | 2 +- homeassistant/components/brother/utils.py | 8 ++++---- tests/components/brother/test_init.py | 6 ++++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/brother/__init__.py b/homeassistant/components/brother/__init__.py index a2cd1a7678f..68255d66566 100644 --- a/homeassistant/components/brother/__init__.py +++ b/homeassistant/components/brother/__init__.py @@ -3,13 +3,14 @@ from __future__ import annotations from brother import Brother, SnmpError +from pysnmp.hlapi.asyncio.cmdgen import lcd from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import CONF_HOST, CONF_TYPE, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from .const import DOMAIN, SNMP +from .const import DOMAIN, SNMP_ENGINE from .coordinator import BrotherDataUpdateCoordinator from .utils import get_snmp_engine @@ -35,7 +36,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b await coordinator.async_config_entry_first_refresh() entry.runtime_data = coordinator - hass.data.setdefault(DOMAIN, {SNMP: snmp_engine}) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) @@ -53,6 +53,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> ] # We only want to remove the SNMP engine when unloading the last config entry if unload_ok and len(loaded_entries) == 1: - hass.data[DOMAIN].pop(SNMP) + lcd.unconfigure(hass.data[SNMP_ENGINE], None) + hass.data.pop(SNMP_ENGINE) return unload_ok diff --git a/homeassistant/components/brother/const.py b/homeassistant/components/brother/const.py index f8d29363acd..1b949e1fa52 100644 --- a/homeassistant/components/brother/const.py +++ b/homeassistant/components/brother/const.py @@ -9,6 +9,6 @@ DOMAIN: Final = "brother" PRINTER_TYPES: Final = ["laser", "ink"] -SNMP: Final = "snmp" +SNMP_ENGINE: Final = "snmp_engine" UPDATE_INTERVAL = timedelta(seconds=30) diff --git a/homeassistant/components/brother/utils.py b/homeassistant/components/brother/utils.py index d7636cdd2e8..0d11f7d2e82 100644 --- a/homeassistant/components/brother/utils.py +++ b/homeassistant/components/brother/utils.py @@ -11,12 +11,12 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import Event, HomeAssistant, callback from homeassistant.helpers import singleton -from .const import DOMAIN, SNMP +from .const import SNMP_ENGINE _LOGGER = logging.getLogger(__name__) -@singleton.singleton("snmp_engine") +@singleton.singleton(SNMP_ENGINE) def get_snmp_engine(hass: HomeAssistant) -> hlapi.SnmpEngine: """Get SNMP engine.""" _LOGGER.debug("Creating SNMP engine") @@ -24,9 +24,9 @@ def get_snmp_engine(hass: HomeAssistant) -> hlapi.SnmpEngine: @callback def shutdown_listener(ev: Event) -> None: - if hass.data.get(DOMAIN): + if hass.data.get(SNMP_ENGINE): _LOGGER.debug("Unconfiguring SNMP engine") - lcd.unconfigure(hass.data[DOMAIN][SNMP], None) + lcd.unconfigure(hass.data[SNMP_ENGINE], None) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_listener) diff --git a/tests/components/brother/test_init.py b/tests/components/brother/test_init.py index 582e64c71ae..ef076aacab2 100644 --- a/tests/components/brother/test_init.py +++ b/tests/components/brother/test_init.py @@ -66,8 +66,10 @@ async def test_unload_entry(hass: HomeAssistant) -> None: assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert entry.state is ConfigEntryState.LOADED - assert await hass.config_entries.async_unload(entry.entry_id) - await hass.async_block_till_done() + with patch("homeassistant.components.brother.lcd.unconfigure") as mock_unconfigure: + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + assert mock_unconfigure.called assert entry.state is ConfigEntryState.NOT_LOADED assert not hass.data.get(DOMAIN)