Skip to content

Commit

Permalink
Merge pull request #33 from USA-RedDragon/nest_protect
Browse files Browse the repository at this point in the history
Add preliminary Nest Protect support
  • Loading branch information
Jacob McSwain authored Nov 4, 2019
2 parents c9693df + 2927881 commit 0c87f75
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
25 changes: 24 additions & 1 deletion custom_components/badnest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Thermostats
"device",
"shared",
# Protect
"topaz",
# Temperature sensors
"kryptonite",
]
Expand Down Expand Up @@ -48,6 +50,7 @@ def __init__(self,
self.cameras = []
self.thermostats = []
self.temperature_sensors = []
self.protects = []
self.login()
self._get_devices()
self.update()
Expand Down Expand Up @@ -149,7 +152,11 @@ def _get_devices(self):

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] = {}
Expand Down Expand Up @@ -250,6 +257,22 @@ def update(self):
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}"):
Expand Down
46 changes: 45 additions & 1 deletion custom_components/badnest/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

_LOGGER = logging.getLogger(__name__)

PROTECT_SENSOR_TYPES = [
"co_status",
"smoke_status",
"battery_health_state"
]


async def async_setup_platform(hass,
config,
Expand All @@ -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):
Expand Down Expand Up @@ -77,3 +90,34 @@ def device_state_attributes(self):
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()

0 comments on commit 0c87f75

Please sign in to comment.