Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Brother component."""
2 
3 from __future__ import annotations
4 
5 from brother import Brother, SnmpError
6 
7 from homeassistant.components.snmp import async_get_snmp_engine
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_HOST, CONF_TYPE, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ConfigEntryNotReady
12 
13 from .coordinator import BrotherDataUpdateCoordinator
14 
15 PLATFORMS = [Platform.SENSOR]
16 
17 type BrotherConfigEntry = ConfigEntry[BrotherDataUpdateCoordinator]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
21  """Set up Brother from a config entry."""
22  host = entry.data[CONF_HOST]
23  printer_type = entry.data[CONF_TYPE]
24 
25  snmp_engine = await async_get_snmp_engine(hass)
26  try:
27  brother = await Brother.create(
28  host, printer_type=printer_type, snmp_engine=snmp_engine
29  )
30  except (ConnectionError, SnmpError, TimeoutError) as error:
31  raise ConfigEntryNotReady from error
32 
33  coordinator = BrotherDataUpdateCoordinator(hass, brother)
34  await coordinator.async_config_entry_first_refresh()
35 
36  entry.runtime_data = coordinator
37 
38  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
39 
40  return True
41 
42 
43 async def async_unload_entry(hass: HomeAssistant, entry: BrotherConfigEntry) -> bool:
44  """Unload a config entry."""
45  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, BrotherConfigEntry entry)
Definition: __init__.py:20
bool async_unload_entry(HomeAssistant hass, BrotherConfigEntry entry)
Definition: __init__.py:43
SnmpEngine async_get_snmp_engine(HomeAssistant hass)
Definition: util.py:76