Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #30 from uberlinuxguy/active_sensor
Browse files Browse the repository at this point in the history
Add a service to change the active Nest Temperature Sensor
  • Loading branch information
mattsch authored Oct 1, 2020
2 parents 5dd0a91 + f62a559 commit dd07530
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
32 changes: 31 additions & 1 deletion custom_components/badnest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,37 @@ def update(self):
self.device_data[sn]['battery_level'] = \
sensor_data['battery_level']

@Decorators.refresh_login
def thermostat_set_active_sensor(self, t_device_id, s_device_id):
if t_device_id not in self.thermostats:
_LOGGER.warning("Unknown t-stat id: {0}".format(t_device_id))
return
if s_device_id is None:
value = {
"active_rcs_sensors": [],
"rcs_control_setting": "OFF",
}
else :
if s_device_id not in self.temperature_sensors:
_LOGGER.warning("Unknown Sensor ID: '{0}'".format(s_device_id))
return
value = {
"active_rcs_sensors": [ f'kryptonite.{s_device_id}'],
"rcs_control_setting": "OVERRIDE",
}
r = self._session.post(
f'{self._czfe_url}/v5/put',
json={
"objects": [
{
"object_key": f'rcs_settings.{t_device_id}',
"op": "MERGE",
"value": value,
}
] }
)
self._check_request(r)

@Decorators.refresh_login
def thermostat_set_temperature(self, device_id, temp, temp_high=None):
if device_id not in self.thermostats:
Expand All @@ -429,7 +460,6 @@ def thermostat_set_temperature(self, device_id, temp, temp_high=None):
]
}
)

self._check_request(r)

@Decorators.refresh_login
Expand Down
29 changes: 28 additions & 1 deletion custom_components/badnest/climate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
"""Demo platform that offers a fake climate device."""
from datetime import datetime

import asyncio

import logging
import voluptuous as vol
from homeassistant.helpers import config_validation as cv


from homeassistant.helpers import config_validation as cv, entity_platform
try:
from homeassistant.components.climate import ClimateEntity
except ImportError:
Expand Down Expand Up @@ -80,7 +87,14 @@ async def async_setup_platform(hass,
thermostats.append(NestClimate(thermostat, api))

async_add_entities(thermostats)

platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
"set_active_sensor",
{
vol.Required('sensor'): cv.string,
},
"custom_set_active_sensor",
)

class NestClimate(ClimateEntity):
"""Representation of a Nest climate device."""
Expand Down Expand Up @@ -279,6 +293,16 @@ def set_temperature(self, **kwargs):
self.device_id,
temp,
)
def set_active_sensor(self, sensor):
sensor_id=None
if sensor != "" :
entity_registry = asyncio.run(self.hass.helpers.entity_registry.async_get_registry())
sensor_entity = entity_registry.async_get(sensor)
sensor_id = sensor_entity.unique_id
self.device.thermostat_set_active_sensor(
self.device_id,
sensor_id,
)

def set_humidity(self, humidity):
"""Set new target humidity."""
Expand Down Expand Up @@ -328,3 +352,6 @@ def set_preset_mode(self, preset_mode):
def update(self):
"""Updates data"""
self.device.update()

def custom_set_active_sensor(entity, **kwargs):
entity.set_active_sensor(kwargs['sensor'])
9 changes: 9 additions & 0 deletions custom_components/badnest/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set_active_sensor:
description: Changes the active sensor for your Nest thermostat
fields:
entity_id:
description: The thermostat to operate on
example: "climate.nest"
sensor:
description: The Nest temperature sensor to use instead of the one in the thermostat. Set to "" to go back to the thermostat
example: "sensor.bedroom_nest_sensor | \"\""

0 comments on commit dd07530

Please sign in to comment.