2020-01-06 17:06:16 +00:00
|
|
|
"""The Brother component."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2021-04-29 14:59:31 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
from brother import Brother, SnmpError
|
2023-12-12 21:36:11 +00:00
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
2021-12-03 16:51:30 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_TYPE, Platform
|
2021-03-29 23:22:33 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-12-12 21:36:11 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
from .const import DOMAIN, SNMP
|
|
|
|
from .coordinator import BrotherDataUpdateCoordinator
|
2021-02-12 17:11:35 +00:00
|
|
|
from .utils import get_snmp_engine
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2021-12-03 16:51:30 +00:00
|
|
|
PLATFORMS = [Platform.SENSOR]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
BrotherConfigEntry = ConfigEntry[BrotherDataUpdateCoordinator]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Set up Brother from a config entry."""
|
|
|
|
host = entry.data[CONF_HOST]
|
2022-09-18 21:28:17 +00:00
|
|
|
printer_type = entry.data[CONF_TYPE]
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2021-02-12 17:11:35 +00:00
|
|
|
snmp_engine = get_snmp_engine(hass)
|
2022-09-18 21:28:17 +00:00
|
|
|
try:
|
|
|
|
brother = await Brother.create(
|
|
|
|
host, printer_type=printer_type, snmp_engine=snmp_engine
|
|
|
|
)
|
2024-03-13 16:50:29 +00:00
|
|
|
except (ConnectionError, SnmpError, TimeoutError) as error:
|
2022-09-18 21:28:17 +00:00
|
|
|
raise ConfigEntryNotReady from error
|
2021-02-12 17:11:35 +00:00
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
coordinator = BrotherDataUpdateCoordinator(hass, brother)
|
2021-03-29 22:51:39 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
entry.runtime_data = coordinator
|
|
|
|
hass.data.setdefault(DOMAIN, {SNMP: snmp_engine})
|
2020-01-06 17:06:16 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
|
2020-01-06 17:06:16 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-26 17:46:55 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
|
2024-05-04 14:29:08 +00:00
|
|
|
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:
|
|
|
|
hass.data[DOMAIN].pop(SNMP)
|
2020-01-06 17:06:16 +00:00
|
|
|
|
|
|
|
return unload_ok
|