Use SnmpEngine stored in hass.data by singleton in Brother integration (#117043)

This commit is contained in:
Maciej Bieniek 2024-05-19 11:33:21 +02:00 committed by GitHub
parent d001e7daea
commit da42a8e1c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 10 deletions

View file

@ -3,13 +3,14 @@
from __future__ import annotations from __future__ import annotations
from brother import Brother, SnmpError from brother import Brother, SnmpError
from pysnmp.hlapi.asyncio.cmdgen import lcd
from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_TYPE, Platform from homeassistant.const import CONF_HOST, CONF_TYPE, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN, SNMP from .const import DOMAIN, SNMP_ENGINE
from .coordinator import BrotherDataUpdateCoordinator from .coordinator import BrotherDataUpdateCoordinator
from .utils import get_snmp_engine 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() await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator entry.runtime_data = coordinator
hass.data.setdefault(DOMAIN, {SNMP: snmp_engine})
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) 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 # We only want to remove the SNMP engine when unloading the last config entry
if unload_ok and len(loaded_entries) == 1: 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 return unload_ok

View file

@ -9,6 +9,6 @@ DOMAIN: Final = "brother"
PRINTER_TYPES: Final = ["laser", "ink"] PRINTER_TYPES: Final = ["laser", "ink"]
SNMP: Final = "snmp" SNMP_ENGINE: Final = "snmp_engine"
UPDATE_INTERVAL = timedelta(seconds=30) UPDATE_INTERVAL = timedelta(seconds=30)

View file

@ -11,12 +11,12 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant, callback from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers import singleton from homeassistant.helpers import singleton
from .const import DOMAIN, SNMP from .const import SNMP_ENGINE
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@singleton.singleton("snmp_engine") @singleton.singleton(SNMP_ENGINE)
def get_snmp_engine(hass: HomeAssistant) -> hlapi.SnmpEngine: def get_snmp_engine(hass: HomeAssistant) -> hlapi.SnmpEngine:
"""Get SNMP engine.""" """Get SNMP engine."""
_LOGGER.debug("Creating SNMP engine") _LOGGER.debug("Creating SNMP engine")
@ -24,9 +24,9 @@ def get_snmp_engine(hass: HomeAssistant) -> hlapi.SnmpEngine:
@callback @callback
def shutdown_listener(ev: Event) -> None: def shutdown_listener(ev: Event) -> None:
if hass.data.get(DOMAIN): if hass.data.get(SNMP_ENGINE):
_LOGGER.debug("Unconfiguring 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) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_listener)

View file

@ -66,8 +66,10 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id) with patch("homeassistant.components.brother.lcd.unconfigure") as mock_unconfigure:
await hass.async_block_till_done() 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 entry.state is ConfigEntryState.NOT_LOADED
assert not hass.data.get(DOMAIN) assert not hass.data.get(DOMAIN)