Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Hisense AEH-W4A1 integration."""
2 
3 import ipaddress
4 import logging
5 
6 from pyaehw4a1.aehw4a1 import AehW4a1
7 import pyaehw4a1.exceptions
8 import voluptuous as vol
9 
10 from homeassistant import config_entries
11 from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import CONF_IP_ADDRESS, Platform
14 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.typing import ConfigType
17 
18 from .const import DOMAIN
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 PLATFORMS = [Platform.CLIMATE]
23 
24 
25 def coerce_ip(value):
26  """Validate that provided value is a valid IP address."""
27  if not value:
28  raise vol.Invalid("Must define an IP address")
29  try:
30  ipaddress.IPv4Network(value)
31  except ValueError as err:
32  raise vol.Invalid("Not a valid IP address") from err
33  return value
34 
35 
36 CONFIG_SCHEMA = vol.Schema(
37  {
38  DOMAIN: {
39  CLIMATE_DOMAIN: vol.Schema(
40  {
41  vol.Optional(CONF_IP_ADDRESS, default=[]): vol.All(
42  cv.ensure_list, [vol.All(cv.string, coerce_ip)]
43  )
44  }
45  )
46  }
47  },
48  extra=vol.ALLOW_EXTRA,
49 )
50 
51 
52 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
53  """Set up the Hisense AEH-W4A1 integration."""
54  conf = config.get(DOMAIN)
55  hass.data[DOMAIN] = {}
56 
57  if conf is not None:
58  devices = conf[CONF_IP_ADDRESS][:]
59  for device in devices:
60  try:
61  await AehW4a1(device).check()
62  except pyaehw4a1.exceptions.ConnectionError:
63  conf[CONF_IP_ADDRESS].remove(device)
64  _LOGGER.warning("Hisense AEH-W4A1 at %s not found", device)
65  if conf[CONF_IP_ADDRESS]:
66  hass.data[DOMAIN] = conf
67  hass.async_create_task(
68  hass.config_entries.flow.async_init(
69  DOMAIN,
70  context={"source": config_entries.SOURCE_IMPORT},
71  )
72  )
73 
74  return True
75 
76 
77 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
78  """Set up a config entry for Hisense AEH-W4A1."""
79  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
80  return True
81 
82 
83 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
84  """Unload a config entry."""
85  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool remove(self, _T matcher)
Definition: match.py:214
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:52
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:83
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:77
def check(config_dir, secrets=False)