Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Deluge integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from ssl import SSLError
7 
8 from deluge_client.client import DelugeRPCClient
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import (
12  CONF_HOST,
13  CONF_PASSWORD,
14  CONF_PORT,
15  CONF_USERNAME,
16  Platform,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
20 
21 from .const import CONF_WEB_PORT
22 from .coordinator import DelugeDataUpdateCoordinator
23 
24 PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
25 
26 _LOGGER = logging.getLogger(__name__)
27 type DelugeConfigEntry = ConfigEntry[DelugeDataUpdateCoordinator]
28 
29 
30 async def async_setup_entry(hass: HomeAssistant, entry: DelugeConfigEntry) -> bool:
31  """Set up Deluge from a config entry."""
32  host = entry.data[CONF_HOST]
33  port = entry.data[CONF_PORT]
34  username = entry.data[CONF_USERNAME]
35  password = entry.data[CONF_PASSWORD]
36  api = await hass.async_add_executor_job(
37  DelugeRPCClient, host, port, username, password
38  )
39  api.web_port = entry.data[CONF_WEB_PORT]
40  try:
41  await hass.async_add_executor_job(api.connect)
42  except (ConnectionRefusedError, TimeoutError, SSLError) as ex:
43  raise ConfigEntryNotReady("Connection to Deluge Daemon failed") from ex
44  except Exception as ex: # noqa: BLE001
45  if type(ex).__name__ == "BadLoginError":
47  "Credentials for Deluge client are not valid"
48  ) from ex
49  _LOGGER.error("Unknown error connecting to Deluge: %s", ex)
50 
51  coordinator = DelugeDataUpdateCoordinator(hass, api, entry)
52  await coordinator.async_config_entry_first_refresh()
53  entry.runtime_data = coordinator
54  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
55 
56  return True
57 
58 
59 async def async_unload_entry(hass: HomeAssistant, entry: DelugeConfigEntry) -> bool:
60  """Unload a config entry."""
61  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, DelugeConfigEntry entry)
Definition: __init__.py:30
bool async_unload_entry(HomeAssistant hass, DelugeConfigEntry entry)
Definition: __init__.py:59