2020-08-20 15:30:41 +00:00
|
|
|
"""The Broadlink integration."""
|
2021-05-20 06:10:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
from dataclasses import dataclass, field
|
2019-04-12 18:11:36 +00:00
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .device import BroadlinkDevice
|
2021-05-20 06:10:13 +00:00
|
|
|
from .heartbeat import BroadlinkHeartbeat
|
2019-04-12 18:11:36 +00:00
|
|
|
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
@dataclass
|
|
|
|
class BroadlinkData:
|
|
|
|
"""Class for sharing data within the Broadlink integration."""
|
2019-04-12 18:11:36 +00:00
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
devices: dict = field(default_factory=dict)
|
|
|
|
platforms: dict = field(default_factory=dict)
|
2021-05-20 06:10:13 +00:00
|
|
|
heartbeat: BroadlinkHeartbeat | None = None
|
2019-04-12 18:11:36 +00:00
|
|
|
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the Broadlink integration."""
|
|
|
|
hass.data[DOMAIN] = BroadlinkData()
|
|
|
|
return True
|
2019-04-12 18:11:36 +00:00
|
|
|
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Set up a Broadlink device from a config entry."""
|
2021-05-20 06:10:13 +00:00
|
|
|
data = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
if data.heartbeat is None:
|
|
|
|
data.heartbeat = BroadlinkHeartbeat(hass)
|
|
|
|
hass.async_create_task(data.heartbeat.async_setup())
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
device = BroadlinkDevice(hass, entry)
|
|
|
|
return await device.async_setup()
|
2019-12-02 21:20:36 +00:00
|
|
|
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
2021-05-20 06:10:13 +00:00
|
|
|
data = hass.data[DOMAIN]
|
|
|
|
|
|
|
|
device = data.devices.pop(entry.entry_id)
|
|
|
|
result = await device.async_unload()
|
|
|
|
|
|
|
|
if not data.devices:
|
|
|
|
await data.heartbeat.async_unload()
|
|
|
|
data.heartbeat = None
|
|
|
|
|
|
|
|
return result
|