Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The IronOS integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import TYPE_CHECKING
7 
8 from aiogithubapi import GitHubAPI
9 from pynecil import Pynecil
10 
11 from homeassistant.components import bluetooth
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import CONF_NAME, Platform
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import ConfigEntryNotReady
16 from homeassistant.helpers import config_validation as cv
17 from homeassistant.helpers.aiohttp_client import async_get_clientsession
18 from homeassistant.helpers.typing import ConfigType
19 from homeassistant.util.hass_dict import HassKey
20 
21 from .const import DOMAIN
22 from .coordinator import IronOSFirmwareUpdateCoordinator, IronOSLiveDataCoordinator
23 
24 PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR, Platform.UPDATE]
25 
26 
27 type IronOSConfigEntry = ConfigEntry[IronOSLiveDataCoordinator]
28 IRON_OS_KEY: HassKey[IronOSFirmwareUpdateCoordinator] = HassKey(DOMAIN)
29 
30 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
31 
32 _LOGGER = logging.getLogger(__name__)
33 
34 
35 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
36  """Set up IronOS firmware update coordinator."""
37 
38  session = async_get_clientsession(hass)
39  github = GitHubAPI(session=session)
40 
41  hass.data[IRON_OS_KEY] = IronOSFirmwareUpdateCoordinator(hass, github)
42  await hass.data[IRON_OS_KEY].async_request_refresh()
43  return True
44 
45 
46 async def async_setup_entry(hass: HomeAssistant, entry: IronOSConfigEntry) -> bool:
47  """Set up IronOS from a config entry."""
48  if TYPE_CHECKING:
49  assert entry.unique_id
50  ble_device = bluetooth.async_ble_device_from_address(
51  hass, entry.unique_id, connectable=True
52  )
53  if not ble_device:
54  raise ConfigEntryNotReady(
55  translation_domain=DOMAIN,
56  translation_key="setup_device_unavailable_exception",
57  translation_placeholders={CONF_NAME: entry.title},
58  )
59 
60  device = Pynecil(ble_device)
61 
62  coordinator = IronOSLiveDataCoordinator(hass, device)
63  await coordinator.async_config_entry_first_refresh()
64 
65  entry.runtime_data = coordinator
66  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
67 
68  return True
69 
70 
71 async def async_unload_entry(hass: HomeAssistant, entry: IronOSConfigEntry) -> bool:
72  """Unload a config entry."""
73  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:35
bool async_unload_entry(HomeAssistant hass, IronOSConfigEntry entry)
Definition: __init__.py:71
bool async_setup_entry(HomeAssistant hass, IronOSConfigEntry entry)
Definition: __init__.py:46
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)