2017-01-17 07:15:11 +00:00
|
|
|
"""
|
|
|
|
Support for Tado Smart Thermostat.
|
|
|
|
|
2017-01-28 15:02:19 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/device_tracker.tado/
|
2017-01-17 07:15:11 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import async_timeout
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-04-24 03:41:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-01-17 07:15:11 +00:00
|
|
|
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
|
|
|
|
from homeassistant.util import Throttle
|
2017-01-28 15:02:19 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
|
|
|
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
2017-01-17 07:15:11 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-04-24 03:41:09 +00:00
|
|
|
CONF_HOME_ID = 'home_id'
|
|
|
|
|
2017-01-28 15:02:19 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=30)
|
|
|
|
|
2017-01-17 07:15:11 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2017-02-10 02:50:14 +00:00
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
2017-01-17 07:15:11 +00:00
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
2017-02-10 02:50:14 +00:00
|
|
|
vol.Optional(CONF_HOME_ID): cv.string
|
2017-01-17 07:15:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
|
|
|
"""Return a Tado scanner."""
|
|
|
|
scanner = TadoDeviceScanner(hass, config[DOMAIN])
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
|
|
|
Device = namedtuple("Device", ["mac", "name"])
|
|
|
|
|
|
|
|
|
|
|
|
class TadoDeviceScanner(DeviceScanner):
|
|
|
|
"""This class gets geofenced devices from Tado."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config):
|
|
|
|
"""Initialize the scanner."""
|
|
|
|
self.last_results = []
|
|
|
|
|
|
|
|
self.username = config[CONF_USERNAME]
|
|
|
|
self.password = config[CONF_PASSWORD]
|
2017-02-10 02:50:14 +00:00
|
|
|
|
|
|
|
# The Tado device tracker can work with or without a home_id
|
|
|
|
self.home_id = config[CONF_HOME_ID] if CONF_HOME_ID in config else None
|
|
|
|
|
|
|
|
# If there's a home_id, we need a different API URL
|
|
|
|
if self.home_id is None:
|
|
|
|
self.tadoapiurl = 'https://my.tado.com/api/v2/me'
|
|
|
|
else:
|
|
|
|
self.tadoapiurl = 'https://my.tado.com/api/v2' \
|
|
|
|
'/homes/{home_id}/mobileDevices'
|
|
|
|
|
|
|
|
# The API URL always needs a username and password
|
|
|
|
self.tadoapiurl += '?username={username}&password={password}'
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
self.websession = async_create_clientsession(
|
|
|
|
hass, cookie_jar=aiohttp.CookieJar(unsafe=True, loop=hass.loop))
|
|
|
|
|
2018-03-11 19:33:07 +00:00
|
|
|
self.success_init = asyncio.run_coroutine_threadsafe(
|
|
|
|
self._async_update_info(), hass.loop
|
|
|
|
).result()
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.info("Scanner initialized")
|
2017-01-17 07:15:11 +00:00
|
|
|
|
2018-03-11 19:33:07 +00:00
|
|
|
async def async_scan_devices(self):
|
2017-01-17 07:15:11 +00:00
|
|
|
"""Scan for devices and return a list containing found device ids."""
|
2018-03-11 19:33:07 +00:00
|
|
|
await self._async_update_info()
|
2017-01-17 07:15:11 +00:00
|
|
|
return [device.mac for device in self.last_results]
|
|
|
|
|
2018-03-11 19:33:07 +00:00
|
|
|
async def async_get_device_name(self, device):
|
2017-01-17 07:15:11 +00:00
|
|
|
"""Return the name of the given device or None if we don't know."""
|
2018-02-11 17:20:28 +00:00
|
|
|
filter_named = [result.name for result in self.last_results
|
|
|
|
if result.mac == device]
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
if filter_named:
|
|
|
|
return filter_named[0]
|
2017-07-06 06:30:01 +00:00
|
|
|
return None
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_SCANS)
|
2018-03-11 19:33:07 +00:00
|
|
|
async def _async_update_info(self):
|
2017-01-17 07:15:11 +00:00
|
|
|
"""
|
|
|
|
Query Tado for device marked as at home.
|
|
|
|
|
|
|
|
Returns boolean if scanning successful.
|
|
|
|
"""
|
|
|
|
_LOGGER.debug("Requesting Tado")
|
|
|
|
|
|
|
|
last_results = []
|
2017-02-10 02:50:14 +00:00
|
|
|
|
2017-01-17 07:15:11 +00:00
|
|
|
try:
|
2018-03-15 20:53:59 +00:00
|
|
|
with async_timeout.timeout(10):
|
2017-02-10 02:50:14 +00:00
|
|
|
# Format the URL here, so we can log the template URL if
|
|
|
|
# anything goes wrong without exposing username and password.
|
2017-04-30 05:04:49 +00:00
|
|
|
url = self.tadoapiurl.format(
|
|
|
|
home_id=self.home_id, username=self.username,
|
|
|
|
password=self.password)
|
2017-02-10 02:50:14 +00:00
|
|
|
|
2018-03-11 19:33:07 +00:00
|
|
|
response = await self.websession.get(url)
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
if response.status != 200:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Error %d on %s.", response.status, self.tadoapiurl)
|
2018-03-11 19:33:07 +00:00
|
|
|
return False
|
2017-01-17 07:15:11 +00:00
|
|
|
|
2018-03-11 19:33:07 +00:00
|
|
|
tado_json = await response.json()
|
2017-01-17 07:15:11 +00:00
|
|
|
|
2017-03-30 07:50:53 +00:00
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
2017-02-10 02:50:14 +00:00
|
|
|
_LOGGER.error("Cannot load Tado data")
|
2017-01-17 07:15:11 +00:00
|
|
|
return False
|
|
|
|
|
2017-02-10 02:50:14 +00:00
|
|
|
# Without a home_id, we fetched an URL where the mobile devices can be
|
|
|
|
# found under the mobileDevices key.
|
|
|
|
if 'mobileDevices' in tado_json:
|
|
|
|
tado_json = tado_json['mobileDevices']
|
|
|
|
|
|
|
|
# Find devices that have geofencing enabled, and are currently at home.
|
|
|
|
for mobile_device in tado_json:
|
2017-03-05 20:38:14 +00:00
|
|
|
if mobile_device.get('location'):
|
2017-02-10 02:50:14 +00:00
|
|
|
if mobile_device['location']['atHome']:
|
|
|
|
device_id = mobile_device['id']
|
|
|
|
device_name = mobile_device['name']
|
|
|
|
last_results.append(Device(device_id, device_name))
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
self.last_results = last_results
|
|
|
|
|
2018-03-11 19:33:07 +00:00
|
|
|
_LOGGER.debug(
|
2017-02-10 02:50:14 +00:00
|
|
|
"Tado presence query successful, %d device(s) at home",
|
|
|
|
len(self.last_results)
|
|
|
|
)
|
|
|
|
|
2017-01-17 07:15:11 +00:00
|
|
|
return True
|