From 5b3c70e0ec5f4e6e3381e3008ee22be9ecec84b7 Mon Sep 17 00:00:00 2001 From: Chris Marcellino Date: Fri, 13 Dec 2019 15:12:19 -0600 Subject: [PATCH 1/3] Add target humidity support to allow automation --- custom_components/badnest/api.py | 31 ++++++++++++++++++++++++ custom_components/badnest/climate.py | 36 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index 0ae3199..1f0307e 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -252,6 +252,10 @@ class NestAPI(): sensor_data["fan_timer_timeout"] self.device_data[sn]['current_humidity'] = \ sensor_data["current_humidity"] + self.device_data[sn]['target_humidity'] = \ + sensor_data["target_humidity"] + self.device_data[sn]['target_humidity_enabled'] = \ + sensor_data["target_humidity_enabled"] if sensor_data["eco"]["mode"] == 'manual-eco' or \ sensor_data["eco"]["mode"] == 'auto-eco': self.device_data[sn]['eco'] = True @@ -362,6 +366,33 @@ class NestAPI(): self.login() self.thermostat_set_temperature(device_id, temp, temp_high) + def thermostat_set_target_humidity(self, device_id, humidity): + if device_id not in self.thermostats: + return + + try: + self._session.post( + f"{self._czfe_url}/v5/put", + json={ + "objects": [ + { + "object_key": f'device.{device_id}', + "op": "MERGE", + "value": {"target_humidity": humidity}, + } + ] + }, + headers={"Authorization": f"Basic {self._access_token}"}, + ) + except requests.exceptions.RequestException as e: + _LOGGER.error(e) + _LOGGER.error('Failed to set humidity, trying again') + self.thermostat_set_target_humidity(device_id, mode) + except KeyError: + _LOGGER.debug('Failed to set humidity, trying to log in again') + self.login() + self.thermostat_set_target_humidity(device_id, mode) + def thermostat_set_mode(self, device_id, mode): if device_id not in self.thermostats: return diff --git a/custom_components/badnest/climate.py b/custom_components/badnest/climate.py index 78a2dbe..00b0e7d 100644 --- a/custom_components/badnest/climate.py +++ b/custom_components/badnest/climate.py @@ -16,6 +16,7 @@ from homeassistant.components.climate.const import ( SUPPORT_PRESET_MODE, SUPPORT_TARGET_TEMPERATURE, SUPPORT_TARGET_TEMPERATURE_RANGE, + SUPPORT_TARGET_HUMIDITY, PRESET_ECO, PRESET_NONE, CURRENT_HVAC_HEAT, @@ -52,6 +53,10 @@ ACTION_NEST_TO_HASS = { MODE_NEST_TO_HASS = {v: k for k, v in MODE_HASS_TO_NEST.items()} +ROUND_TARGET_HUMIDITY_TO_NEAREST = 5 +NEST_HUMIDITY_MIN = 10 +NEST_HUMIDITY_MAX = 60 + PRESET_MODES = [PRESET_NONE, PRESET_ECO] _LOGGER = logging.getLogger(__name__) @@ -109,6 +114,10 @@ class NestClimate(ClimateDevice): if self.device.device_data[device_id]['has_fan']: self._support_flags = self._support_flags | SUPPORT_FAN_MODE + if self.device.device_data[device_id]['target_humidity_enabled']: + self._support_flags = self._support_flags | SUPPORT_TARGET_HUMIDITY + + @property def unique_id(self): """Return an unique ID.""" @@ -144,6 +153,21 @@ class NestClimate(ClimateDevice): """Return the current humidity.""" return self.device.device_data[self.device_id]['current_humidity'] + @property + def target_humidity(self): + """Return the target humidity.""" + return self.device.device_data[self.device_id]['target_humidity'] + + @property + def min_humidity(self): + """Return the min target humidity.""" + return NEST_HUMIDITY_MIN + + @property + def max_humidity(self): + """Return the max target humidity.""" + return NEST_HUMIDITY_MAX + @property def target_temperature(self): """Return the temperature we try to reach.""" @@ -253,6 +277,18 @@ class NestClimate(ClimateDevice): temp, ) + def set_humidity(self, humidity): + """Set new target humidity.""" + humidity -= humidity % ROUND_TARGET_HUMIDITY_TO_NEAREST + if humidity < NEST_HUMIDITY_MIN: + humidity = NEST_HUMIDITY_MIN + if humidity > NEST_HUMIDITY_MAX: + humidity = NEST_HUMIDITY_MAX + self.device.thermostat_set_target_humidity( + self.device_id, + humidity, + ) + def set_hvac_mode(self, hvac_mode): """Set operation mode.""" self.device.thermostat_set_mode( From f1ebd8a2f77c2204e6b3e68ce67258e757a48e3d Mon Sep 17 00:00:00 2001 From: Chris Marcellino Date: Sun, 19 Jan 2020 15:09:22 -0600 Subject: [PATCH 2/3] Round humidity changes to nearest 5% instead of down --- custom_components/badnest/climate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/badnest/climate.py b/custom_components/badnest/climate.py index 00b0e7d..e21f22d 100644 --- a/custom_components/badnest/climate.py +++ b/custom_components/badnest/climate.py @@ -279,7 +279,7 @@ class NestClimate(ClimateDevice): def set_humidity(self, humidity): """Set new target humidity.""" - humidity -= humidity % ROUND_TARGET_HUMIDITY_TO_NEAREST + humidity = int(round(float(humidity) / ROUND_TARGET_HUMIDITY_TO_NEAREST) * ROUND_TARGET_HUMIDITY_TO_NEAREST) if humidity < NEST_HUMIDITY_MIN: humidity = NEST_HUMIDITY_MIN if humidity > NEST_HUMIDITY_MAX: From cbc8702ee00b5da61e8ee3fc2c17a0a18d0fdb6d Mon Sep 17 00:00:00 2001 From: Chris Marcellino Date: Mon, 20 Jan 2020 07:18:37 -0600 Subject: [PATCH 3/3] Fix copy-past error in humidity setter code --- custom_components/badnest/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index 1f0307e..90da9d1 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -387,11 +387,11 @@ class NestAPI(): except requests.exceptions.RequestException as e: _LOGGER.error(e) _LOGGER.error('Failed to set humidity, trying again') - self.thermostat_set_target_humidity(device_id, mode) + self.thermostat_set_target_humidity(device_id, humidity) except KeyError: _LOGGER.debug('Failed to set humidity, trying to log in again') self.login() - self.thermostat_set_target_humidity(device_id, mode) + self.thermostat_set_target_humidity(device_id, humidity) def thermostat_set_mode(self, device_id, mode): if device_id not in self.thermostats: