2019-09-25 20:38:21 +00:00
|
|
|
"""Support for ecobee."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2016-02-19 05:27:50 +00:00
|
|
|
from datetime import timedelta
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2019-12-08 17:01:50 +00:00
|
|
|
from pyecobee import ECOBEE_API_KEY, ECOBEE_REFRESH_TOKEN, Ecobee, ExpiredTokenError
|
|
|
|
import voluptuous as vol
|
2019-09-25 20:38:21 +00:00
|
|
|
|
2021-12-31 10:05:44 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2022-10-21 21:19:26 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform
|
2021-12-31 10:05:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-10-21 21:19:26 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, discovery
|
2021-12-31 10:05:44 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import Throttle
|
2016-09-07 18:21:42 +00:00
|
|
|
|
2022-10-21 21:19:26 +00:00
|
|
|
from .const import (
|
|
|
|
_LOGGER,
|
|
|
|
ATTR_CONFIG_ENTRY_ID,
|
|
|
|
CONF_REFRESH_TOKEN,
|
|
|
|
DATA_ECOBEE_CONFIG,
|
|
|
|
DATA_HASS_CONFIG,
|
|
|
|
DOMAIN,
|
|
|
|
PLATFORMS,
|
|
|
|
)
|
2016-09-07 18:21:42 +00:00
|
|
|
|
2024-04-22 17:28:08 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=180)
|
2016-09-07 18:21:42 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2019-09-25 20:38:21 +00:00
|
|
|
{DOMAIN: vol.Schema({vol.Optional(CONF_API_KEY): cv.string})}, extra=vol.ALLOW_EXTRA
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-08-21 17:29:13 +00:00
|
|
|
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2021-12-31 10:05:44 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2023-02-03 22:08:48 +00:00
|
|
|
"""Ecobee uses config flow for configuration.
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
But, an "ecobee:" entry in configuration.yaml will trigger an import flow
|
|
|
|
if a config entry doesn't already exist. If ecobee.conf exists, the import
|
|
|
|
flow will attempt to import it and create a config entry, to assist users
|
2021-03-02 20:43:59 +00:00
|
|
|
migrating from the old ecobee integration. Otherwise, the user will have to
|
2019-09-25 20:38:21 +00:00
|
|
|
continue setting up the integration via the config flow.
|
|
|
|
"""
|
2022-10-21 21:19:26 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
hass.data[DATA_ECOBEE_CONFIG] = config.get(DOMAIN, {})
|
2022-10-21 21:19:26 +00:00
|
|
|
hass.data[DATA_HASS_CONFIG] = config
|
2019-09-25 20:38:21 +00:00
|
|
|
|
|
|
|
if not hass.config_entries.async_entries(DOMAIN) and hass.data[DATA_ECOBEE_CONFIG]:
|
|
|
|
# No config entry exists and configuration.yaml config exists, trigger the import flow.
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}
|
|
|
|
)
|
|
|
|
)
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
return True
|
2015-11-20 22:47:25 +00:00
|
|
|
|
|
|
|
|
2021-12-31 10:05:44 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2019-09-25 20:38:21 +00:00
|
|
|
"""Set up ecobee via a config entry."""
|
|
|
|
api_key = entry.data[CONF_API_KEY]
|
|
|
|
refresh_token = entry.data[CONF_REFRESH_TOKEN]
|
2015-11-23 16:15:19 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
data = EcobeeData(hass, entry, api_key=api_key, refresh_token=refresh_token)
|
2015-11-23 16:15:19 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
if not await data.refresh():
|
|
|
|
return False
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
await data.update()
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
if data.ecobee.thermostats is None:
|
|
|
|
_LOGGER.error("No ecobee devices found to set up")
|
|
|
|
return False
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
hass.data[DOMAIN] = data
|
2015-11-21 17:24:06 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2015-11-21 17:24:06 +00:00
|
|
|
|
2022-10-21 21:19:26 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
discovery.async_load_platform(
|
|
|
|
hass,
|
|
|
|
Platform.NOTIFY,
|
|
|
|
DOMAIN,
|
|
|
|
{CONF_NAME: entry.title, ATTR_CONFIG_ENTRY_ID: entry.entry_id},
|
|
|
|
hass.data[DATA_HASS_CONFIG],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
return True
|
2015-11-21 17:24:06 +00:00
|
|
|
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
class EcobeeData:
|
2023-02-03 22:08:48 +00:00
|
|
|
"""Handle getting the latest data from ecobee.com so platforms can use it.
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
Also handle refreshing tokens and updating config entry with refreshed tokens.
|
|
|
|
"""
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2023-02-07 13:54:23 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistant, entry: ConfigEntry, api_key: str, refresh_token: str
|
|
|
|
) -> None:
|
2019-09-25 20:38:21 +00:00
|
|
|
"""Initialize the Ecobee data object."""
|
|
|
|
self._hass = hass
|
|
|
|
self._entry = entry
|
|
|
|
self.ecobee = Ecobee(
|
|
|
|
config={ECOBEE_API_KEY: api_key, ECOBEE_REFRESH_TOKEN: refresh_token}
|
|
|
|
)
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
async def update(self):
|
|
|
|
"""Get the latest data from ecobee.com."""
|
|
|
|
try:
|
|
|
|
await self._hass.async_add_executor_job(self.ecobee.update)
|
|
|
|
_LOGGER.debug("Updating ecobee")
|
|
|
|
except ExpiredTokenError:
|
2020-02-20 05:05:22 +00:00
|
|
|
_LOGGER.debug("Refreshing expired ecobee tokens")
|
2019-09-25 20:38:21 +00:00
|
|
|
await self.refresh()
|
|
|
|
|
|
|
|
async def refresh(self) -> bool:
|
|
|
|
"""Refresh ecobee tokens and update config entry."""
|
|
|
|
_LOGGER.debug("Refreshing ecobee tokens and updating config entry")
|
|
|
|
if await self._hass.async_add_executor_job(self.ecobee.refresh_tokens):
|
|
|
|
self._hass.config_entries.async_update_entry(
|
|
|
|
self._entry,
|
|
|
|
data={
|
|
|
|
CONF_API_KEY: self.ecobee.config[ECOBEE_API_KEY],
|
|
|
|
CONF_REFRESH_TOKEN: self.ecobee.config[ECOBEE_REFRESH_TOKEN],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return True
|
2020-02-20 05:05:22 +00:00
|
|
|
_LOGGER.error("Error refreshing ecobee tokens")
|
2019-09-25 20:38:21 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-12-31 10:05:44 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2019-09-25 20:38:21 +00:00
|
|
|
"""Unload the config entry and platforms."""
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
return unload_ok
|