2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ecobee devices."""
|
2015-11-29 21:49:05 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2016-02-19 05:27:50 +00:00
|
|
|
from datetime import timedelta
|
2016-09-07 18:21:42 +00:00
|
|
|
|
2016-08-21 17:29:13 +00:00
|
|
|
import voluptuous as vol
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2016-08-21 17:29:13 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
from homeassistant.const import CONF_API_KEY
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import Throttle
|
2017-11-20 03:47:55 +00:00
|
|
|
from homeassistant.util.json import save_json
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2016-09-07 18:21:42 +00:00
|
|
|
_CONFIGURING = {}
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CONF_HOLD_TEMP = 'hold_temp'
|
|
|
|
|
|
|
|
DOMAIN = 'ecobee'
|
|
|
|
|
|
|
|
ECOBEE_CONFIG_FILE = 'ecobee.conf'
|
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=180)
|
|
|
|
|
|
|
|
NETWORK = None
|
|
|
|
|
2016-08-21 17:29:13 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Optional(CONF_API_KEY): cv.string,
|
2019-02-13 20:21:14 +00:00
|
|
|
vol.Optional(CONF_HOLD_TEMP, default=False): cv.boolean,
|
2016-08-21 17:29:13 +00:00
|
|
|
})
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2015-11-23 16:15:19 +00:00
|
|
|
def request_configuration(network, hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Request configuration steps from the user."""
|
2017-08-14 05:37:50 +00:00
|
|
|
configurator = hass.components.configurator
|
2015-11-20 22:47:25 +00:00
|
|
|
if 'ecobee' in _CONFIGURING:
|
|
|
|
configurator.notify_errors(
|
|
|
|
_CONFIGURING['ecobee'], "Failed to register, please try again.")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
def ecobee_configuration_callback(callback_data):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle configuration callbacks."""
|
2015-11-20 22:47:25 +00:00
|
|
|
network.request_tokens()
|
|
|
|
network.update()
|
2015-11-23 16:15:19 +00:00
|
|
|
setup_ecobee(hass, network, config)
|
2015-11-20 22:47:25 +00:00
|
|
|
|
|
|
|
_CONFIGURING['ecobee'] = configurator.request_config(
|
2017-08-14 05:37:50 +00:00
|
|
|
"Ecobee", ecobee_configuration_callback,
|
2015-11-20 22:47:25 +00:00
|
|
|
description=(
|
|
|
|
'Please authorize this app at https://www.ecobee.com/consumer'
|
2015-11-21 17:24:06 +00:00
|
|
|
'portal/index.html with pin code: ' + network.pin),
|
2015-11-20 22:47:25 +00:00
|
|
|
description_image="/static/images/config_ecobee_thermostat.png",
|
|
|
|
submit_caption="I have authorized the app."
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-11-23 16:15:19 +00:00
|
|
|
def setup_ecobee(hass, network, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Ecobee thermostat."""
|
2015-11-20 22:47:25 +00:00
|
|
|
# If ecobee has a PIN then it needs to be configured.
|
|
|
|
if network.pin is not None:
|
2015-11-23 16:15:19 +00:00
|
|
|
request_configuration(network, hass, config)
|
2015-11-20 22:47:25 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if 'ecobee' in _CONFIGURING:
|
2017-08-14 05:37:50 +00:00
|
|
|
configurator = hass.components.configurator
|
2015-11-20 22:47:25 +00:00
|
|
|
configurator.request_done(_CONFIGURING.pop('ecobee'))
|
|
|
|
|
2016-08-21 17:29:13 +00:00
|
|
|
hold_temp = config[DOMAIN].get(CONF_HOLD_TEMP)
|
2015-11-23 16:15:19 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
discovery.load_platform(
|
|
|
|
hass, 'climate', DOMAIN, {'hold_temp': hold_temp}, config)
|
2016-06-15 05:51:46 +00:00
|
|
|
discovery.load_platform(hass, 'sensor', DOMAIN, {}, config)
|
2016-08-19 07:11:56 +00:00
|
|
|
discovery.load_platform(hass, 'binary_sensor', DOMAIN, {}, config)
|
2017-12-01 11:30:45 +00:00
|
|
|
discovery.load_platform(hass, 'weather', DOMAIN, {}, config)
|
2015-11-23 16:15:19 +00:00
|
|
|
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class EcobeeData:
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Get the latest data and update the states."""
|
|
|
|
|
2015-11-21 17:24:06 +00:00
|
|
|
def __init__(self, config_file):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Init the Ecobee data object."""
|
2015-11-21 17:24:06 +00:00
|
|
|
from pyecobee import Ecobee
|
|
|
|
self.ecobee = Ecobee(config_file)
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Get the latest data from pyecobee."""
|
2015-11-21 17:24:06 +00:00
|
|
|
self.ecobee.update()
|
2016-09-07 18:21:42 +00:00
|
|
|
_LOGGER.info("Ecobee data updated successfully")
|
2015-11-21 17:24:06 +00:00
|
|
|
|
|
|
|
|
2015-11-20 22:47:25 +00:00
|
|
|
def setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Ecobee.
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2015-11-20 22:47:25 +00:00
|
|
|
Will automatically load thermostat and sensor components to support
|
|
|
|
devices discovered on the network.
|
|
|
|
"""
|
|
|
|
global NETWORK
|
|
|
|
|
|
|
|
if 'ecobee' in _CONFIGURING:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Create ecobee.conf if it doesn't exist
|
|
|
|
if not os.path.isfile(hass.config.path(ECOBEE_CONFIG_FILE)):
|
|
|
|
jsonconfig = {"API_KEY": config[DOMAIN].get(CONF_API_KEY)}
|
2017-11-20 03:47:55 +00:00
|
|
|
save_json(hass.config.path(ECOBEE_CONFIG_FILE), jsonconfig)
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2015-11-21 17:24:06 +00:00
|
|
|
NETWORK = EcobeeData(hass.config.path(ECOBEE_CONFIG_FILE))
|
2015-11-20 22:47:25 +00:00
|
|
|
|
2015-11-23 16:15:19 +00:00
|
|
|
setup_ecobee(hass, NETWORK.ecobee, config)
|
2015-11-20 22:47:25 +00:00
|
|
|
|
|
|
|
return True
|