Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The drop_connect integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import TYPE_CHECKING
7 
8 from homeassistant.components import mqtt
9 from homeassistant.components.mqtt import ReceiveMessage
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant, callback
13 
14 from .const import CONF_DATA_TOPIC, CONF_DEVICE_TYPE, DOMAIN
15 from .coordinator import DROPDeviceDataUpdateCoordinator
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 PLATFORMS: list[Platform] = [
20  Platform.BINARY_SENSOR,
21  Platform.SELECT,
22  Platform.SENSOR,
23  Platform.SWITCH,
24 ]
25 
26 
27 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
28  """Set up DROP from a config entry."""
29 
30  # Make sure MQTT integration is enabled and the client is available.
31  if not await mqtt.async_wait_for_mqtt_client(hass):
32  _LOGGER.error("MQTT integration is not available")
33  return False
34 
35  if TYPE_CHECKING:
36  assert config_entry.unique_id is not None
37  drop_data_coordinator = DROPDeviceDataUpdateCoordinator(
38  hass, config_entry.unique_id
39  )
40 
41  @callback
42  def mqtt_callback(msg: ReceiveMessage) -> None:
43  """Pass MQTT payload to DROP API parser."""
44  if drop_data_coordinator.drop_api.parse_drop_message(
45  msg.topic, msg.payload, msg.qos, msg.retain
46  ):
47  drop_data_coordinator.async_set_updated_data(None)
48 
49  config_entry.async_on_unload(
50  await mqtt.async_subscribe(
51  hass, config_entry.data[CONF_DATA_TOPIC], mqtt_callback
52  )
53  )
54  _LOGGER.debug(
55  "Entry %s (%s) subscribed to %s",
56  config_entry.unique_id,
57  config_entry.data[CONF_DEVICE_TYPE],
58  config_entry.data[CONF_DATA_TOPIC],
59  )
60 
61  hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = drop_data_coordinator
62  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
63  return True
64 
65 
66 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
67  """Unload a config entry."""
68  if unload_ok := await hass.config_entries.async_unload_platforms(
69  config_entry, PLATFORMS
70  ):
71  hass.data[DOMAIN].pop(config_entry.entry_id)
72  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:66
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:27