1
0
mirror of https://github.com/USA-RedDragon/badnest.git synced 2025-09-07 10:01:53 +01:00

4 Commits
3.1.3 ... 3.2.1

Author SHA1 Message Date
Jacob McSwain
3d49b85c50 Merge pull request #24 from USA-RedDragon/cameras-multiple-api-instances
Attempt to make the camera's use different API instances
2019-10-23 21:48:30 -05:00
Jacob McSwain
4a6c88a6ef Merge pull request #23 from USA-RedDragon/api-fix-token-expiration
API: Handle KeyError to fix the expiration of tokens
2019-10-23 21:47:20 -05:00
Jacob McSwain
51efa759e8 Attempt to make the camera's use different API instances 2019-10-23 21:46:55 -05:00
Jacob McSwain
77bd36b4bd API: Handle KeyError to fix the expiration of tokens 2019-10-23 21:29:24 -05:00
2 changed files with 72 additions and 42 deletions

View File

@@ -126,7 +126,10 @@ class NestThermostatAPI(NestAPI):
return devices
except requests.exceptions.RequestException as e:
_LOGGER.error(e)
_LOGGER.error('Failed to get devices, trying to log in again')
_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()
@@ -193,7 +196,10 @@ class NestThermostatAPI(NestAPI):
self.mode = temp_mode
except requests.exceptions.RequestException as e:
_LOGGER.error(e)
_LOGGER.error('Failed to update, trying to log in again')
_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()
@@ -297,53 +303,79 @@ class NestTemperatureSensorAPI(NestAPI):
self.update()
def get_devices(self):
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']
for bucket in buckets:
if bucket.startswith('kryptonite.'):
devices.append(bucket.replace('kryptonite.', ''))
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']
for bucket in buckets:
if bucket.startswith('kryptonite.'):
devices.append(bucket.replace('kryptonite.', ''))
return devices
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):
r = self._session.post(
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
json={
"known_bucket_types": ["kryptonite"],
"known_bucket_versions": [],
},
headers={"Authorization": f"Basic {self._access_token}"},
)
try:
r = self._session.post(
f"{API_URL}/api/0.1/user/{self._user_id}/app_launch",
json={
"known_bucket_types": ["kryptonite"],
"known_bucket_versions": [],
},
headers={"Authorization": f"Basic {self._access_token}"},
)
for bucket in r.json()["updated_buckets"]:
if bucket["object_key"].startswith(
f"kryptonite.{self._device_id}"):
sensor_data = bucket["value"]
self.temperature = sensor_data["current_temperature"]
self.battery_level = sensor_data["battery_level"]
for bucket in r.json()["updated_buckets"]:
if bucket["object_key"].startswith(
f"kryptonite.{self._device_id}"):
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
@@ -353,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()

View File

@@ -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