2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Tado Smart device trackers."""
|
2019-11-24 21:47:31 +00:00
|
|
|
import asyncio
|
2017-01-17 07:15:11 +00:00
|
|
|
from collections import namedtuple
|
2019-11-24 21:47:31 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
import async_timeout
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-01-28 15:02:19 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DeviceScanner,
|
|
|
|
)
|
2020-04-08 16:47:38 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, HTTP_OK
|
2017-01-17 07:15:11 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
2019-11-24 21:47:31 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.util import Throttle
|
2017-01-17 07:15:11 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOME_ID = "home_id"
|
2017-04-24 03:41:09 +00:00
|
|
|
|
2017-01-28 15:02:19 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=30)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
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."""
|
2019-06-06 10:07:30 +00:00
|
|
|
self.hass = hass
|
2017-01-17 07:15:11 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.tadoapiurl = "https://my.tado.com/api/v2/me"
|
2017-02-10 02:50:14 +00:00
|
|
|
else:
|
2020-01-02 19:17:10 +00:00
|
|
|
self.tadoapiurl = "https://my.tado.com/api/v2/homes/{home_id}/mobileDevices"
|
2017-02-10 02:50:14 +00:00
|
|
|
|
|
|
|
# The API URL always needs a username and password
|
2019-07-31 19:25:30 +00:00
|
|
|
self.tadoapiurl += "?username={username}&password={password}"
|
2017-01-17 07:15:11 +00:00
|
|
|
|
2019-06-06 10:07:30 +00:00
|
|
|
self.websession = None
|
2017-01-17 07:15:11 +00:00
|
|
|
|
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."""
|
2019-07-31 19:25:30 +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")
|
|
|
|
|
2019-06-06 10:07:30 +00:00
|
|
|
if self.websession is None:
|
|
|
|
self.websession = async_create_clientsession(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, cookie_jar=aiohttp.CookieJar(unsafe=True)
|
|
|
|
)
|
2019-06-06 10:07:30 +00:00
|
|
|
|
2017-01-17 07:15:11 +00:00
|
|
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
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
|
|
|
|
2020-04-08 16:47:38 +00:00
|
|
|
if response.status != HTTP_OK:
|
2020-07-05 21:04:19 +00:00
|
|
|
_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.
|
2019-07-31 19:25:30 +00:00
|
|
|
if "mobileDevices" in tado_json:
|
|
|
|
tado_json = tado_json["mobileDevices"]
|
2017-02-10 02:50:14 +00:00
|
|
|
|
|
|
|
# Find devices that have geofencing enabled, and are currently at home.
|
|
|
|
for mobile_device in tado_json:
|
2019-07-31 19:25:30 +00:00
|
|
|
if mobile_device.get("location"):
|
|
|
|
if mobile_device["location"]["atHome"]:
|
|
|
|
device_id = mobile_device["id"]
|
|
|
|
device_name = mobile_device["name"]
|
2017-02-10 02:50:14 +00:00
|
|
|
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",
|
2019-07-31 19:25:30 +00:00
|
|
|
len(self.last_results),
|
2017-02-10 02:50:14 +00:00
|
|
|
)
|
|
|
|
|
2017-01-17 07:15:11 +00:00
|
|
|
return True
|