2019-04-03 15:40:03 +00:00
|
|
|
"""Integration with the Rachio Iro sprinkler system controller."""
|
2018-07-01 15:54:51 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2019-12-12 15:46:33 +00:00
|
|
|
import secrets
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2019-12-02 23:59:13 +00:00
|
|
|
from rachiopy import Rachio
|
2020-10-11 00:44:49 +00:00
|
|
|
from requests.exceptions import ConnectTimeout
|
2019-12-02 23:59:13 +00:00
|
|
|
|
2021-01-21 20:07:47 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-03-31 21:46:30 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2020-03-14 05:46:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2020-03-31 21:46:30 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2021-01-21 20:07:47 +00:00
|
|
|
from .const import CONF_CLOUDHOOK_URL, CONF_MANUAL_RUN_MINS, CONF_WEBHOOK_ID, DOMAIN
|
2020-03-31 21:46:30 +00:00
|
|
|
from .device import RachioPerson
|
2020-04-05 22:47:27 +00:00
|
|
|
from .webhooks import (
|
|
|
|
async_get_or_create_registered_webhook_id_and_url,
|
|
|
|
async_register_webhook,
|
|
|
|
)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
PLATFORMS = ["switch", "binary_sensor"]
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2021-01-21 20:07:47 +00:00
|
|
|
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the rachio component from YAML."""
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
2021-03-02 20:43:59 +00:00
|
|
|
hass.config_entries.async_forward_entry_unload(entry, platform)
|
|
|
|
for platform in PLATFORMS
|
2020-03-14 05:46:17 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2020-04-05 22:47:27 +00:00
|
|
|
async def async_remove_entry(hass, entry):
|
|
|
|
"""Remove a rachio config entry."""
|
|
|
|
if CONF_CLOUDHOOK_URL in entry.data:
|
|
|
|
await hass.components.cloud.async_delete_cloudhook(entry.data[CONF_WEBHOOK_ID])
|
|
|
|
|
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up the Rachio config entry."""
|
|
|
|
|
|
|
|
config = entry.data
|
|
|
|
options = entry.options
|
|
|
|
|
|
|
|
# CONF_MANUAL_RUN_MINS can only come from a yaml import
|
|
|
|
if not options.get(CONF_MANUAL_RUN_MINS) and config.get(CONF_MANUAL_RUN_MINS):
|
2020-03-16 02:01:41 +00:00
|
|
|
options_copy = options.copy()
|
|
|
|
options_copy[CONF_MANUAL_RUN_MINS] = config[CONF_MANUAL_RUN_MINS]
|
2020-04-05 22:25:31 +00:00
|
|
|
hass.config_entries.async_update_entry(entry, options=options_copy)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
|
|
|
# Configure API
|
2020-03-16 02:01:41 +00:00
|
|
|
api_key = config[CONF_API_KEY]
|
2018-07-01 15:54:51 +00:00
|
|
|
rachio = Rachio(api_key)
|
|
|
|
|
|
|
|
# Get the URL of this server
|
2019-12-12 15:46:33 +00:00
|
|
|
rachio.webhook_auth = secrets.token_hex()
|
2020-04-05 22:47:27 +00:00
|
|
|
webhook_id, webhook_url = await async_get_or_create_registered_webhook_id_and_url(
|
|
|
|
hass, entry
|
|
|
|
)
|
|
|
|
rachio.webhook_url = webhook_url
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2020-03-16 02:01:41 +00:00
|
|
|
person = RachioPerson(rachio, entry)
|
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
# Get the API user
|
|
|
|
try:
|
2020-03-16 02:01:41 +00:00
|
|
|
await hass.async_add_executor_job(person.setup, hass)
|
2020-10-11 00:44:49 +00:00
|
|
|
except ConnectTimeout as error:
|
2018-07-01 15:54:51 +00:00
|
|
|
_LOGGER.error("Could not reach the Rachio API: %s", error)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from error
|
2018-07-01 15:54:51 +00:00
|
|
|
|
|
|
|
# Check for Rachio controller devices
|
|
|
|
if not person.controllers:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("No Rachio devices found in account %s", person.username)
|
2018-07-01 15:54:51 +00:00
|
|
|
return False
|
2020-05-01 00:41:24 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
"%d Rachio device(s) found; The url %s must be accessible from the internet in order to receive updates",
|
|
|
|
len(person.controllers),
|
|
|
|
webhook_url,
|
|
|
|
)
|
2018-07-01 15:54:51 +00:00
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
# Enable platform
|
2020-03-14 05:46:17 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = person
|
2020-04-05 22:47:27 +00:00
|
|
|
async_register_webhook(hass, webhook_id, entry.entry_id)
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
for platform in PLATFORMS:
|
2020-03-14 05:46:17 +00:00
|
|
|
hass.async_create_task(
|
2021-03-02 20:43:59 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(entry, platform)
|
2020-03-14 05:46:17 +00:00
|
|
|
)
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
return True
|