mirror of
https://github.com/USA-RedDragon/badnest.git
synced 2025-01-18 18:20:44 +00:00
Merge pull request #32 from USA-RedDragon/rewrite-api
Rewrite API to be a singleton
This commit is contained in:
commit
c9693df3a2
@ -3,7 +3,8 @@ import voluptuous as vol
|
|||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||||
|
|
||||||
from .const import DOMAIN, CONF_ISSUE_TOKEN, CONF_COOKIE
|
from .api import NestAPI
|
||||||
|
from .const import DOMAIN, CONF_ISSUE_TOKEN, CONF_COOKIE, CONF_REGION
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
@ -11,10 +12,12 @@ CONFIG_SCHEMA = vol.Schema(
|
|||||||
{
|
{
|
||||||
vol.Required(CONF_EMAIL, default=""): cv.string,
|
vol.Required(CONF_EMAIL, default=""): cv.string,
|
||||||
vol.Required(CONF_PASSWORD, default=""): cv.string,
|
vol.Required(CONF_PASSWORD, default=""): cv.string,
|
||||||
|
vol.Optional(CONF_REGION, default="us"): cv.string,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
vol.Required(CONF_ISSUE_TOKEN, default=""): cv.string,
|
vol.Required(CONF_ISSUE_TOKEN, default=""): cv.string,
|
||||||
vol.Required(CONF_COOKIE, default=""): cv.string,
|
vol.Required(CONF_COOKIE, default=""): cv.string,
|
||||||
|
vol.Optional(CONF_REGION, default="us"): cv.string,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@ -29,17 +32,22 @@ def setup(hass, config):
|
|||||||
password = config[DOMAIN].get(CONF_PASSWORD)
|
password = config[DOMAIN].get(CONF_PASSWORD)
|
||||||
issue_token = config[DOMAIN].get(CONF_ISSUE_TOKEN)
|
issue_token = config[DOMAIN].get(CONF_ISSUE_TOKEN)
|
||||||
cookie = config[DOMAIN].get(CONF_COOKIE)
|
cookie = config[DOMAIN].get(CONF_COOKIE)
|
||||||
|
region = config[DOMAIN].get(CONF_REGION)
|
||||||
else:
|
else:
|
||||||
email = None
|
email = None
|
||||||
password = None
|
password = None
|
||||||
issue_token = None
|
issue_token = None
|
||||||
cookie = None
|
cookie = None
|
||||||
|
region = None
|
||||||
|
|
||||||
hass.data[DOMAIN] = {
|
hass.data[DOMAIN] = {
|
||||||
CONF_EMAIL: email,
|
'api': NestAPI(
|
||||||
CONF_PASSWORD: password,
|
email,
|
||||||
CONF_ISSUE_TOKEN: issue_token,
|
password,
|
||||||
CONF_COOKIE: cookie,
|
issue_token,
|
||||||
|
cookie,
|
||||||
|
region,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import requests
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
API_URL = "https://home.nest.com"
|
API_URL = "https://home.nest.com"
|
||||||
CAMERA_WEBAPI_BASE = "https://webapi.camera.home.nest.com"
|
CAMERA_WEBAPI_BASE = "https://webapi.camera.home.nest.com"
|
||||||
CAMERA_US_URL = "https://nexusapi-us1.camera.home.nest.com"
|
|
||||||
CAMERA_EU_URL = "https://nexusapi-eu1.camera.home.nest.com"
|
|
||||||
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) " \
|
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) " \
|
||||||
"AppleWebKit/537.36 (KHTML, like Gecko) " \
|
"AppleWebKit/537.36 (KHTML, like Gecko) " \
|
||||||
"Chrome/75.0.3770.100 Safari/537.36"
|
"Chrome/75.0.3770.100 Safari/537.36"
|
||||||
@ -13,32 +12,64 @@ URL_JWT = "https://nestauthproxyservice-pa.googleapis.com/v1/issue_jwt"
|
|||||||
# Nest website's (public) API key
|
# Nest website's (public) API key
|
||||||
NEST_API_KEY = "AIzaSyAdkSIMNc51XGNEAYWasX9UOWkS5P6sZE4"
|
NEST_API_KEY = "AIzaSyAdkSIMNc51XGNEAYWasX9UOWkS5P6sZE4"
|
||||||
|
|
||||||
|
KNOWN_BUCKET_TYPES = [
|
||||||
|
# Thermostats
|
||||||
|
"device",
|
||||||
|
"shared",
|
||||||
|
# Temperature sensors
|
||||||
|
"kryptonite",
|
||||||
|
]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class NestAPI:
|
class NestAPI():
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
issue_token,
|
issue_token,
|
||||||
cookie,
|
cookie,
|
||||||
device_id=None):
|
region):
|
||||||
|
self.device_data = {}
|
||||||
|
self._wheres = {}
|
||||||
self._user_id = None
|
self._user_id = None
|
||||||
self._access_token = None
|
self._access_token = None
|
||||||
self._session = requests.Session()
|
self._session = requests.Session()
|
||||||
self._session.headers.update({"Referer": "https://home.nest.com/"})
|
self._session.headers.update({
|
||||||
self._device_id = device_id
|
"Referer": "https://home.nest.com/",
|
||||||
|
"User-Agent": USER_AGENT,
|
||||||
|
})
|
||||||
self._email = email
|
self._email = email
|
||||||
self._password = password
|
self._password = password
|
||||||
self._issue_token = issue_token
|
self._issue_token = issue_token
|
||||||
self._cookie = cookie
|
self._cookie = cookie
|
||||||
|
self._czfe_url = None
|
||||||
|
self._camera_url = f'https://nexusapi-{region}1.camera.home.nest.com'
|
||||||
|
self.cameras = []
|
||||||
|
self.thermostats = []
|
||||||
|
self.temperature_sensors = []
|
||||||
self.login()
|
self.login()
|
||||||
|
self._get_devices()
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
def __getitem__(self, name):
|
||||||
|
return getattr(self, name)
|
||||||
|
|
||||||
|
def __setitem__(self, name, value):
|
||||||
|
return setattr(self, name, value)
|
||||||
|
|
||||||
|
def __delitem__(self, name):
|
||||||
|
return delattr(self, name)
|
||||||
|
|
||||||
|
def __contains__(self, name):
|
||||||
|
return hasattr(self, name)
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
if not self._email and not self._password:
|
if not self._email and not self._password:
|
||||||
self._login_google(self._issue_token, self._cookie)
|
self._login_google(self._issue_token, self._cookie)
|
||||||
else:
|
else:
|
||||||
self._login_nest(self._email, self._password)
|
self._login_nest(self._email, self._password)
|
||||||
|
self._login_dropcam()
|
||||||
|
|
||||||
def _login_nest(self, email, password):
|
def _login_nest(self, email, password):
|
||||||
r = self._session.post(
|
r = self._session.post(
|
||||||
@ -49,18 +80,18 @@ class NestAPI:
|
|||||||
|
|
||||||
def _login_google(self, issue_token, cookie):
|
def _login_google(self, issue_token, cookie):
|
||||||
headers = {
|
headers = {
|
||||||
'Sec-Fetch-Mode': 'cors',
|
|
||||||
'User-Agent': USER_AGENT,
|
'User-Agent': USER_AGENT,
|
||||||
|
'Sec-Fetch-Mode': 'cors',
|
||||||
'X-Requested-With': 'XmlHttpRequest',
|
'X-Requested-With': 'XmlHttpRequest',
|
||||||
'Referer': 'https://accounts.google.com/o/oauth2/iframe',
|
'Referer': 'https://accounts.google.com/o/oauth2/iframe',
|
||||||
'cookie': cookie
|
'cookie': cookie
|
||||||
}
|
}
|
||||||
r = requests.get(url=issue_token, headers=headers)
|
r = self._session.get(url=issue_token, headers=headers)
|
||||||
access_token = r.json()['access_token']
|
access_token = r.json()['access_token']
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'Authorization': 'Bearer ' + access_token,
|
|
||||||
'User-Agent': USER_AGENT,
|
'User-Agent': USER_AGENT,
|
||||||
|
'Authorization': 'Bearer ' + access_token,
|
||||||
'x-goog-api-key': NEST_API_KEY,
|
'x-goog-api-key': NEST_API_KEY,
|
||||||
'Referer': 'https://home.nest.com'
|
'Referer': 'https://home.nest.com'
|
||||||
}
|
}
|
||||||
@ -70,44 +101,40 @@ class NestAPI:
|
|||||||
"google_oauth_access_token": access_token,
|
"google_oauth_access_token": access_token,
|
||||||
"policy_id": 'authproxy-oauth-policy'
|
"policy_id": 'authproxy-oauth-policy'
|
||||||
}
|
}
|
||||||
r = requests.post(url=URL_JWT, headers=headers, params=params)
|
r = self._session.post(url=URL_JWT, headers=headers, params=params)
|
||||||
self._user_id = r.json()['claims']['subject']['nestId']['id']
|
self._user_id = r.json()['claims']['subject']['nestId']['id']
|
||||||
self._access_token = r.json()['jwt']
|
self._access_token = r.json()['jwt']
|
||||||
|
|
||||||
|
def _login_dropcam(self):
|
||||||
|
self._session.post(
|
||||||
|
f"{API_URL}/dropcam/api/login",
|
||||||
|
data={"access_token": self._access_token}
|
||||||
|
)
|
||||||
|
|
||||||
class NestThermostatAPI(NestAPI):
|
def _get_cameras(self):
|
||||||
def __init__(self,
|
cameras = []
|
||||||
email,
|
|
||||||
password,
|
|
||||||
issue_token,
|
|
||||||
cookie,
|
|
||||||
device_id=None):
|
|
||||||
super(NestThermostatAPI, self).__init__(
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
issue_token,
|
|
||||||
cookie,
|
|
||||||
device_id)
|
|
||||||
self._czfe_url = None
|
|
||||||
self._compressor_lockout_enabled = None
|
|
||||||
self._compressor_lockout_time = None
|
|
||||||
self._hvac_ac_state = None
|
|
||||||
self._hvac_heater_state = None
|
|
||||||
self.mode = None
|
|
||||||
self._time_to_target = None
|
|
||||||
self._fan_timer_timeout = None
|
|
||||||
self.can_heat = None
|
|
||||||
self.can_cool = None
|
|
||||||
self.has_fan = None
|
|
||||||
self.fan = None
|
|
||||||
self.current_temperature = None
|
|
||||||
self.target_temperature = None
|
|
||||||
self.target_temperature_high = None
|
|
||||||
self.target_temperature_low = None
|
|
||||||
self.current_humidity = None
|
|
||||||
self.update()
|
|
||||||
|
|
||||||
def get_devices(self):
|
try:
|
||||||
|
r = self._session.get(
|
||||||
|
f"{CAMERA_WEBAPI_BASE}/api/cameras."
|
||||||
|
+ "get_owned_and_member_of_with_properties"
|
||||||
|
)
|
||||||
|
|
||||||
|
for camera in r.json()["items"]:
|
||||||
|
cameras.append(camera['uuid'])
|
||||||
|
self.device_data[camera['uuid']] = {}
|
||||||
|
|
||||||
|
return cameras
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
_LOGGER.error(e)
|
||||||
|
_LOGGER.error('Failed to get cameras, trying again')
|
||||||
|
return self._get_cameras()
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to get cameras, trying to log in again')
|
||||||
|
self.login()
|
||||||
|
return self._get_cameras()
|
||||||
|
|
||||||
|
def _get_devices(self):
|
||||||
try:
|
try:
|
||||||
r = self._session.post(
|
r = self._session.post(
|
||||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||||
@ -117,206 +144,22 @@ class NestThermostatAPI(NestAPI):
|
|||||||
},
|
},
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
)
|
)
|
||||||
devices = []
|
|
||||||
buckets = r.json()['updated_buckets'][0]['value']['buckets']
|
|
||||||
for bucket in buckets:
|
|
||||||
if bucket.startswith('device.'):
|
|
||||||
devices.append(bucket.replace('device.', ''))
|
|
||||||
|
|
||||||
return devices
|
|
||||||
except requests.exceptions.RequestException as e:
|
|
||||||
_LOGGER.error(e)
|
|
||||||
_LOGGER.error('Failed to get devices, trying again')
|
|
||||||
return self.get_devices()
|
|
||||||
except KeyError:
|
|
||||||
_LOGGER.debug('Failed to get devices, trying to log in again')
|
|
||||||
self.login()
|
|
||||||
return self.get_devices()
|
|
||||||
|
|
||||||
def get_action(self):
|
|
||||||
if self._hvac_ac_state:
|
|
||||||
return "cooling"
|
|
||||||
elif self._hvac_heater_state:
|
|
||||||
return "heating"
|
|
||||||
else:
|
|
||||||
return "off"
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
try:
|
|
||||||
r = self._session.post(
|
|
||||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
|
||||||
json={
|
|
||||||
"known_bucket_types": ["shared", "device"],
|
|
||||||
"known_bucket_versions": [],
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
self._czfe_url = r.json()["service_urls"]["urls"]["czfe_url"]
|
self._czfe_url = r.json()["service_urls"]["urls"]["czfe_url"]
|
||||||
|
|
||||||
temp_mode = None
|
|
||||||
for bucket in r.json()["updated_buckets"]:
|
|
||||||
if bucket["object_key"] \
|
|
||||||
.startswith(f"shared.{self._device_id}"):
|
|
||||||
thermostat_data = bucket["value"]
|
|
||||||
self.current_temperature = \
|
|
||||||
thermostat_data["current_temperature"]
|
|
||||||
self.target_temperature = \
|
|
||||||
thermostat_data["target_temperature"]
|
|
||||||
self._compressor_lockout_enabled = thermostat_data[
|
|
||||||
"compressor_lockout_enabled"
|
|
||||||
]
|
|
||||||
self._compressor_lockout_time = thermostat_data[
|
|
||||||
"compressor_lockout_timeout"
|
|
||||||
]
|
|
||||||
self._hvac_ac_state = thermostat_data["hvac_ac_state"]
|
|
||||||
self._hvac_heater_state = \
|
|
||||||
thermostat_data["hvac_heater_state"]
|
|
||||||
if temp_mode is None:
|
|
||||||
temp_mode = thermostat_data["target_temperature_type"]
|
|
||||||
self.target_temperature_high = thermostat_data[
|
|
||||||
"target_temperature_high"
|
|
||||||
]
|
|
||||||
self.target_temperature_low = \
|
|
||||||
thermostat_data["target_temperature_low"]
|
|
||||||
self.can_heat = thermostat_data["can_heat"]
|
|
||||||
self.can_cool = thermostat_data["can_cool"]
|
|
||||||
elif bucket["object_key"] \
|
|
||||||
.startswith(f"device.{self._device_id}"):
|
|
||||||
thermostat_data = bucket["value"]
|
|
||||||
self._time_to_target = thermostat_data["time_to_target"]
|
|
||||||
self._fan_timer_timeout = \
|
|
||||||
thermostat_data["fan_timer_timeout"]
|
|
||||||
self.has_fan = thermostat_data["has_fan"]
|
|
||||||
self.fan = thermostat_data["fan_timer_timeout"] > 0
|
|
||||||
self.current_humidity = thermostat_data["current_humidity"]
|
|
||||||
if thermostat_data["eco"]["mode"] == 'manual-eco' or \
|
|
||||||
thermostat_data["eco"]["mode"] == 'auto-eco':
|
|
||||||
temp_mode = 'eco'
|
|
||||||
self.mode = temp_mode
|
|
||||||
except requests.exceptions.RequestException as e:
|
|
||||||
_LOGGER.error(e)
|
|
||||||
_LOGGER.error('Failed to update, trying again')
|
|
||||||
self.update()
|
|
||||||
except KeyError:
|
|
||||||
_LOGGER.debug('Failed to update, trying to log in again')
|
|
||||||
self.login()
|
|
||||||
self.update()
|
|
||||||
|
|
||||||
def set_temp(self, temp, temp_high=None):
|
|
||||||
if temp_high is None:
|
|
||||||
self._session.post(
|
|
||||||
f"{self._czfe_url}/v5/put",
|
|
||||||
json={
|
|
||||||
"objects": [
|
|
||||||
{
|
|
||||||
"object_key": f'shared.{self._device_id}',
|
|
||||||
"op": "MERGE",
|
|
||||||
"value": {"target_temperature": temp},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self._session.post(
|
|
||||||
f"{self._czfe_url}/v5/put",
|
|
||||||
json={
|
|
||||||
"objects": [
|
|
||||||
{
|
|
||||||
"object_key": f'shared.{self._device_id}',
|
|
||||||
"op": "MERGE",
|
|
||||||
"value": {
|
|
||||||
"target_temperature_low": temp,
|
|
||||||
"target_temperature_high": temp_high,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
def set_mode(self, mode):
|
|
||||||
self._session.post(
|
|
||||||
f"{self._czfe_url}/v5/put",
|
|
||||||
json={
|
|
||||||
"objects": [
|
|
||||||
{
|
|
||||||
"object_key": f'shared.{self._device_id}',
|
|
||||||
"op": "MERGE",
|
|
||||||
"value": {"target_temperature_type": mode},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
def set_fan(self, date):
|
|
||||||
self._session.post(
|
|
||||||
f"{self._czfe_url}/v5/put",
|
|
||||||
json={
|
|
||||||
"objects": [
|
|
||||||
{
|
|
||||||
"object_key": f'device.{self._device_id}',
|
|
||||||
"op": "MERGE",
|
|
||||||
"value": {"fan_timer_timeout": date},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
def set_eco_mode(self, state):
|
|
||||||
mode = 'manual-eco' if state else 'schedule'
|
|
||||||
self._session.post(
|
|
||||||
f"{self._czfe_url}/v5/put",
|
|
||||||
json={
|
|
||||||
"objects": [
|
|
||||||
{
|
|
||||||
"object_key": f'device.{self._device_id}',
|
|
||||||
"op": "MERGE",
|
|
||||||
"value": {"eco": {"mode": mode}},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class NestTemperatureSensorAPI(NestAPI):
|
|
||||||
def __init__(self,
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
issue_token,
|
|
||||||
cookie,
|
|
||||||
device_id=None):
|
|
||||||
super(NestTemperatureSensorAPI, self).__init__(
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
issue_token,
|
|
||||||
cookie,
|
|
||||||
device_id)
|
|
||||||
self.temperature = None
|
|
||||||
self._device_id = device_id
|
|
||||||
self.update()
|
|
||||||
|
|
||||||
def get_devices(self):
|
|
||||||
try:
|
|
||||||
r = self._session.post(
|
|
||||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
|
||||||
json={
|
|
||||||
"known_bucket_types": ["buckets"],
|
|
||||||
"known_bucket_versions": [],
|
|
||||||
},
|
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
|
||||||
)
|
|
||||||
devices = []
|
|
||||||
buckets = r.json()['updated_buckets'][0]['value']['buckets']
|
buckets = r.json()['updated_buckets'][0]['value']['buckets']
|
||||||
for bucket in buckets:
|
for bucket in buckets:
|
||||||
if bucket.startswith('kryptonite.'):
|
if bucket.startswith('kryptonite.'):
|
||||||
devices.append(bucket.replace('kryptonite.', ''))
|
sn = bucket.replace('kryptonite.', '')
|
||||||
|
self.temperature_sensors.append(sn)
|
||||||
|
self.device_data[sn] = {}
|
||||||
|
elif bucket.startswith('device.'):
|
||||||
|
sn = bucket.replace('device.', '')
|
||||||
|
self.thermostats.append(sn)
|
||||||
|
self.device_data[sn] = {}
|
||||||
|
|
||||||
|
self.cameras = self._get_cameras()
|
||||||
|
|
||||||
return devices
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
_LOGGER.error(e)
|
_LOGGER.error(e)
|
||||||
_LOGGER.error('Failed to get devices, trying again')
|
_LOGGER.error('Failed to get devices, trying again')
|
||||||
@ -328,21 +171,120 @@ class NestTemperatureSensorAPI(NestAPI):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
try:
|
try:
|
||||||
|
# To get friendly names
|
||||||
r = self._session.post(
|
r = self._session.post(
|
||||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||||
json={
|
json={
|
||||||
"known_bucket_types": ["kryptonite"],
|
"known_bucket_types": ["where"],
|
||||||
"known_bucket_versions": [],
|
"known_bucket_versions": [],
|
||||||
},
|
},
|
||||||
headers={"Authorization": f"Basic {self._access_token}"},
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
)
|
)
|
||||||
|
|
||||||
for bucket in r.json()["updated_buckets"]:
|
for bucket in r.json()["updated_buckets"]:
|
||||||
|
sensor_data = bucket["value"]
|
||||||
|
sn = bucket["object_key"].split('.')[1]
|
||||||
if bucket["object_key"].startswith(
|
if bucket["object_key"].startswith(
|
||||||
f"kryptonite.{self._device_id}"):
|
f"where.{sn}"):
|
||||||
sensor_data = bucket["value"]
|
wheres = sensor_data['wheres']
|
||||||
self.temperature = sensor_data["current_temperature"]
|
for where in wheres:
|
||||||
self.battery_level = sensor_data["battery_level"]
|
self._wheres[where['where_id']] = where['name']
|
||||||
|
|
||||||
|
r = self._session.post(
|
||||||
|
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||||
|
json={
|
||||||
|
"known_bucket_types": KNOWN_BUCKET_TYPES,
|
||||||
|
"known_bucket_versions": [],
|
||||||
|
},
|
||||||
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
|
)
|
||||||
|
|
||||||
|
for bucket in r.json()["updated_buckets"]:
|
||||||
|
sensor_data = bucket["value"]
|
||||||
|
sn = bucket["object_key"].split('.')[1]
|
||||||
|
# Thermostats
|
||||||
|
if bucket["object_key"].startswith(
|
||||||
|
f"shared.{sn}"):
|
||||||
|
self.device_data[sn]['current_temperature'] = \
|
||||||
|
sensor_data["current_temperature"]
|
||||||
|
self.device_data[sn]['target_temperature'] = \
|
||||||
|
sensor_data["target_temperature"]
|
||||||
|
self.device_data[sn]['hvac_ac_state'] = \
|
||||||
|
sensor_data["hvac_ac_state"]
|
||||||
|
self.device_data[sn]['hvac_heater_state'] = \
|
||||||
|
sensor_data["hvac_heater_state"]
|
||||||
|
self.device_data[sn]['target_temperature_high'] = \
|
||||||
|
sensor_data["target_temperature_high"]
|
||||||
|
self.device_data[sn]['target_temperature_low'] = \
|
||||||
|
sensor_data["target_temperature_low"]
|
||||||
|
self.device_data[sn]['can_heat'] = \
|
||||||
|
sensor_data["can_heat"]
|
||||||
|
self.device_data[sn]['can_cool'] = \
|
||||||
|
sensor_data["can_cool"]
|
||||||
|
self.device_data[sn]['mode'] = \
|
||||||
|
sensor_data["target_temperature_type"]
|
||||||
|
if self.device_data[sn]['hvac_ac_state']:
|
||||||
|
self.device_data[sn]['action'] = "cooling"
|
||||||
|
elif self.device_data[sn]['hvac_heater_state']:
|
||||||
|
self.device_data[sn]['action'] = "heating"
|
||||||
|
else:
|
||||||
|
self.device_data[sn]['action'] = "off"
|
||||||
|
# Thermostats, pt 2
|
||||||
|
elif bucket["object_key"].startswith(
|
||||||
|
f"device.{sn}"):
|
||||||
|
self.device_data[sn]['name'] = self._wheres[
|
||||||
|
sensor_data['where_id']
|
||||||
|
]
|
||||||
|
if sensor_data.get('description', None):
|
||||||
|
self.device_data[sn]['name'] += \
|
||||||
|
f' ({sensor_data["description"]})'
|
||||||
|
self.device_data[sn]['name'] += ' Thermostat'
|
||||||
|
self.device_data[sn]['has_fan'] = \
|
||||||
|
sensor_data["has_fan"]
|
||||||
|
self.device_data[sn]['fan'] = \
|
||||||
|
sensor_data["fan_timer_timeout"]
|
||||||
|
self.device_data[sn]['current_humidity'] = \
|
||||||
|
sensor_data["current_humidity"]
|
||||||
|
if sensor_data["eco"]["mode"] == 'manual-eco' or \
|
||||||
|
sensor_data["eco"]["mode"] == 'auto-eco':
|
||||||
|
self.device_data[sn]['eco'] = True
|
||||||
|
else:
|
||||||
|
self.device_data[sn]['eco'] = False
|
||||||
|
# Temperature sensors
|
||||||
|
elif bucket["object_key"].startswith(
|
||||||
|
f"kryptonite.{sn}"):
|
||||||
|
self.device_data[sn]['name'] = self._wheres[
|
||||||
|
sensor_data['where_id']
|
||||||
|
]
|
||||||
|
if sensor_data.get('description', None):
|
||||||
|
self.device_data[sn]['name'] += \
|
||||||
|
f' ({sensor_data["description"]})'
|
||||||
|
self.device_data[sn]['name'] += ' Temperature'
|
||||||
|
self.device_data[sn]['temperature'] = \
|
||||||
|
sensor_data['current_temperature']
|
||||||
|
self.device_data[sn]['battery_level'] = \
|
||||||
|
sensor_data['battery_level']
|
||||||
|
|
||||||
|
# Cameras
|
||||||
|
for camera in self.cameras:
|
||||||
|
r = self._session.get(
|
||||||
|
f"{API_URL}/dropcam/api/cameras/{camera}"
|
||||||
|
)
|
||||||
|
sensor_data = r.json()[0]
|
||||||
|
self.device_data[camera]['name'] = \
|
||||||
|
sensor_data["name"]
|
||||||
|
self.device_data[camera]['is_online'] = \
|
||||||
|
sensor_data["is_online"]
|
||||||
|
self.device_data[camera]['is_streaming'] = \
|
||||||
|
sensor_data["is_streaming"]
|
||||||
|
self.device_data[camera]['battery_voltage'] = \
|
||||||
|
sensor_data["rq_battery_battery_volt"]
|
||||||
|
self.device_data[camera]['ac_voltage'] = \
|
||||||
|
sensor_data["rq_battery_vbridge_volt"]
|
||||||
|
self.device_data[camera]['location'] = \
|
||||||
|
sensor_data["location"]
|
||||||
|
self.device_data[camera]['data_tier'] = \
|
||||||
|
sensor_data["properties"]["streaming.data-usage-tier"]
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
_LOGGER.error(e)
|
_LOGGER.error(e)
|
||||||
_LOGGER.error('Failed to update, trying again')
|
_LOGGER.error('Failed to update, trying again')
|
||||||
@ -352,91 +294,182 @@ class NestTemperatureSensorAPI(NestAPI):
|
|||||||
self.login()
|
self.login()
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
def thermostat_set_temperature(self, device_id, temp, temp_high=None):
|
||||||
|
if device_id not in self.thermostats:
|
||||||
|
return
|
||||||
|
|
||||||
class NestCameraAPI(NestAPI):
|
try:
|
||||||
def __init__(self,
|
if temp_high is None:
|
||||||
email,
|
self._session.post(
|
||||||
password,
|
f"{self._czfe_url}/v5/put",
|
||||||
issue_token,
|
json={
|
||||||
cookie,
|
"objects": [
|
||||||
region,
|
{
|
||||||
device_id=None):
|
"object_key": f'shared.{device_id}',
|
||||||
super(NestCameraAPI, self).__init__(
|
"op": "MERGE",
|
||||||
email,
|
"value": {"target_temperature": temp},
|
||||||
password,
|
}
|
||||||
issue_token,
|
]
|
||||||
cookie,
|
},
|
||||||
device_id)
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
# log into dropcam
|
)
|
||||||
self._session.post(
|
else:
|
||||||
f"{API_URL}/dropcam/api/login",
|
self._session.post(
|
||||||
data={"access_token": self._access_token}
|
f"{self._czfe_url}/v5/put",
|
||||||
)
|
json={
|
||||||
self._device_id = device_id
|
"objects": [
|
||||||
self.location = None
|
{
|
||||||
self.name = "Nest Camera"
|
"object_key": f'shared.{device_id}',
|
||||||
self.online = None
|
"op": "MERGE",
|
||||||
self.is_streaming = None
|
"value": {
|
||||||
self.battery_voltage = None
|
"target_temperature_low": temp,
|
||||||
self.ac_voltge = None
|
"target_temperature_high": temp_high,
|
||||||
self.data_tier = None
|
},
|
||||||
if region.lower() == 'eu':
|
}
|
||||||
self._camera_url = CAMERA_EU_URL
|
]
|
||||||
elif region.lower() == 'us':
|
},
|
||||||
self._camera_url = CAMERA_US_URL
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
else:
|
)
|
||||||
lower_region = region.lower()
|
except requests.exceptions.RequestException as e:
|
||||||
self._camera_url = \
|
_LOGGER.error(e)
|
||||||
f'https://nexusapi-{lower_region}1.camera.home.nest.com'
|
_LOGGER.error('Failed to set temperature, trying again')
|
||||||
self.update()
|
self.thermostat_set_temperature(device_id, temp, temp_high)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to set temperature, trying to log in again')
|
||||||
|
self.login()
|
||||||
|
self.thermostat_set_temperature(device_id, temp, temp_high)
|
||||||
|
|
||||||
def update(self):
|
def thermostat_set_mode(self, device_id, mode):
|
||||||
if self._device_id:
|
if device_id not in self.thermostats:
|
||||||
props = self.get_properties()
|
return
|
||||||
self._location = None
|
|
||||||
self.name = props["name"]
|
|
||||||
self.online = props["is_online"]
|
|
||||||
self.is_streaming = props["is_streaming"]
|
|
||||||
self.battery_voltage = props["rq_battery_battery_volt"]
|
|
||||||
self.ac_voltge = props["rq_battery_vbridge_volt"]
|
|
||||||
self.location = props["location"]
|
|
||||||
self.data_tier = props["properties"]["streaming.data-usage-tier"]
|
|
||||||
|
|
||||||
def _set_properties(self, property, value):
|
try:
|
||||||
r = self._session.post(
|
self._session.post(
|
||||||
f"{CAMERA_WEBAPI_BASE}/api/dropcams.set_properties",
|
f"{self._czfe_url}/v5/put",
|
||||||
data={property: value, "uuid": self._device_id},
|
json={
|
||||||
)
|
"objects": [
|
||||||
|
{
|
||||||
|
"object_key": f'shared.{device_id}',
|
||||||
|
"op": "MERGE",
|
||||||
|
"value": {"target_temperature_type": mode},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
|
)
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
_LOGGER.error(e)
|
||||||
|
_LOGGER.error('Failed to set mode, trying again')
|
||||||
|
self.thermostat_set_mode(device_id, mode)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to set mode, trying to log in again')
|
||||||
|
self.login()
|
||||||
|
self.thermostat_set_mode(device_id, mode)
|
||||||
|
|
||||||
return r.json()["items"]
|
def thermostat_set_fan(self, device_id, date):
|
||||||
|
if device_id not in self.thermostats:
|
||||||
|
return
|
||||||
|
|
||||||
def get_properties(self):
|
try:
|
||||||
r = self._session.get(
|
self._session.post(
|
||||||
f"{API_URL}/dropcam/api/cameras/{self._device_id}"
|
f"{self._czfe_url}/v5/put",
|
||||||
)
|
json={
|
||||||
return r.json()[0]
|
"objects": [
|
||||||
|
{
|
||||||
|
"object_key": f'device.{device_id}',
|
||||||
|
"op": "MERGE",
|
||||||
|
"value": {"fan_timer_timeout": date},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
|
)
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
_LOGGER.error(e)
|
||||||
|
_LOGGER.error('Failed to set fan, trying again')
|
||||||
|
self.thermostat_set_fan(device_id, date)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to set fan, trying to log in again')
|
||||||
|
self.login()
|
||||||
|
self.thermostat_set_fan(device_id, date)
|
||||||
|
|
||||||
def get_cameras(self):
|
def thermostat_set_eco_mode(self, device_id, state):
|
||||||
r = self._session.get(
|
if device_id not in self.thermostats:
|
||||||
f"{CAMERA_WEBAPI_BASE}/api/cameras."
|
return
|
||||||
+ "get_owned_and_member_of_with_properties"
|
|
||||||
)
|
|
||||||
return r.json()["items"]
|
|
||||||
|
|
||||||
# def set_upload_quality(self, quality):
|
try:
|
||||||
# quality = str(quality)
|
mode = 'manual-eco' if state else 'schedule'
|
||||||
# return self._set_properties("streaming.data-usage-tier", quality)
|
self._session.post(
|
||||||
|
f"{self._czfe_url}/v5/put",
|
||||||
|
json={
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"object_key": f'device.{device_id}',
|
||||||
|
"op": "MERGE",
|
||||||
|
"value": {"eco": {"mode": mode}},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
headers={"Authorization": f"Basic {self._access_token}"},
|
||||||
|
)
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
_LOGGER.error(e)
|
||||||
|
_LOGGER.error('Failed to set eco, trying again')
|
||||||
|
self.thermostat_set_eco_mode(device_id, state)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to set eco, trying to log in again')
|
||||||
|
self.login()
|
||||||
|
self.thermostat_set_eco_mode(device_id, state)
|
||||||
|
|
||||||
def turn_off(self):
|
def _camera_set_properties(self, device_id, property, value):
|
||||||
return self._set_properties("streaming.enabled", "false")
|
if device_id not in self.cameras:
|
||||||
|
return
|
||||||
|
|
||||||
def turn_on(self):
|
try:
|
||||||
return self._set_properties("streaming.enabled", "true")
|
r = self._session.post(
|
||||||
|
f"{CAMERA_WEBAPI_BASE}/api/dropcams.set_properties",
|
||||||
|
data={property: value, "uuid": device_id},
|
||||||
|
)
|
||||||
|
|
||||||
def get_image(self, now):
|
return r.json()["items"]
|
||||||
r = self._session.get(
|
except requests.exceptions.RequestException as e:
|
||||||
f'{self._camera_url}/get_image?uuid={self._device_id}' +
|
_LOGGER.error(e)
|
||||||
f'&cachebuster={now}'
|
_LOGGER.error('Failed to set camera property, trying again')
|
||||||
)
|
return self._camera_set_properties(device_id, property, value)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to set camera property, ' +
|
||||||
|
'trying to log in again')
|
||||||
|
self.login()
|
||||||
|
return self._camera_set_properties(device_id, property, value)
|
||||||
|
|
||||||
return r.content
|
def camera_turn_off(self, device_id):
|
||||||
|
if device_id not in self.cameras:
|
||||||
|
return
|
||||||
|
|
||||||
|
return self._set_properties(device_id, "streaming.enabled", "false")
|
||||||
|
|
||||||
|
def camera_turn_on(self, device_id):
|
||||||
|
if device_id not in self.cameras:
|
||||||
|
return
|
||||||
|
|
||||||
|
return self._set_properties(device_id, "streaming.enabled", "true")
|
||||||
|
|
||||||
|
def camera_get_image(self, device_id, now):
|
||||||
|
if device_id not in self.cameras:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
r = self._session.get(
|
||||||
|
f'{self._camera_url}/get_image?uuid={device_id}' +
|
||||||
|
f'&cachebuster={now}'
|
||||||
|
)
|
||||||
|
|
||||||
|
return r.content
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
_LOGGER.error(e)
|
||||||
|
_LOGGER.error('Failed to get camera image, trying again')
|
||||||
|
return self.camera_get_image(device_id, now)
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.debug('Failed to get camera image, trying to log in again')
|
||||||
|
self.login()
|
||||||
|
return self.camera_get_image(device_id, now)
|
||||||
|
@ -2,61 +2,31 @@
|
|||||||
import logging
|
import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.components.camera import (
|
from homeassistant.components.camera import (
|
||||||
Camera,
|
Camera,
|
||||||
SUPPORT_ON_OFF,
|
SUPPORT_ON_OFF,
|
||||||
PLATFORM_SCHEMA
|
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from .api import NestCameraAPI
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_COOKIE,
|
|
||||||
CONF_ISSUE_TOKEN,
|
|
||||||
CONF_REGION,
|
|
||||||
DOMAIN
|
DOMAIN
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
||||||
{
|
|
||||||
vol.Optional(CONF_REGION, default="us"): cv.string,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass,
|
async def async_setup_platform(hass,
|
||||||
config,
|
config,
|
||||||
async_add_entities,
|
async_add_entities,
|
||||||
discovery_info=None):
|
discovery_info=None):
|
||||||
"""Set up a Nest Camera."""
|
"""Set up a Nest Camera."""
|
||||||
api = NestCameraAPI(
|
api = hass.data[DOMAIN]['api']
|
||||||
hass.data[DOMAIN][CONF_EMAIL],
|
|
||||||
hass.data[DOMAIN][CONF_PASSWORD],
|
|
||||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
|
||||||
hass.data[DOMAIN][CONF_COOKIE],
|
|
||||||
config.get(CONF_REGION)
|
|
||||||
)
|
|
||||||
|
|
||||||
# cameras = await hass.async_add_executor_job(nest.get_cameras())
|
|
||||||
cameras = []
|
cameras = []
|
||||||
_LOGGER.info("Adding cameras")
|
_LOGGER.info("Adding temperature sensors")
|
||||||
for camera in api.get_cameras():
|
for camera in api['cameras']:
|
||||||
_LOGGER.info("Adding nest cam uuid: %s", camera["uuid"])
|
_LOGGER.info(f"Adding nest camera uuid: {camera}")
|
||||||
device = NestCamera(camera["uuid"], NestCameraAPI(
|
cameras.append(NestCamera(camera, api))
|
||||||
hass.data[DOMAIN][CONF_EMAIL],
|
|
||||||
hass.data[DOMAIN][CONF_PASSWORD],
|
|
||||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
|
||||||
hass.data[DOMAIN][CONF_COOKIE],
|
|
||||||
config.get(CONF_REGION),
|
|
||||||
camera["uuid"]
|
|
||||||
))
|
|
||||||
cameras.append(device)
|
|
||||||
|
|
||||||
async_add_entities(cameras)
|
async_add_entities(cameras)
|
||||||
|
|
||||||
@ -78,7 +48,7 @@ class NestCamera(Camera):
|
|||||||
"""Return information about the device."""
|
"""Return information about the device."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {(DOMAIN, self._uuid)},
|
"identifiers": {(DOMAIN, self._uuid)},
|
||||||
"name": self.name,
|
"name": self._device.device_data[self._uuid]['name'],
|
||||||
"manufacturer": "Nest Labs",
|
"manufacturer": "Nest Labs",
|
||||||
"model": "Camera",
|
"model": "Camera",
|
||||||
}
|
}
|
||||||
@ -95,20 +65,20 @@ class NestCamera(Camera):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if on."""
|
"""Return true if on."""
|
||||||
return self._device.online
|
return self._device.device_data[self._uuid]['online']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_recording(self):
|
def is_recording(self):
|
||||||
return True
|
return True
|
||||||
"""Return true if the device is recording."""
|
"""Return true if the device is recording."""
|
||||||
return self._device.is_streaming
|
return self._device.device_data[self._uuid]['is_streaming']
|
||||||
|
|
||||||
def turn_off(self):
|
def turn_off(self):
|
||||||
self._device.turn_off()
|
self._device.camera_turn_off(self._uuid)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def turn_on(self):
|
def turn_on(self):
|
||||||
self._device.turn_on()
|
self._device.camera_turn_on(self._uuid)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -123,7 +93,7 @@ class NestCamera(Camera):
|
|||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of this camera."""
|
"""Return the name of this camera."""
|
||||||
return self._device.name
|
return self._device.device_data[self._uuid]['name']
|
||||||
|
|
||||||
def _ready_for_snapshot(self, now):
|
def _ready_for_snapshot(self, now):
|
||||||
return self._next_snapshot_at is None or now > self._next_snapshot_at
|
return self._next_snapshot_at is None or now > self._next_snapshot_at
|
||||||
@ -132,8 +102,7 @@ class NestCamera(Camera):
|
|||||||
"""Return a still image response from the camera."""
|
"""Return a still image response from the camera."""
|
||||||
now = utcnow()
|
now = utcnow()
|
||||||
if self._ready_for_snapshot(now) or True:
|
if self._ready_for_snapshot(now) or True:
|
||||||
image = self._device.get_image(now)
|
image = self._device.camera_get_image(self._uuid, now)
|
||||||
# _LOGGER.info(image)
|
|
||||||
|
|
||||||
self._next_snapshot_at = now + self._time_between_snapshots
|
self._next_snapshot_at = now + self._time_between_snapshots
|
||||||
self._last_image = image
|
self._last_image = image
|
||||||
|
@ -25,12 +25,11 @@ from homeassistant.components.climate.const import (
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_TEMPERATURE,
|
ATTR_TEMPERATURE,
|
||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
CONF_EMAIL,
|
|
||||||
CONF_PASSWORD,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from .api import NestThermostatAPI
|
from .const import (
|
||||||
from .const import DOMAIN, CONF_ISSUE_TOKEN, CONF_COOKIE
|
DOMAIN,
|
||||||
|
)
|
||||||
|
|
||||||
NEST_MODE_HEAT_COOL = "range"
|
NEST_MODE_HEAT_COOL = "range"
|
||||||
NEST_MODE_ECO = "eco"
|
NEST_MODE_ECO = "eco"
|
||||||
@ -63,24 +62,13 @@ async def async_setup_platform(hass,
|
|||||||
async_add_entities,
|
async_add_entities,
|
||||||
discovery_info=None):
|
discovery_info=None):
|
||||||
"""Set up the Nest climate device."""
|
"""Set up the Nest climate device."""
|
||||||
api = NestThermostatAPI(
|
api = hass.data[DOMAIN]['api']
|
||||||
hass.data[DOMAIN][CONF_EMAIL],
|
|
||||||
hass.data[DOMAIN][CONF_PASSWORD],
|
|
||||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
|
||||||
hass.data[DOMAIN][CONF_COOKIE],
|
|
||||||
)
|
|
||||||
|
|
||||||
thermostats = []
|
thermostats = []
|
||||||
_LOGGER.info("Adding thermostats")
|
_LOGGER.info("Adding thermostats")
|
||||||
for thermostat in api.get_devices():
|
for thermostat in api['thermostats']:
|
||||||
_LOGGER.info(f"Adding nest thermostat uuid: {thermostat}")
|
_LOGGER.info(f"Adding nest thermostat uuid: {thermostat}")
|
||||||
thermostats.append(NestClimate(thermostat, NestThermostatAPI(
|
thermostats.append(NestClimate(thermostat, api))
|
||||||
hass.data[DOMAIN][CONF_EMAIL],
|
|
||||||
hass.data[DOMAIN][CONF_PASSWORD],
|
|
||||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
|
||||||
hass.data[DOMAIN][CONF_COOKIE],
|
|
||||||
thermostat
|
|
||||||
)))
|
|
||||||
|
|
||||||
async_add_entities(thermostats)
|
async_add_entities(thermostats)
|
||||||
|
|
||||||
@ -103,21 +91,22 @@ class NestClimate(ClimateDevice):
|
|||||||
|
|
||||||
self.device = api
|
self.device = api
|
||||||
|
|
||||||
if self.device.can_heat and self.device.can_cool:
|
if self.device.device_data[device_id]['can_heat'] \
|
||||||
|
and self.device.device_data[device_id]['can_cool']:
|
||||||
self._operation_list.append(HVAC_MODE_AUTO)
|
self._operation_list.append(HVAC_MODE_AUTO)
|
||||||
self._support_flags |= SUPPORT_TARGET_TEMPERATURE_RANGE
|
self._support_flags |= SUPPORT_TARGET_TEMPERATURE_RANGE
|
||||||
|
|
||||||
# Add supported nest thermostat features
|
# Add supported nest thermostat features
|
||||||
if self.device.can_heat:
|
if self.device.device_data[device_id]['can_heat']:
|
||||||
self._operation_list.append(HVAC_MODE_HEAT)
|
self._operation_list.append(HVAC_MODE_HEAT)
|
||||||
|
|
||||||
if self.device.can_cool:
|
if self.device.device_data[device_id]['can_cool']:
|
||||||
self._operation_list.append(HVAC_MODE_COOL)
|
self._operation_list.append(HVAC_MODE_COOL)
|
||||||
|
|
||||||
self._operation_list.append(HVAC_MODE_OFF)
|
self._operation_list.append(HVAC_MODE_OFF)
|
||||||
|
|
||||||
# feature of device
|
# feature of device
|
||||||
if self.device.has_fan:
|
if self.device.device_data[device_id]['has_fan']:
|
||||||
self._support_flags = self._support_flags | SUPPORT_FAN_MODE
|
self._support_flags = self._support_flags | SUPPORT_FAN_MODE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -125,6 +114,11 @@ class NestClimate(ClimateDevice):
|
|||||||
"""Return an unique ID."""
|
"""Return an unique ID."""
|
||||||
return self.device_id
|
return self.device_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return an friendly name."""
|
||||||
|
return self.device.device_data[self.device_id]['name']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Return the list of supported features."""
|
"""Return the list of supported features."""
|
||||||
@ -135,11 +129,6 @@ class NestClimate(ClimateDevice):
|
|||||||
"""Return the polling state."""
|
"""Return the polling state."""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the climate device."""
|
|
||||||
return self.device_id
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def temperature_unit(self):
|
def temperature_unit(self):
|
||||||
"""Return the unit of measurement."""
|
"""Return the unit of measurement."""
|
||||||
@ -148,53 +137,63 @@ class NestClimate(ClimateDevice):
|
|||||||
@property
|
@property
|
||||||
def current_temperature(self):
|
def current_temperature(self):
|
||||||
"""Return the current temperature."""
|
"""Return the current temperature."""
|
||||||
return self.device.current_temperature
|
return self.device.device_data[self.device_id]['current_temperature']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_humidity(self):
|
def current_humidity(self):
|
||||||
"""Return the current humidity."""
|
"""Return the current humidity."""
|
||||||
return self.device.current_humidity
|
return self.device.device_data[self.device_id]['current_humidity']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_temperature(self):
|
def target_temperature(self):
|
||||||
"""Return the temperature we try to reach."""
|
"""Return the temperature we try to reach."""
|
||||||
if self.device.mode not in (NEST_MODE_HEAT_COOL, NEST_MODE_ECO):
|
if self.device.device_data[self.device_id]['mode'] \
|
||||||
return self.device.target_temperature
|
!= NEST_MODE_HEAT_COOL \
|
||||||
|
and not self.device.device_data[self.device_id]['eco']:
|
||||||
|
return \
|
||||||
|
self.device.device_data[self.device_id]['target_temperature']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_temperature_high(self):
|
def target_temperature_high(self):
|
||||||
"""Return the highbound target temperature we try to reach."""
|
"""Return the highbound target temperature we try to reach."""
|
||||||
if self.device.mode == NEST_MODE_ECO:
|
if self.device.device_data[self.device_id]['mode'] \
|
||||||
# TODO: Grab properly
|
== NEST_MODE_HEAT_COOL \
|
||||||
return None
|
and not self.device.device_data[self.device_id]['eco']:
|
||||||
if self.device.mode == NEST_MODE_HEAT_COOL:
|
return \
|
||||||
return self.device.target_temperature_high
|
self.device. \
|
||||||
|
device_data[self.device_id]['target_temperature_high']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_temperature_low(self):
|
def target_temperature_low(self):
|
||||||
"""Return the lowbound target temperature we try to reach."""
|
"""Return the lowbound target temperature we try to reach."""
|
||||||
if self.device.mode == NEST_MODE_ECO:
|
if self.device.device_data[self.device_id]['mode'] \
|
||||||
# TODO: Grab properly
|
== NEST_MODE_HEAT_COOL \
|
||||||
return None
|
and not self.device.device_data[self.device_id]['eco']:
|
||||||
if self.device.mode == NEST_MODE_HEAT_COOL:
|
return \
|
||||||
return self.device.target_temperature_low
|
self.device. \
|
||||||
|
device_data[self.device_id]['target_temperature_low']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hvac_action(self):
|
def hvac_action(self):
|
||||||
"""Return current operation ie. heat, cool, idle."""
|
"""Return current operation ie. heat, cool, idle."""
|
||||||
return ACTION_NEST_TO_HASS[self.device.get_action()]
|
return ACTION_NEST_TO_HASS[
|
||||||
|
self.device.device_data[self.device_id]['action']
|
||||||
|
]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hvac_mode(self):
|
def hvac_mode(self):
|
||||||
"""Return hvac target hvac state."""
|
"""Return hvac target hvac state."""
|
||||||
if self.device.mode is None or self.device.mode == NEST_MODE_ECO:
|
if self.device.device_data[self.device_id]['mode'] is None \
|
||||||
|
or self.device.device_data[self.device_id]['eco']:
|
||||||
# We assume the first operation in operation list is the main one
|
# We assume the first operation in operation list is the main one
|
||||||
return self._operation_list[0]
|
return self._operation_list[0]
|
||||||
|
|
||||||
return MODE_NEST_TO_HASS[self.device.mode]
|
return MODE_NEST_TO_HASS[
|
||||||
|
self.device.device_data[self.device_id]['mode']
|
||||||
|
]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hvac_modes(self):
|
def hvac_modes(self):
|
||||||
@ -204,7 +203,7 @@ class NestClimate(ClimateDevice):
|
|||||||
@property
|
@property
|
||||||
def preset_mode(self):
|
def preset_mode(self):
|
||||||
"""Return current preset mode."""
|
"""Return current preset mode."""
|
||||||
if self.device.mode == NEST_MODE_ECO:
|
if self.device.device_data[self.device_id]['eco']:
|
||||||
return PRESET_ECO
|
return PRESET_ECO
|
||||||
|
|
||||||
return PRESET_NONE
|
return PRESET_NONE
|
||||||
@ -217,16 +216,19 @@ class NestClimate(ClimateDevice):
|
|||||||
@property
|
@property
|
||||||
def fan_mode(self):
|
def fan_mode(self):
|
||||||
"""Return whether the fan is on."""
|
"""Return whether the fan is on."""
|
||||||
if self.device.has_fan:
|
if self.device.device_data[self.device_id]['has_fan']:
|
||||||
# Return whether the fan is on
|
# Return whether the fan is on
|
||||||
return FAN_ON if self.device.fan else FAN_AUTO
|
if self.device.device_data[self.device_id]['fan']:
|
||||||
|
return FAN_ON
|
||||||
|
else:
|
||||||
|
return FAN_AUTO
|
||||||
# No Fan available so disable slider
|
# No Fan available so disable slider
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fan_modes(self):
|
def fan_modes(self):
|
||||||
"""Return the list of available fan modes."""
|
"""Return the list of available fan modes."""
|
||||||
if self.device.has_fan:
|
if self.device.device_data[self.device_id]['has_fan']:
|
||||||
return self._fan_modes
|
return self._fan_modes
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -235,33 +237,52 @@ class NestClimate(ClimateDevice):
|
|||||||
temp = None
|
temp = None
|
||||||
target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW)
|
target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW)
|
||||||
target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
|
target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
|
||||||
if self.device.mode == NEST_MODE_HEAT_COOL:
|
if self.device.device_data[self.device_id]['mode'] == \
|
||||||
|
NEST_MODE_HEAT_COOL:
|
||||||
if target_temp_low is not None and target_temp_high is not None:
|
if target_temp_low is not None and target_temp_high is not None:
|
||||||
self.device.set_temp(target_temp_low, target_temp_high)
|
self.device.thermostat_set_temperature(
|
||||||
|
self.device_id,
|
||||||
|
target_temp_low,
|
||||||
|
target_temp_high,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
temp = kwargs.get(ATTR_TEMPERATURE)
|
temp = kwargs.get(ATTR_TEMPERATURE)
|
||||||
if temp is not None:
|
if temp is not None:
|
||||||
self.device.set_temp(temp)
|
self.device.thermostat_set_temperature(
|
||||||
|
self.device_id,
|
||||||
|
temp,
|
||||||
|
)
|
||||||
|
|
||||||
def set_hvac_mode(self, hvac_mode):
|
def set_hvac_mode(self, hvac_mode):
|
||||||
"""Set operation mode."""
|
"""Set operation mode."""
|
||||||
self.device.set_mode(MODE_HASS_TO_NEST[hvac_mode])
|
self.device.thermostat_set_mode(
|
||||||
|
self.device_id,
|
||||||
|
MODE_HASS_TO_NEST[hvac_mode],
|
||||||
|
)
|
||||||
|
|
||||||
def set_fan_mode(self, fan_mode):
|
def set_fan_mode(self, fan_mode):
|
||||||
"""Turn fan on/off."""
|
"""Turn fan on/off."""
|
||||||
if self.device.has_fan:
|
if self.device.device_data[self.device_id]['has_fan']:
|
||||||
if fan_mode == "on":
|
if fan_mode == "on":
|
||||||
self.device.set_fan(int(datetime.now().timestamp() + 60 * 30))
|
self.device.thermostat_set_fan(
|
||||||
|
self.device_id,
|
||||||
|
int(datetime.now().timestamp() + 60 * 30),
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.device.set_fan(0)
|
self.device.thermostat_set_fan(
|
||||||
|
self.device_id,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
|
||||||
def set_preset_mode(self, preset_mode):
|
def set_preset_mode(self, preset_mode):
|
||||||
"""Set preset mode."""
|
"""Set preset mode."""
|
||||||
need_eco = preset_mode in (PRESET_ECO)
|
need_eco = preset_mode == PRESET_ECO
|
||||||
is_eco = self.device.mode == NEST_MODE_ECO
|
|
||||||
|
|
||||||
if is_eco != need_eco:
|
if need_eco != self.device.device_data[self.device_id]['eco']:
|
||||||
self.device.set_eco_mode(need_eco)
|
self.device.thermostat_set_eco_mode(
|
||||||
|
self.device_id,
|
||||||
|
need_eco,
|
||||||
|
)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Updates data"""
|
"""Updates data"""
|
||||||
|
@ -2,14 +2,11 @@ import logging
|
|||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
from .api import NestTemperatureSensorAPI
|
from .const import DOMAIN
|
||||||
from .const import DOMAIN, CONF_COOKIE, CONF_ISSUE_TOKEN
|
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_BATTERY_LEVEL,
|
ATTR_BATTERY_LEVEL,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
CONF_EMAIL,
|
|
||||||
CONF_PASSWORD,
|
|
||||||
TEMP_CELSIUS
|
TEMP_CELSIUS
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,33 +18,21 @@ async def async_setup_platform(hass,
|
|||||||
async_add_entities,
|
async_add_entities,
|
||||||
discovery_info=None):
|
discovery_info=None):
|
||||||
"""Set up the Nest climate device."""
|
"""Set up the Nest climate device."""
|
||||||
api = NestTemperatureSensorAPI(
|
api = hass.data[DOMAIN]['api']
|
||||||
hass.data[DOMAIN][CONF_EMAIL],
|
|
||||||
hass.data[DOMAIN][CONF_PASSWORD],
|
|
||||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
|
||||||
hass.data[DOMAIN][CONF_COOKIE],
|
|
||||||
)
|
|
||||||
|
|
||||||
sensors = []
|
temperature_sensors = []
|
||||||
_LOGGER.info("Adding temperature sensors")
|
_LOGGER.info("Adding temperature sensors")
|
||||||
for sensor in api.get_devices():
|
for sensor in api['temperature_sensors']:
|
||||||
_LOGGER.info(f"Adding nest temp sensor uuid: {sensor}")
|
_LOGGER.info(f"Adding nest temp sensor uuid: {sensor}")
|
||||||
sensors.append(
|
temperature_sensors.append(NestTemperatureSensor(sensor, api))
|
||||||
NestTemperatureSensor(
|
|
||||||
sensor,
|
async_add_entities(temperature_sensors)
|
||||||
NestTemperatureSensorAPI(
|
|
||||||
hass.data[DOMAIN][CONF_EMAIL],
|
|
||||||
hass.data[DOMAIN][CONF_PASSWORD],
|
|
||||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
|
||||||
hass.data[DOMAIN][CONF_COOKIE],
|
|
||||||
sensor
|
|
||||||
)))
|
|
||||||
|
|
||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
class NestTemperatureSensor(Entity):
|
class NestTemperatureSensor(Entity):
|
||||||
"""Implementation of the DHT sensor."""
|
"""Implementation of the Nest Temperature Sensor."""
|
||||||
|
|
||||||
def __init__(self, device_id, api):
|
def __init__(self, device_id, api):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
@ -56,15 +41,20 @@ class NestTemperatureSensor(Entity):
|
|||||||
self.device_id = device_id
|
self.device_id = device_id
|
||||||
self.device = api
|
self.device = api
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return an unique ID."""
|
||||||
|
return self.device_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return self.device_id
|
return self.device.device_data[self.device_id]['name']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.device.temperature
|
return self.device.device_data[self.device_id]['temperature']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
@ -83,4 +73,7 @@ class NestTemperatureSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return {ATTR_BATTERY_LEVEL: self.device.battery_level}
|
return {
|
||||||
|
ATTR_BATTERY_LEVEL:
|
||||||
|
self.device.device_data[self.device_id]['battery_level']
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user