|
1 | | -""" |
2 | | -The McIntosh A/V integration. |
3 | | -""" |
| 1 | +"""The McIntosh A/V integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
4 | 4 |
|
5 | 5 | import logging |
6 | | -from dataclasses import dataclass |
7 | | -from typing import Any |
8 | | -from collections.abc import Mapping |
9 | 6 |
|
10 | 7 | from homeassistant.config_entries import ConfigEntry |
11 | 8 | from homeassistant.const import Platform |
12 | 9 | from homeassistant.core import HomeAssistant |
13 | 10 | from homeassistant.exceptions import ConfigEntryNotReady |
14 | 11 |
|
15 | | -from .pymcintosh import async_get_mcintosh, McIntoshAsync |
16 | | - |
17 | 12 | from .const import CONF_MODEL, CONF_URL, DOMAIN |
| 13 | +from .coordinator import McIntoshCoordinator |
| 14 | +from .pymcintosh import async_get_mcintosh |
18 | 15 | from .utils import get_connection_overrides |
19 | 16 |
|
20 | 17 | LOG = logging.getLogger(__name__) |
21 | 18 |
|
22 | 19 | PLATFORMS = [Platform.MEDIA_PLAYER, Platform.SWITCH, Platform.NUMBER] |
23 | 20 |
|
24 | | - |
25 | | -@dataclass |
26 | | -class DeviceClientDetails: |
27 | | - client: McIntoshAsync |
28 | | - config: Mapping[str, Any] |
| 21 | +type McIntoshConfigEntry = ConfigEntry[McIntoshCoordinator] |
29 | 22 |
|
30 | 23 |
|
31 | | -async def connect_to_device(hass: HomeAssistant, entry: ConfigEntry): |
| 24 | +async def async_setup_entry(hass: HomeAssistant, entry: McIntoshConfigEntry) -> bool: |
| 25 | + """Set up McIntosh from a config entry.""" |
32 | 26 | config = entry.data |
33 | 27 | url = config[CONF_URL] |
34 | 28 | model_id = config[CONF_MODEL] |
35 | 29 |
|
36 | 30 | try: |
37 | | - # connect to the device to confirm everything works |
38 | 31 | client = await async_get_mcintosh( |
39 | 32 | model_id, url, hass.loop, **get_connection_overrides(config) |
40 | 33 | ) |
41 | | - except Exception as e: |
42 | | - raise ConfigEntryNotReady(f'Connection failed to {model_id} @ {url}') from e |
| 34 | + except Exception as err: |
| 35 | + raise ConfigEntryNotReady(f'Connection failed to {model_id} @ {url}') from err |
43 | 36 |
|
44 | | - # save under the entry id so multiple devices can be added to a single HASS |
45 | | - hass.data[DOMAIN][entry.entry_id] = DeviceClientDetails(client, config) |
| 37 | + coordinator = McIntoshCoordinator(hass, client, model_id) |
| 38 | + coordinator.config_entry = entry |
46 | 39 |
|
| 40 | + await coordinator.async_config_entry_first_refresh() |
47 | 41 |
|
48 | | -async def config_update_listener(hass: HomeAssistant, config_entry: ConfigEntry): |
49 | | - """Handle options update.""" |
50 | | - LOG.info(f'Reconnecting to device after reconfiguration: {config_entry}') |
51 | | - await connect_to_device(hass, config_entry) |
| 42 | + hass.data.setdefault(DOMAIN, {}) |
| 43 | + hass.data[DOMAIN][entry.entry_id] = coordinator |
52 | 44 |
|
| 45 | + entry.async_on_unload(entry.add_update_listener(_async_update_listener)) |
53 | 46 |
|
54 | | -async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): |
55 | | - """Set up from a config entry.""" |
56 | | - assert config_entry.unique_id, 'Config entry must have a unique_id set' |
57 | | - hass.data.setdefault(DOMAIN, {}) |
| 47 | + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) |
58 | 48 |
|
59 | | - await connect_to_device(hass, config_entry) |
| 49 | + return True |
60 | 50 |
|
61 | | - # register listener to handle config options changes |
62 | | - config_entry.async_on_unload( |
63 | | - config_entry.add_update_listener(config_update_listener) |
64 | | - ) |
65 | 51 |
|
66 | | - # forward the setup to the media_player platform |
67 | | - # hass.async_create_task( |
68 | | - # hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) |
69 | | - # ) |
70 | | - await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) |
| 52 | +async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: |
| 53 | + """Handle options update.""" |
| 54 | + LOG.info(f'Reloading after configuration change: {entry.title}') |
| 55 | + await hass.config_entries.async_reload(entry.entry_id) |
71 | 56 |
|
72 | | - return True |
| 57 | + |
| 58 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 59 | + """Unload a config entry.""" |
| 60 | + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
| 61 | + if unload_ok: |
| 62 | + hass.data[DOMAIN].pop(entry.entry_id, None) |
| 63 | + return unload_ok |
0 commit comments