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
|
2018-07-01 15:54:51 +00:00
|
|
|
import voluptuous as vol
|
2019-12-02 23:59:13 +00:00
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_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
|
|
|
|
2020-03-14 05:46:17 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_CUSTOM_URL,
|
|
|
|
CONF_MANUAL_RUN_MINS,
|
|
|
|
DEFAULT_MANUAL_RUN_MINS,
|
|
|
|
DOMAIN,
|
|
|
|
RACHIO_API_EXCEPTIONS,
|
|
|
|
)
|
2020-03-31 21:46:30 +00:00
|
|
|
from .device import RachioPerson
|
|
|
|
from .webhooks import WEBHOOK_PATH, RachioWebhookView
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORTED_DOMAINS = ["switch", "binary_sensor"]
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_CUSTOM_URL): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_MANUAL_RUN_MINS, default=DEFAULT_MANUAL_RUN_MINS
|
|
|
|
): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
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."""
|
|
|
|
|
|
|
|
conf = config.get(DOMAIN)
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
if not conf:
|
|
|
|
return True
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in SUPPORTED_DOMAINS
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2020-03-14 05:46:17 +00:00
|
|
|
custom_url = config.get(CONF_CUSTOM_URL)
|
2018-07-01 15:54:51 +00:00
|
|
|
hass_url = hass.config.api.base_url if custom_url is None else custom_url
|
2019-12-12 15:46:33 +00:00
|
|
|
rachio.webhook_auth = secrets.token_hex()
|
2020-03-14 05:46:17 +00:00
|
|
|
webhook_url_path = f"{WEBHOOK_PATH}-{entry.entry_id}"
|
|
|
|
rachio.webhook_url = f"{hass_url}{webhook_url_path}"
|
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-03-14 05:46:17 +00:00
|
|
|
# Yes we really do get all these exceptions (hopefully rachiopy switches to requests)
|
|
|
|
# and there is not a reasonable timeout here so it can block for a long time
|
|
|
|
except RACHIO_API_EXCEPTIONS as error:
|
2018-07-01 15:54:51 +00:00
|
|
|
_LOGGER.error("Could not reach the Rachio API: %s", error)
|
2020-03-14 05:46:17 +00:00
|
|
|
raise ConfigEntryNotReady
|
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
|
2018-07-23 08:16:05 +00:00
|
|
|
_LOGGER.info("%d Rachio device(s) found", len(person.controllers))
|
2018-07-01 15:54:51 +00:00
|
|
|
|
|
|
|
# Enable component
|
2020-03-14 05:46:17 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = person
|
|
|
|
|
|
|
|
# Listen for incoming webhook connections after the data is there
|
|
|
|
hass.http.register_view(RachioWebhookView(entry.entry_id, webhook_url_path))
|
2018-09-27 21:17:15 +00:00
|
|
|
|
|
|
|
for component in SUPPORTED_DOMAINS:
|
2020-03-14 05:46:17 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
2018-09-27 21:17:15 +00:00
|
|
|
|
2018-07-01 15:54:51 +00:00
|
|
|
return True
|