core/homeassistant/components/ecobee/__init__.py

124 lines
3.5 KiB
Python
Raw Normal View History

"""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
import voluptuous as vol
2015-11-29 21:49:05 +00:00
import homeassistant.helpers.config_validation as cv
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
from homeassistant.util.json import save_json
_CONFIGURING = {}
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_HOLD_TEMP = "hold_temp"
2019-07-31 19:25:30 +00:00
DOMAIN = "ecobee"
2019-07-31 19:25:30 +00:00
ECOBEE_CONFIG_FILE = "ecobee.conf"
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=180)
NETWORK = None
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(CONF_API_KEY): cv.string,
vol.Optional(CONF_HOLD_TEMP, default=False): cv.boolean,
}
)
},
extra=vol.ALLOW_EXTRA,
)
def request_configuration(network, hass, config):
2016-03-07 17:49:31 +00:00
"""Request configuration steps from the user."""
configurator = hass.components.configurator
2019-07-31 19:25:30 +00:00
if "ecobee" in _CONFIGURING:
configurator.notify_errors(
2019-07-31 19:25:30 +00:00
_CONFIGURING["ecobee"], "Failed to register, please try again."
)
return
def ecobee_configuration_callback(callback_data):
"""Handle configuration callbacks."""
network.request_tokens()
network.update()
setup_ecobee(hass, network, config)
2019-07-31 19:25:30 +00:00
_CONFIGURING["ecobee"] = configurator.request_config(
"Ecobee",
ecobee_configuration_callback,
description=(
2019-07-31 19:25:30 +00:00
"Please authorize this app at https://www.ecobee.com/consumer"
"portal/index.html with pin code: " + network.pin
),
description_image="/static/images/config_ecobee_thermostat.png",
2019-07-31 19:25:30 +00:00
submit_caption="I have authorized the app.",
)
def setup_ecobee(hass, network, config):
"""Set up the Ecobee thermostat."""
# If ecobee has a PIN then it needs to be configured.
if network.pin is not None:
request_configuration(network, hass, config)
return
2019-07-31 19:25:30 +00:00
if "ecobee" in _CONFIGURING:
configurator = hass.components.configurator
2019-07-31 19:25:30 +00:00
configurator.request_done(_CONFIGURING.pop("ecobee"))
hold_temp = config[DOMAIN].get(CONF_HOLD_TEMP)
2019-07-31 19:25:30 +00:00
discovery.load_platform(hass, "climate", DOMAIN, {"hold_temp": hold_temp}, config)
discovery.load_platform(hass, "sensor", DOMAIN, {}, config)
discovery.load_platform(hass, "binary_sensor", DOMAIN, {}, config)
discovery.load_platform(hass, "weather", DOMAIN, {}, config)
class EcobeeData:
2016-03-08 16:55:57 +00:00
"""Get the latest data and update the states."""
def __init__(self, config_file):
"""Init the Ecobee data object."""
from pyecobee import Ecobee
2019-07-31 19:25:30 +00:00
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."""
self.ecobee.update()
_LOGGER.debug("Ecobee data updated successfully")
def setup(hass, config):
"""Set up the Ecobee.
2016-03-08 16:55:57 +00:00
Will automatically load thermostat and sensor components to support
devices discovered on the network.
"""
global NETWORK
2019-07-31 19:25:30 +00:00
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)}
save_json(hass.config.path(ECOBEE_CONFIG_FILE), jsonconfig)
NETWORK = EcobeeData(hass.config.path(ECOBEE_CONFIG_FILE))
setup_ecobee(hass, NETWORK.ecobee, config)
return True