Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Integration with the Rachio Iro sprinkler system controller."""
2 
3 import logging
4 import secrets
5 
6 from rachiopy import Rachio
7 from requests.exceptions import ConnectTimeout
8 
9 from homeassistant.components import cloud
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_API_KEY, CONF_WEBHOOK_ID, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
14 
15 from .const import CONF_CLOUDHOOK_URL, CONF_MANUAL_RUN_MINS, DOMAIN
16 from .device import RachioPerson
17 from .webhooks import (
18  async_get_or_create_registered_webhook_id_and_url,
19  async_register_webhook,
20  async_unregister_webhook,
21 )
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 PLATFORMS = [Platform.BINARY_SENSOR, Platform.CALENDAR, Platform.SWITCH]
26 
27 
28 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
29  """Unload a config entry."""
30  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
31  async_unregister_webhook(hass, entry)
32  hass.data[DOMAIN].pop(entry.entry_id)
33  return unload_ok
34 
35 
36 async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
37  """Remove a rachio config entry."""
38  if CONF_CLOUDHOOK_URL in entry.data:
39  await cloud.async_delete_cloudhook(hass, entry.data[CONF_WEBHOOK_ID])
40 
41 
42 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
43  """Set up the Rachio config entry."""
44 
45  config = entry.data
46  options = entry.options
47 
48  # CONF_MANUAL_RUN_MINS can only come from a yaml import
49  if not options.get(CONF_MANUAL_RUN_MINS) and config.get(CONF_MANUAL_RUN_MINS):
50  options_copy = options.copy()
51  options_copy[CONF_MANUAL_RUN_MINS] = config[CONF_MANUAL_RUN_MINS]
52  hass.config_entries.async_update_entry(entry, options=options_copy)
53 
54  # Configure API
55  api_key = config[CONF_API_KEY]
56  rachio = Rachio(api_key)
57 
58  # Get the URL of this server
59  rachio.webhook_auth = secrets.token_hex()
60  try:
62  hass, entry
63  )
64  except cloud.CloudNotConnected as exc:
65  # User has an active cloud subscription, but the connection to the cloud is down
66  raise ConfigEntryNotReady from exc
67  rachio.webhook_url = webhook_url
68 
69  person = RachioPerson(rachio, entry)
70 
71  # Get the API user
72  try:
73  await person.async_setup(hass)
74  except ConfigEntryAuthFailed as error:
75  # Reauth is not yet implemented
76  _LOGGER.error("Authentication failed: %s", error)
77  return False
78  except ConnectTimeout as error:
79  _LOGGER.error("Could not reach the Rachio API: %s", error)
80  raise ConfigEntryNotReady from error
81 
82  # Check for Rachio controller devices
83  if not person.controllers and not person.base_stations:
84  _LOGGER.error("No Rachio devices found in account %s", person.username)
85  return False
86  _LOGGER.debug(
87  (
88  "%d Rachio device(s) found; The url %s must be accessible from the internet"
89  " in order to receive updates"
90  ),
91  len(person.controllers) + len(person.base_stations),
92  webhook_url,
93  )
94 
95  for base in person.base_stations:
96  await base.status_coordinator.async_config_entry_first_refresh()
97  await base.schedule_coordinator.async_config_entry_first_refresh()
98 
99  # Enable platform
100  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = person
101  async_register_webhook(hass, entry)
102 
103  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
104 
105  return True
None async_register_webhook(HomeAssistant hass, ConfigEntry entry)
Definition: webhooks.py:86
None async_unregister_webhook(HomeAssistant hass, ConfigEntry entry)
Definition: webhooks.py:117
str async_get_or_create_registered_webhook_id_and_url(HomeAssistant hass, ConfigEntry entry)
Definition: webhooks.py:125
None async_remove_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:36
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:42
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:28