2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Hydrawise cloud."""
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2023-09-26 07:15:20 +00:00
|
|
|
from pydrawise import legacy
|
2018-05-26 16:42:52 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2023-09-23 22:03:07 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ACCESS_TOKEN,
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
Platform,
|
|
|
|
)
|
2023-05-22 13:47:32 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-04 13:09:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-02 15:31:48 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2023-10-30 14:18:59 +00:00
|
|
|
from .const import DOMAIN, SCAN_INTERVAL
|
2023-05-24 12:07:37 +00:00
|
|
|
from .coordinator import HydrawiseDataUpdateCoordinator
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
|
|
|
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2023-09-23 22:03:07 +00:00
|
|
|
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
|
|
|
|
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2023-05-24 12:07:37 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2018-05-26 16:42:52 +00:00
|
|
|
"""Set up the Hunter Hydrawise component."""
|
2023-09-23 22:03:07 +00:00
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_API_KEY: config[DOMAIN][CONF_ACCESS_TOKEN]},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2023-09-23 22:03:07 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Hydrawise from a config entry."""
|
|
|
|
access_token = config_entry.data[CONF_API_KEY]
|
2023-11-15 23:59:37 +00:00
|
|
|
hydrawise = legacy.LegacyHydrawiseAsync(access_token)
|
2023-10-30 14:18:59 +00:00
|
|
|
coordinator = HydrawiseDataUpdateCoordinator(hass, hydrawise, SCAN_INTERVAL)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
2023-09-23 22:03:07 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2018-05-26 16:42:52 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-09-23 22:03:07 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|