-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
83 lines (60 loc) · 2.29 KB
/
__init__.py
File metadata and controls
83 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""The Example Integration integration."""
from __future__ import annotations
import logging
import aiohttp
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .api import APIClient
from .const import DOMAIN
from .coordinator import ExampleIntegrationCoordinator
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Example Integration from a config entry.
Args:
hass: Home Assistant instance.
entry: Config entry for this integration.
Returns:
True if setup was successful.
"""
# Get configuration from config entry
host = entry.data[CONF_HOST]
api_key = entry.data[CONF_API_KEY]
# Create API client with shared aiohttp session
session = async_get_clientsession(hass)
api_client = APIClient(host, session)
# Authenticate with device
try:
await api_client.authenticate(api_key)
except Exception as err:
_LOGGER.error("Failed to authenticate with device: %s", err)
return False
# Create data update coordinator
coordinator = ExampleIntegrationCoordinator(hass, api_client)
# Fetch initial data
await coordinator.async_config_entry_first_refresh()
# Store coordinator in hass.data
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = coordinator
# Forward setup to platforms
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry.
Args:
hass: Home Assistant instance.
entry: Config entry to unload.
Returns:
True if unload was successful.
"""
# Unload platforms
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
# Clean up coordinator and API client
if unload_ok:
coordinator: ExampleIntegrationCoordinator = hass.data[DOMAIN].pop(
entry.entry_id
)
await coordinator.api_client.close()
return unload_ok