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 <nick@koston.org>
This commit is contained in:
Maciej Bieniek 2024-05-31 14:52:43 +02:00 committed by GitHub
parent 5ed9d58a7b
commit c85743822a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 10 additions and 78 deletions

View file

@ -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)

View file

@ -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:

View file

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

View file

@ -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",

View file

@ -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

View file

@ -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