diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index 2608721..b651734 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -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__( diff --git a/custom_components/badnest/sensor.py b/custom_components/badnest/sensor.py new file mode 100644 index 0000000..7db78d9 --- /dev/null +++ b/custom_components/badnest/sensor.py @@ -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} diff --git a/hacs.json b/hacs.json index 1ae8637..8a02e4f 100644 --- a/hacs.json +++ b/hacs.json @@ -1,4 +1,4 @@ { "name": "Bad Nest Thermostat", - "domains": ["climate", "camera"] + "domains": ["climate", "camera", "sensor"] }