mirror of
https://github.com/USA-RedDragon/badnest.git
synced 2025-09-07 17:31:53 +01:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3d49b85c50 | ||
|
4a6c88a6ef | ||
|
51efa759e8 | ||
|
77bd36b4bd | ||
|
1228304609 | ||
|
68e71850be | ||
|
5f4b14514c | ||
|
6f097c58a8 | ||
|
98daabc5d7 | ||
|
c4f702b859 | ||
|
977c553a8d | ||
|
f1abecefce |
@@ -25,10 +25,18 @@ class NestAPI:
|
||||
self._session = requests.Session()
|
||||
self._session.headers.update({"Referer": "https://home.nest.com/"})
|
||||
self._device_id = device_id
|
||||
if not email and not password:
|
||||
self._login_google(issue_token, cookie, api_key)
|
||||
self._email = email
|
||||
self._password = password
|
||||
self._issue_token = issue_token
|
||||
self._cookie = cookie
|
||||
self._api_key = api_key
|
||||
self.login()
|
||||
|
||||
def login(self):
|
||||
if not self._email and not self._password:
|
||||
self._login_google(self._issue_token, self._cookie, self._api_key)
|
||||
else:
|
||||
self._login_nest(email, password)
|
||||
self._login_nest(self._email, self._password)
|
||||
|
||||
def _login_nest(self, email, password):
|
||||
r = self._session.post(
|
||||
@@ -100,6 +108,7 @@ class NestThermostatAPI(NestAPI):
|
||||
self.update()
|
||||
|
||||
def get_devices(self):
|
||||
try:
|
||||
r = self._session.post(
|
||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||
json={
|
||||
@@ -115,6 +124,14 @@ class NestThermostatAPI(NestAPI):
|
||||
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:
|
||||
@@ -125,6 +142,7 @@ class NestThermostatAPI(NestAPI):
|
||||
return "off"
|
||||
|
||||
def update(self):
|
||||
try:
|
||||
r = self._session.post(
|
||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||
json={
|
||||
@@ -136,12 +154,15 @@ class NestThermostatAPI(NestAPI):
|
||||
|
||||
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}"):
|
||||
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.target_temperature = \
|
||||
thermostat_data["target_temperature"]
|
||||
self._compressor_lockout_enabled = thermostat_data[
|
||||
"compressor_lockout_enabled"
|
||||
]
|
||||
@@ -149,9 +170,10 @@ class NestThermostatAPI(NestAPI):
|
||||
"compressor_lockout_timeout"
|
||||
]
|
||||
self._hvac_ac_state = thermostat_data["hvac_ac_state"]
|
||||
self._hvac_heater_state = thermostat_data["hvac_heater_state"]
|
||||
if self.mode != 'eco':
|
||||
self.mode = thermostat_data["target_temperature_type"]
|
||||
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"
|
||||
]
|
||||
@@ -159,17 +181,27 @@ class NestThermostatAPI(NestAPI):
|
||||
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}"):
|
||||
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._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':
|
||||
self.mode = 'eco'
|
||||
else:
|
||||
self.mode = 'unknown'
|
||||
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:
|
||||
@@ -271,6 +303,7 @@ class NestTemperatureSensorAPI(NestAPI):
|
||||
self.update()
|
||||
|
||||
def get_devices(self):
|
||||
try:
|
||||
r = self._session.post(
|
||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||
json={
|
||||
@@ -286,8 +319,17 @@ class NestTemperatureSensorAPI(NestAPI):
|
||||
devices.append(bucket.replace('kryptonite.', ''))
|
||||
|
||||
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 update(self):
|
||||
try:
|
||||
r = self._session.post(
|
||||
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
|
||||
json={
|
||||
@@ -303,21 +345,37 @@ class NestTemperatureSensorAPI(NestAPI):
|
||||
sensor_data = bucket["value"]
|
||||
self.temperature = sensor_data["current_temperature"]
|
||||
self.battery_level = sensor_data["battery_level"]
|
||||
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()
|
||||
|
||||
|
||||
class NestCameraAPI(NestAPI):
|
||||
def __init__(self, email, password, issue_token, cookie, api_key):
|
||||
def __init__(self,
|
||||
email,
|
||||
password,
|
||||
issue_token,
|
||||
cookie,
|
||||
api_key,
|
||||
device_id=None):
|
||||
super(NestCameraAPI, self).__init__(
|
||||
email,
|
||||
password,
|
||||
issue_token,
|
||||
cookie,
|
||||
api_key)
|
||||
api_key,
|
||||
device_id)
|
||||
# log into dropcam
|
||||
self._session.post(
|
||||
f"{API_URL}/dropcam/api/login",
|
||||
data={"access_token": self._access_token}
|
||||
)
|
||||
self._device_id = device_id
|
||||
self.location = None
|
||||
self.name = "Nest Camera"
|
||||
self.online = None
|
||||
@@ -327,10 +385,6 @@ class NestCameraAPI(NestAPI):
|
||||
self.data_tier = None
|
||||
self.update()
|
||||
|
||||
def set_device(self, uuid):
|
||||
self._device_id = uuid
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
if self._device_id:
|
||||
props = self.get_properties()
|
||||
|
@@ -13,7 +13,6 @@ from .const import DOMAIN, CONF_ISSUE_TOKEN, CONF_COOKIE, CONF_APIKEY
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = "Nest Camera"
|
||||
DATA_KEY = "camera.badnest"
|
||||
|
||||
|
||||
async def async_setup_platform(hass,
|
||||
@@ -21,8 +20,6 @@ async def async_setup_platform(hass,
|
||||
async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up a Nest Camera."""
|
||||
hass.data[DATA_KEY] = dict()
|
||||
|
||||
api = NestCameraAPI(
|
||||
hass.data[DOMAIN][CONF_EMAIL],
|
||||
hass.data[DOMAIN][CONF_PASSWORD],
|
||||
@@ -36,9 +33,15 @@ async def async_setup_platform(hass,
|
||||
_LOGGER.info("Adding cameras")
|
||||
for camera in api.get_cameras():
|
||||
_LOGGER.info("Adding nest cam uuid: %s", camera["uuid"])
|
||||
device = NestCamera(camera["uuid"], api)
|
||||
device = NestCamera(camera["uuid"], NestCameraAPI(
|
||||
hass.data[DOMAIN][CONF_EMAIL],
|
||||
hass.data[DOMAIN][CONF_PASSWORD],
|
||||
hass.data[DOMAIN][CONF_ISSUE_TOKEN],
|
||||
hass.data[DOMAIN][CONF_COOKIE],
|
||||
hass.data[DOMAIN][CONF_APIKEY],
|
||||
camera["uuid"]
|
||||
))
|
||||
cameras.append(device)
|
||||
hass.data[DATA_KEY][camera["uuid"]] = device
|
||||
|
||||
async_add_entities(cameras)
|
||||
|
||||
@@ -50,7 +53,6 @@ class NestCamera(Camera):
|
||||
"""Initialize a Nest camera."""
|
||||
super().__init__()
|
||||
self._uuid = uuid
|
||||
api.set_device(self._uuid)
|
||||
self._device = api
|
||||
self._time_between_snapshots = timedelta(seconds=30)
|
||||
self._last_image = None
|
||||
|
@@ -192,9 +192,7 @@ class NestClimate(ClimateDevice):
|
||||
@property
|
||||
def hvac_mode(self):
|
||||
"""Return hvac target hvac state."""
|
||||
if self.device.mode == 'unknown' \
|
||||
or self.device.mode == NEST_MODE_ECO \
|
||||
or self.device.mode is None:
|
||||
if self.device.mode is None or self.device.mode == NEST_MODE_ECO:
|
||||
# We assume the first operation in operation list is the main one
|
||||
return self._operation_list[0]
|
||||
|
||||
|
Reference in New Issue
Block a user