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

Commit dd07530

Browse files
authored
Merge pull request #30 from uberlinuxguy/active_sensor
Add a service to change the active Nest Temperature Sensor
2 parents 5dd0a91 + f62a559 commit dd07530

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

custom_components/badnest/api.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,37 @@ def update(self):
404404
self.device_data[sn]['battery_level'] = \
405405
sensor_data['battery_level']
406406

407+
@Decorators.refresh_login
408+
def thermostat_set_active_sensor(self, t_device_id, s_device_id):
409+
if t_device_id not in self.thermostats:
410+
_LOGGER.warning("Unknown t-stat id: {0}".format(t_device_id))
411+
return
412+
if s_device_id is None:
413+
value = {
414+
"active_rcs_sensors": [],
415+
"rcs_control_setting": "OFF",
416+
}
417+
else :
418+
if s_device_id not in self.temperature_sensors:
419+
_LOGGER.warning("Unknown Sensor ID: '{0}'".format(s_device_id))
420+
return
421+
value = {
422+
"active_rcs_sensors": [ f'kryptonite.{s_device_id}'],
423+
"rcs_control_setting": "OVERRIDE",
424+
}
425+
r = self._session.post(
426+
f'{self._czfe_url}/v5/put',
427+
json={
428+
"objects": [
429+
{
430+
"object_key": f'rcs_settings.{t_device_id}',
431+
"op": "MERGE",
432+
"value": value,
433+
}
434+
] }
435+
)
436+
self._check_request(r)
437+
407438
@Decorators.refresh_login
408439
def thermostat_set_temperature(self, device_id, temp, temp_high=None):
409440
if device_id not in self.thermostats:
@@ -429,7 +460,6 @@ def thermostat_set_temperature(self, device_id, temp, temp_high=None):
429460
]
430461
}
431462
)
432-
433463
self._check_request(r)
434464

435465
@Decorators.refresh_login

custom_components/badnest/climate.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
"""Demo platform that offers a fake climate device."""
22
from datetime import datetime
3+
4+
import asyncio
5+
36
import logging
7+
import voluptuous as vol
8+
from homeassistant.helpers import config_validation as cv
9+
410

11+
from homeassistant.helpers import config_validation as cv, entity_platform
512
try:
613
from homeassistant.components.climate import ClimateEntity
714
except ImportError:
@@ -80,7 +87,14 @@ async def async_setup_platform(hass,
8087
thermostats.append(NestClimate(thermostat, api))
8188

8289
async_add_entities(thermostats)
83-
90+
platform = entity_platform.current_platform.get()
91+
platform.async_register_entity_service(
92+
"set_active_sensor",
93+
{
94+
vol.Required('sensor'): cv.string,
95+
},
96+
"custom_set_active_sensor",
97+
)
8498

8599
class NestClimate(ClimateEntity):
86100
"""Representation of a Nest climate device."""
@@ -279,6 +293,16 @@ def set_temperature(self, **kwargs):
279293
self.device_id,
280294
temp,
281295
)
296+
def set_active_sensor(self, sensor):
297+
sensor_id=None
298+
if sensor != "" :
299+
entity_registry = asyncio.run(self.hass.helpers.entity_registry.async_get_registry())
300+
sensor_entity = entity_registry.async_get(sensor)
301+
sensor_id = sensor_entity.unique_id
302+
self.device.thermostat_set_active_sensor(
303+
self.device_id,
304+
sensor_id,
305+
)
282306

283307
def set_humidity(self, humidity):
284308
"""Set new target humidity."""
@@ -328,3 +352,6 @@ def set_preset_mode(self, preset_mode):
328352
def update(self):
329353
"""Updates data"""
330354
self.device.update()
355+
356+
def custom_set_active_sensor(entity, **kwargs):
357+
entity.set_active_sensor(kwargs['sensor'])
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set_active_sensor:
2+
description: Changes the active sensor for your Nest thermostat
3+
fields:
4+
entity_id:
5+
description: The thermostat to operate on
6+
example: "climate.nest"
7+
sensor:
8+
description: The Nest temperature sensor to use instead of the one in the thermostat. Set to "" to go back to the thermostat
9+
example: "sensor.bedroom_nest_sensor | \"\""

0 commit comments

Comments
 (0)