Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Big Ass Fans integration."""
2 
3 from __future__ import annotations
4 
5 from asyncio import timeout
6 
7 from aiobafi6 import Device, Service
8 from aiobafi6.discovery import PORT
9 from aiobafi6.exceptions import DeviceUUIDMismatchError
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_IP_ADDRESS, Platform
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.exceptions import ConfigEntryNotReady
15 
16 from .const import QUERY_INTERVAL, RUN_TIMEOUT
17 
18 type BAFConfigEntry = ConfigEntry[Device]
19 
20 
21 PLATFORMS: list[Platform] = [
22  Platform.BINARY_SENSOR,
23  Platform.CLIMATE,
24  Platform.FAN,
25  Platform.LIGHT,
26  Platform.NUMBER,
27  Platform.SENSOR,
28  Platform.SWITCH,
29 ]
30 
31 
32 async def async_setup_entry(hass: HomeAssistant, entry: BAFConfigEntry) -> bool:
33  """Set up Big Ass Fans from a config entry."""
34  ip_address = entry.data[CONF_IP_ADDRESS]
35 
36  service = Service(ip_addresses=[ip_address], uuid=entry.unique_id, port=PORT)
37  device = Device(service, query_interval_seconds=QUERY_INTERVAL)
38  run_future = device.async_run()
39 
40  try:
41  async with timeout(RUN_TIMEOUT):
42  await device.async_wait_available()
43  except DeviceUUIDMismatchError as ex:
44  raise ConfigEntryNotReady(
45  f"Unexpected device found at {ip_address}; expected {entry.unique_id}, found {device.dns_sd_uuid}"
46  ) from ex
47  except TimeoutError as ex:
48  run_future.cancel()
49  raise ConfigEntryNotReady(f"Timed out connecting to {ip_address}") from ex
50 
51  @callback
52  def _async_cancel_run() -> None:
53  run_future.cancel()
54 
55  entry.runtime_data = device
56  entry.async_on_unload(_async_cancel_run)
57  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
58 
59  return True
60 
61 
62 async def async_unload_entry(hass: HomeAssistant, entry: BAFConfigEntry) -> bool:
63  """Unload a config entry."""
64  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, BAFConfigEntry entry)
Definition: __init__.py:62
bool async_setup_entry(HomeAssistant hass, BAFConfigEntry entry)
Definition: __init__.py:32