1
0
mirror of https://github.com/USA-RedDragon/badnest.git synced 2024-10-05 18:31:13 +01:00

Merge pull request #33 from USA-RedDragon/nest_protect

Add preliminary Nest Protect support
This commit is contained in:
Jacob McSwain 2019-11-03 21:46:57 -06:00 committed by GitHub
commit 0c87f7561a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 2 deletions

View File

@ -16,6 +16,8 @@ KNOWN_BUCKET_TYPES = [
# Thermostats
"device",
"shared",
# Protect
"topaz",
# Temperature sensors
"kryptonite",
]
@ -48,6 +50,7 @@ class NestAPI():
self.cameras = []
self.thermostats = []
self.temperature_sensors = []
self.protects = []
self.login()
self._get_devices()
self.update()
@ -149,7 +152,11 @@ class NestAPI():
buckets = r.json()['updated_buckets'][0]['value']['buckets']
for bucket in buckets:
if bucket.startswith('kryptonite.'):
if bucket.startswith('topaz.'):
sn = bucket.replace('topaz.', '')
self.protects.append(sn)
self.device_data[sn] = {}
elif bucket.startswith('kryptonite.'):
sn = bucket.replace('kryptonite.', '')
self.temperature_sensors.append(sn)
self.device_data[sn] = {}
@ -250,6 +257,22 @@ class NestAPI():
self.device_data[sn]['eco'] = True
else:
self.device_data[sn]['eco'] = False
# Protect
elif bucket["object_key"].startswith(
f"topaz.{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'] += ' Protect'
self.device_data[sn]['co_status'] = \
sensor_data['co_status']
self.device_data[sn]['smoke_status'] = \
sensor_data['smoke_status']
self.device_data[sn]['battery_health_state'] = \
sensor_data['battery_health_state']
# Temperature sensors
elif bucket["object_key"].startswith(
f"kryptonite.{sn}"):

View File

@ -12,6 +12,12 @@ from homeassistant.const import (
_LOGGER = logging.getLogger(__name__)
PROTECT_SENSOR_TYPES = [
"co_status",
"smoke_status",
"battery_health_state"
]
async def async_setup_platform(hass,
config,
@ -28,7 +34,14 @@ async def async_setup_platform(hass,
async_add_entities(temperature_sensors)
async_add_entities(sensors)
protect_sensors = []
_LOGGER.info("Adding protect sensors")
for sensor in api['protects']:
_LOGGER.info(f"Adding nest protect sensor uuid: {sensor}")
for sensor_type in PROTECT_SENSOR_TYPES:
protect_sensors.append(NestProtectSensor(sensor, sensor_type, api))
async_add_entities(protect_sensors)
class NestTemperatureSensor(Entity):
@ -77,3 +90,34 @@ class NestTemperatureSensor(Entity):
ATTR_BATTERY_LEVEL:
self.device.device_data[self.device_id]['battery_level']
}
class NestProtectSensor(Entity):
"""Implementation of the Nest Protect sensor."""
def __init__(self, device_id, sensor_type, api):
"""Initialize the sensor."""
self._name = "Nest Protect Sensor"
self.device_id = device_id
self._sensor_type = sensor_type
self.device = api
@property
def unique_id(self):
"""Return an unique ID."""
return self.device_id + '_' + self._sensor_type
@property
def name(self):
"""Return the name of the sensor."""
return self.device.device_data[self.device_id]['name'] + \
f' {self._sensor_type}'
@property
def state(self):
"""Return the state of the sensor."""
return self.device.device_data[self.device_id][self._sensor_type]
def update(self):
"""Get the latest data from the Protect and updates the states."""
self.device.update()