mirror of
https://github.com/USA-RedDragon/badnest.git
synced 2025-01-18 13:05:26 +00:00
Add temperature sensor support
This commit is contained in:
parent
f2643d4427
commit
87c65ab23e
@ -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__(
|
||||
|
88
custom_components/badnest/sensor.py
Normal file
88
custom_components/badnest/sensor.py
Normal 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}
|
Loading…
Reference in New Issue
Block a user