From d823e5665959017a70a4a845c1b06be605e47cd1 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Fri, 31 May 2024 14:52:43 +0200 Subject: [PATCH] In Brother integration use SnmpEngine from SNMP integration (#118554) Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com> Co-authored-by: J. Nick Koston --- homeassistant/components/brother/__init__.py | 22 +++---------- .../components/brother/config_flow.py | 6 ++-- homeassistant/components/brother/const.py | 2 -- .../components/brother/manifest.json | 1 + homeassistant/components/brother/utils.py | 33 ------------------- tests/components/brother/test_init.py | 24 ++------------ 6 files changed, 10 insertions(+), 78 deletions(-) delete mode 100644 homeassistant/components/brother/utils.py diff --git a/homeassistant/components/brother/__init__.py b/homeassistant/components/brother/__init__.py index 68255d66566..e828d35f9c7 100644 --- a/homeassistant/components/brother/__init__.py +++ b/homeassistant/components/brother/__init__.py @@ -3,16 +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.components.snmp import async_get_snmp_engine +from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_TYPE, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from .const import DOMAIN, SNMP_ENGINE from .coordinator import BrotherDataUpdateCoordinator -from .utils import get_snmp_engine PLATFORMS = [Platform.SENSOR] @@ -24,7 +22,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b host = entry.data[CONF_HOST] printer_type = entry.data[CONF_TYPE] - snmp_engine = get_snmp_engine(hass) + snmp_engine = await async_get_snmp_engine(hass) try: brother = await Brother.create( host, printer_type=printer_type, snmp_engine=snmp_engine @@ -44,16 +42,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b async def async_unload_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - - loaded_entries = [ - entry - for entry in hass.config_entries.async_entries(DOMAIN) - if entry.state == ConfigEntryState.LOADED - ] - # We only want to remove the SNMP engine when unloading the last config entry - if unload_ok and len(loaded_entries) == 1: - lcd.unconfigure(hass.data[SNMP_ENGINE], None) - hass.data.pop(SNMP_ENGINE) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/brother/config_flow.py b/homeassistant/components/brother/config_flow.py index ca2f1ae5a39..2b711186fff 100644 --- a/homeassistant/components/brother/config_flow.py +++ b/homeassistant/components/brother/config_flow.py @@ -8,13 +8,13 @@ from brother import Brother, SnmpError, UnsupportedModelError import voluptuous as vol from homeassistant.components import zeroconf +from homeassistant.components.snmp import async_get_snmp_engine from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_TYPE from homeassistant.exceptions import HomeAssistantError from homeassistant.util.network import is_host_valid from .const import DOMAIN, PRINTER_TYPES -from .utils import get_snmp_engine DATA_SCHEMA = vol.Schema( { @@ -45,7 +45,7 @@ class BrotherConfigFlow(ConfigFlow, domain=DOMAIN): if not is_host_valid(user_input[CONF_HOST]): raise InvalidHost - snmp_engine = get_snmp_engine(self.hass) + snmp_engine = await async_get_snmp_engine(self.hass) brother = await Brother.create( user_input[CONF_HOST], snmp_engine=snmp_engine @@ -79,7 +79,7 @@ class BrotherConfigFlow(ConfigFlow, domain=DOMAIN): # Do not probe the device if the host is already configured self._async_abort_entries_match({CONF_HOST: self.host}) - snmp_engine = get_snmp_engine(self.hass) + snmp_engine = await async_get_snmp_engine(self.hass) model = discovery_info.properties.get("product") try: diff --git a/homeassistant/components/brother/const.py b/homeassistant/components/brother/const.py index 1b949e1fa52..c0ae7cf60b0 100644 --- a/homeassistant/components/brother/const.py +++ b/homeassistant/components/brother/const.py @@ -9,6 +9,4 @@ DOMAIN: Final = "brother" PRINTER_TYPES: Final = ["laser", "ink"] -SNMP_ENGINE: Final = "snmp_engine" - UPDATE_INTERVAL = timedelta(seconds=30) diff --git a/homeassistant/components/brother/manifest.json b/homeassistant/components/brother/manifest.json index 3bbaf40f686..6d4912db4cb 100644 --- a/homeassistant/components/brother/manifest.json +++ b/homeassistant/components/brother/manifest.json @@ -1,6 +1,7 @@ { "domain": "brother", "name": "Brother Printer", + "after_dependencies": ["snmp"], "codeowners": ["@bieniu"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/brother", diff --git a/homeassistant/components/brother/utils.py b/homeassistant/components/brother/utils.py deleted file mode 100644 index 0d11f7d2e82..00000000000 --- a/homeassistant/components/brother/utils.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Brother helpers functions.""" - -from __future__ import annotations - -import logging - -import pysnmp.hlapi.asyncio as hlapi -from pysnmp.hlapi.asyncio.cmdgen import lcd - -from homeassistant.const import EVENT_HOMEASSISTANT_STOP -from homeassistant.core import Event, HomeAssistant, callback -from homeassistant.helpers import singleton - -from .const import SNMP_ENGINE - -_LOGGER = logging.getLogger(__name__) - - -@singleton.singleton(SNMP_ENGINE) -def get_snmp_engine(hass: HomeAssistant) -> hlapi.SnmpEngine: - """Get SNMP engine.""" - _LOGGER.debug("Creating SNMP engine") - snmp_engine = hlapi.SnmpEngine() - - @callback - def shutdown_listener(ev: Event) -> None: - if hass.data.get(SNMP_ENGINE): - _LOGGER.debug("Unconfiguring SNMP engine") - lcd.unconfigure(hass.data[SNMP_ENGINE], None) - - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_listener) - - return snmp_engine diff --git a/tests/components/brother/test_init.py b/tests/components/brother/test_init.py index 2b366348b03..1a2c6bf23f2 100644 --- a/tests/components/brother/test_init.py +++ b/tests/components/brother/test_init.py @@ -7,7 +7,6 @@ import pytest from homeassistant.components.brother.const import DOMAIN from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant from . import init_integration @@ -64,27 +63,8 @@ async def test_unload_entry( assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert mock_config_entry.state is ConfigEntryState.LOADED - with patch("homeassistant.components.brother.lcd.unconfigure") as mock_unconfigure: - assert await hass.config_entries.async_unload(mock_config_entry.entry_id) - await hass.async_block_till_done() - assert mock_unconfigure.called + assert await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() assert mock_config_entry.state is ConfigEntryState.NOT_LOADED assert not hass.data.get(DOMAIN) - - -async def test_unconfigure_snmp_engine_on_ha_stop( - hass: HomeAssistant, - mock_brother_client: AsyncMock, - mock_config_entry: MockConfigEntry, -) -> None: - """Test that the SNMP engine is unconfigured when HA stops.""" - await init_integration(hass, mock_config_entry) - - with patch( - "homeassistant.components.brother.utils.lcd.unconfigure" - ) as mock_unconfigure: - hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) - await hass.async_block_till_done() - - assert mock_unconfigure.called