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

Merge pull request #13 from USA-RedDragon/temperature-sensors

Add temperature sensor support
This commit is contained in:
Jacob McSwain 2019-10-19 21:04:13 -05:00 committed by GitHub
commit 074a2fdf44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 143 additions and 1 deletions

View File

@ -247,6 +247,60 @@ class NestThermostatAPI(NestAPI):
)
class NestTemperatureSensorAPI(NestAPI):
def __init__(self,
email,
password,
issue_token,
cookie,
api_key,
device_id=None):
super(NestTemperatureSensorAPI, self).__init__(
email,
password,
issue_token,
cookie,
api_key,
device_id)
self.temperature = None
self._device_id = device_id
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.', ''))
return 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}"},
)
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"]
class NestCameraAPI(NestAPI):
def __init__(self, email, password, issue_token, cookie, api_key):
super(NestCameraAPI, self).__init__(

View File

@ -0,0 +1,88 @@
import logging
from homeassistant.helpers.entity import Entity
from .api import NestTemperatureSensorAPI
from .const import DOMAIN, CONF_APIKEY, CONF_COOKIE, CONF_ISSUE_TOKEN
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
DEVICE_CLASS_TEMPERATURE,
CONF_EMAIL,
CONF_PASSWORD,
TEMP_CELSIUS
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass,
config,
async_add_entities,
discovery_info=None):
"""Set up the Nest climate device."""
api = NestTemperatureSensorAPI(
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]
)
sensors = []
_LOGGER.info("Adding temperature sensors")
for sensor in api.get_devices():
_LOGGER.info(f"Adding nest temp sensor uuid: {sensor}")
sensors.append(
NestTemperatureSensor(
sensor,
NestTemperatureSensorAPI(
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],
sensor
)))
async_add_entities(sensors)
class NestTemperatureSensor(Entity):
"""Implementation of the DHT sensor."""
def __init__(self, device_id, api):
"""Initialize the sensor."""
self._name = "Nest Temperature Sensor"
self._unit_of_measurement = TEMP_CELSIUS
self.device_id = device_id
self.device = api
@property
def name(self):
"""Return the name of the sensor."""
return self.device_id
@property
def state(self):
"""Return the state of the sensor."""
return self.device.temperature
@property
def device_class(self):
"""Return the device class of this entity."""
return DEVICE_CLASS_TEMPERATURE
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
def update(self):
"""Get the latest data from the DHT and updates the states."""
self.device.update()
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {ATTR_BATTERY_LEVEL: self.device.battery_level}

View File

@ -1,4 +1,4 @@
{
"name": "Bad Nest Thermostat",
"domains": ["climate", "camera"]
"domains": ["climate", "camera", "sensor"]
}