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-31 12:52:43 +00:00
|
|
|
from homeassistant.components.snmp import async_get_snmp_engine
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
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 .coordinator import BrotherDataUpdateCoordinator
|
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-17 13:42:58 +00:00
|
|
|
type 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
|
|
|
|
2024-05-31 12:52:43 +00:00
|
|
|
snmp_engine = await async_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
|
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."""
|
2024-05-31 12:52:43 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|