mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
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:
parent
e401a0da7f
commit
d823e56659
@ -3,16 +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.components.snmp import async_get_snmp_engine
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
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_ENGINE
|
|
||||||
from .coordinator import BrotherDataUpdateCoordinator
|
from .coordinator import BrotherDataUpdateCoordinator
|
||||||
from .utils import get_snmp_engine
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
@ -24,7 +22,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> b
|
|||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
printer_type = entry.data[CONF_TYPE]
|
printer_type = entry.data[CONF_TYPE]
|
||||||
|
|
||||||
snmp_engine = get_snmp_engine(hass)
|
snmp_engine = await async_get_snmp_engine(hass)
|
||||||
try:
|
try:
|
||||||
brother = await Brother.create(
|
brother = await Brother.create(
|
||||||
host, printer_type=printer_type, snmp_engine=snmp_engine
|
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:
|
async def async_unload_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return 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
|
|
||||||
|
@ -8,13 +8,13 @@ from brother import Brother, SnmpError, UnsupportedModelError
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import zeroconf
|
from homeassistant.components import zeroconf
|
||||||
|
from homeassistant.components.snmp import async_get_snmp_engine
|
||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_HOST, CONF_TYPE
|
from homeassistant.const import CONF_HOST, CONF_TYPE
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.util.network import is_host_valid
|
from homeassistant.util.network import is_host_valid
|
||||||
|
|
||||||
from .const import DOMAIN, PRINTER_TYPES
|
from .const import DOMAIN, PRINTER_TYPES
|
||||||
from .utils import get_snmp_engine
|
|
||||||
|
|
||||||
DATA_SCHEMA = vol.Schema(
|
DATA_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
@ -45,7 +45,7 @@ class BrotherConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
if not is_host_valid(user_input[CONF_HOST]):
|
if not is_host_valid(user_input[CONF_HOST]):
|
||||||
raise InvalidHost
|
raise InvalidHost
|
||||||
|
|
||||||
snmp_engine = get_snmp_engine(self.hass)
|
snmp_engine = await async_get_snmp_engine(self.hass)
|
||||||
|
|
||||||
brother = await Brother.create(
|
brother = await Brother.create(
|
||||||
user_input[CONF_HOST], snmp_engine=snmp_engine
|
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
|
# Do not probe the device if the host is already configured
|
||||||
self._async_abort_entries_match({CONF_HOST: self.host})
|
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")
|
model = discovery_info.properties.get("product")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -9,6 +9,4 @@ DOMAIN: Final = "brother"
|
|||||||
|
|
||||||
PRINTER_TYPES: Final = ["laser", "ink"]
|
PRINTER_TYPES: Final = ["laser", "ink"]
|
||||||
|
|
||||||
SNMP_ENGINE: Final = "snmp_engine"
|
|
||||||
|
|
||||||
UPDATE_INTERVAL = timedelta(seconds=30)
|
UPDATE_INTERVAL = timedelta(seconds=30)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"domain": "brother",
|
"domain": "brother",
|
||||||
"name": "Brother Printer",
|
"name": "Brother Printer",
|
||||||
|
"after_dependencies": ["snmp"],
|
||||||
"codeowners": ["@bieniu"],
|
"codeowners": ["@bieniu"],
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/brother",
|
"documentation": "https://www.home-assistant.io/integrations/brother",
|
||||||
|
@ -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
|
|
@ -7,7 +7,6 @@ import pytest
|
|||||||
|
|
||||||
from homeassistant.components.brother.const import DOMAIN
|
from homeassistant.components.brother.const import DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import init_integration
|
from . import init_integration
|
||||||
@ -64,27 +63,8 @@ async def test_unload_entry(
|
|||||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
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)
|
||||||
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
await hass.async_block_till_done()
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert mock_unconfigure.called
|
|
||||||
|
|
||||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
assert not hass.data.get(DOMAIN)
|
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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user