Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Lidarr component."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass, fields
6 
7 from aiopyarr.lidarr_client import LidarrClient
8 from aiopyarr.models.host_configuration import PyArrHostConfiguration
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import device_registry as dr
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 from homeassistant.helpers.device_registry import DeviceEntryType
16 
17 from .const import DEFAULT_NAME, DOMAIN
18 from .coordinator import (
19  AlbumsDataUpdateCoordinator,
20  DiskSpaceDataUpdateCoordinator,
21  QueueDataUpdateCoordinator,
22  StatusDataUpdateCoordinator,
23  WantedDataUpdateCoordinator,
24 )
25 
26 type LidarrConfigEntry = ConfigEntry[LidarrData]
27 
28 PLATFORMS = [Platform.SENSOR]
29 
30 
31 @dataclass(kw_only=True, slots=True)
32 class LidarrData:
33  """Lidarr data type."""
34 
35  disk_space: DiskSpaceDataUpdateCoordinator
36  queue: QueueDataUpdateCoordinator
37  status: StatusDataUpdateCoordinator
38  wanted: WantedDataUpdateCoordinator
39  albums: AlbumsDataUpdateCoordinator
40 
41 
42 async def async_setup_entry(hass: HomeAssistant, entry: LidarrConfigEntry) -> bool:
43  """Set up Lidarr from a config entry."""
44  host_configuration = PyArrHostConfiguration(
45  api_token=entry.data[CONF_API_KEY],
46  verify_ssl=entry.data[CONF_VERIFY_SSL],
47  url=entry.data[CONF_URL],
48  )
49  lidarr = LidarrClient(
50  host_configuration=host_configuration,
51  session=async_get_clientsession(hass, host_configuration.verify_ssl),
52  request_timeout=60,
53  )
54  data = LidarrData(
55  disk_space=DiskSpaceDataUpdateCoordinator(hass, host_configuration, lidarr),
56  queue=QueueDataUpdateCoordinator(hass, host_configuration, lidarr),
57  status=StatusDataUpdateCoordinator(hass, host_configuration, lidarr),
58  wanted=WantedDataUpdateCoordinator(hass, host_configuration, lidarr),
59  albums=AlbumsDataUpdateCoordinator(hass, host_configuration, lidarr),
60  )
61  for field in fields(data):
62  coordinator = getattr(data, field.name)
63  await coordinator.async_config_entry_first_refresh()
64  device_registry = dr.async_get(hass)
65  device_registry.async_get_or_create(
66  config_entry_id=entry.entry_id,
67  configuration_url=entry.data[CONF_URL],
68  entry_type=DeviceEntryType.SERVICE,
69  identifiers={(DOMAIN, entry.entry_id)},
70  manufacturer=DEFAULT_NAME,
71  sw_version=data.status.data,
72  )
73  entry.runtime_data = data
74  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
75 
76  return True
77 
78 
79 async def async_unload_entry(hass: HomeAssistant, entry: LidarrConfigEntry) -> bool:
80  """Unload a config entry."""
81  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, LidarrConfigEntry entry)
Definition: __init__.py:79
bool async_setup_entry(HomeAssistant hass, LidarrConfigEntry entry)
Definition: __init__.py:42
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)