Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Integration to integrate Keymitt BLE devices with Home Assistant."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from microbot import MicroBotApiClient
8 
9 from homeassistant.components import bluetooth
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_ADDRESS, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 
15 from .const import DOMAIN
16 from .coordinator import MicroBotDataUpdateCoordinator
17 
18 _LOGGER: logging.Logger = logging.getLogger(__package__)
19 PLATFORMS: list[str] = [Platform.SWITCH]
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23  """Set up this integration using UI."""
24  hass.data.setdefault(DOMAIN, {})
25  token: str = entry.data[CONF_ACCESS_TOKEN]
26  bdaddr: str = entry.data[CONF_ADDRESS]
27  ble_device = bluetooth.async_ble_device_from_address(hass, bdaddr)
28  if not ble_device:
29  raise ConfigEntryNotReady(f"Could not find MicroBot with address {bdaddr}")
30  client = MicroBotApiClient(
31  device=ble_device,
32  token=token,
33  )
34  coordinator = MicroBotDataUpdateCoordinator(
35  hass, client=client, ble_device=ble_device
36  )
37 
38  hass.data[DOMAIN][entry.entry_id] = coordinator
39 
40  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
41  entry.async_on_unload(coordinator.async_start())
42 
43  return True
44 
45 
46 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
47  """Handle removal of an entry."""
48  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
49  hass.data[DOMAIN].pop(entry.entry_id)
50 
51  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:22
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:46