1 """Support for ecobee."""
3 from datetime
import timedelta
5 from pyecobee
import ECOBEE_API_KEY, ECOBEE_REFRESH_TOKEN, Ecobee, ExpiredTokenError
6 import voluptuous
as vol
26 CONFIG_SCHEMA = vol.Schema(
27 {DOMAIN: vol.Schema({vol.Optional(CONF_API_KEY): cv.string})}, extra=vol.ALLOW_EXTRA
31 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
32 """Ecobee uses config flow for configuration.
34 But, an "ecobee:" entry in configuration.yaml will trigger an import flow
35 if a config entry doesn't already exist. If ecobee.conf exists, the import
36 flow will attempt to import it and create a config entry, to assist users
37 migrating from the old ecobee integration. Otherwise, the user will have to
38 continue setting up the integration via the config flow.
41 hass.data[DATA_ECOBEE_CONFIG] = config.get(DOMAIN, {})
42 hass.data[DATA_HASS_CONFIG] = config
44 if not hass.config_entries.async_entries(DOMAIN)
and hass.data[DATA_ECOBEE_CONFIG]:
46 hass.async_create_task(
47 hass.config_entries.flow.async_init(
48 DOMAIN, context={
"source": SOURCE_IMPORT}
56 """Set up ecobee via a config entry."""
57 api_key = entry.data[CONF_API_KEY]
58 refresh_token = entry.data[CONF_REFRESH_TOKEN]
60 data =
EcobeeData(hass, entry, api_key=api_key, refresh_token=refresh_token)
62 if not await data.refresh():
67 if data.ecobee.thermostats
is None:
68 _LOGGER.error(
"No ecobee devices found to set up")
71 hass.data[DOMAIN] = data
73 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
79 """Handle getting the latest data from ecobee.com so platforms can use it.
81 Also handle refreshing tokens and updating config entry with refreshed tokens.
85 self, hass: HomeAssistant, entry: ConfigEntry, api_key: str, refresh_token: str
87 """Initialize the Ecobee data object."""
91 config={ECOBEE_API_KEY: api_key, ECOBEE_REFRESH_TOKEN: refresh_token}
94 @Throttle(MIN_TIME_BETWEEN_UPDATES)
96 """Get the latest data from ecobee.com."""
98 await self.
_hass_hass.async_add_executor_job(self.
ecobeeecobee.update)
99 _LOGGER.debug(
"Updating ecobee")
100 except ExpiredTokenError:
101 _LOGGER.debug(
"Refreshing expired ecobee tokens")
105 """Refresh ecobee tokens and update config entry."""
106 _LOGGER.debug(
"Refreshing ecobee tokens and updating config entry")
107 if await self.
_hass_hass.async_add_executor_job(self.
ecobeeecobee.refresh_tokens):
108 self.
_hass_hass.config_entries.async_update_entry(
111 CONF_API_KEY: self.
ecobeeecobee.config[ECOBEE_API_KEY],
112 CONF_REFRESH_TOKEN: self.
ecobeeecobee.config[ECOBEE_REFRESH_TOKEN],
116 _LOGGER.error(
"Error refreshing ecobee tokens")
121 """Unload the config entry and platforms."""
122 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
124 hass.data.pop(DOMAIN)
None __init__(self, HomeAssistant hass, ConfigEntry entry, str api_key, str refresh_token)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)