Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The D-Link Power Plug integration."""
2 
3 from __future__ import annotations
4 
5 from pyW215.pyW215 import SmartPlug
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 
12 from .const import CONF_USE_LEGACY_PROTOCOL
13 from .data import SmartPlugData
14 
15 type DLinkConfigEntry = ConfigEntry[SmartPlugData]
16 
17 PLATFORMS = [Platform.SWITCH]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: DLinkConfigEntry) -> bool:
21  """Set up D-Link Power Plug from a config entry."""
22  smartplug = await hass.async_add_executor_job(
23  SmartPlug,
24  entry.data[CONF_HOST],
25  entry.data[CONF_PASSWORD],
26  entry.data[CONF_USERNAME],
27  entry.data[CONF_USE_LEGACY_PROTOCOL],
28  )
29  if not smartplug.authenticated and smartplug.use_legacy_protocol:
30  raise ConfigEntryNotReady("Cannot connect/authenticate")
31 
32  entry.runtime_data = SmartPlugData(smartplug)
33  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
34 
35  return True
36 
37 
38 async def async_unload_entry(hass: HomeAssistant, entry: DLinkConfigEntry) -> bool:
39  """Unload a config entry."""
40  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)