Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Gree Climate integration."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from homeassistant.components.network import async_get_ipv4_broadcast_addresses
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.event import async_track_time_interval
11 
12 from .const import (
13  COORDINATORS,
14  DATA_DISCOVERY_SERVICE,
15  DISCOVERY_SCAN_INTERVAL,
16  DISPATCHERS,
17  DOMAIN,
18 )
19 from .coordinator import DiscoveryService
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 PLATFORMS = [Platform.CLIMATE, Platform.SWITCH]
24 
25 
26 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
27  """Set up Gree Climate from a config entry."""
28  hass.data.setdefault(DOMAIN, {})
29  gree_discovery = DiscoveryService(hass)
30  hass.data[DATA_DISCOVERY_SERVICE] = gree_discovery
31 
32  async def _async_scan_update(_=None):
33  bcast_addr = list(await async_get_ipv4_broadcast_addresses(hass))
34  await gree_discovery.discovery.scan(0, bcast_ifaces=bcast_addr)
35 
36  _LOGGER.debug("Scanning network for Gree devices")
37  await _async_scan_update()
38 
39  entry.async_on_unload(
41  hass, _async_scan_update, timedelta(seconds=DISCOVERY_SCAN_INTERVAL)
42  )
43  )
44 
45  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
46 
47  return True
48 
49 
50 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
51  """Unload a config entry."""
52  if hass.data.get(DATA_DISCOVERY_SERVICE) is not None:
53  hass.data.pop(DATA_DISCOVERY_SERVICE)
54 
55  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
56 
57  if unload_ok:
58  hass.data[DOMAIN].pop(COORDINATORS, None)
59  hass.data[DOMAIN].pop(DISPATCHERS, None)
60 
61  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:26
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:50
set[IPv4Address] async_get_ipv4_broadcast_addresses(HomeAssistant hass)
Definition: __init__.py:104
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)
Definition: event.py:1679