diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index 5170874..f2fba76 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -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: @@ -429,7 +460,6 @@ def thermostat_set_temperature(self, device_id, temp, temp_high=None): ] } ) - self._check_request(r) @Decorators.refresh_login diff --git a/custom_components/badnest/climate.py b/custom_components/badnest/climate.py index 2fb93fe..c8349c1 100644 --- a/custom_components/badnest/climate.py +++ b/custom_components/badnest/climate.py @@ -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: @@ -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.""" @@ -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.""" @@ -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']) diff --git a/custom_components/badnest/services.yaml b/custom_components/badnest/services.yaml new file mode 100644 index 0000000..9a4a395 --- /dev/null +++ b/custom_components/badnest/services.yaml @@ -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 | \"\""