From b4debfcf9cdcb1dda1fc083a91d3decd4726f68b Mon Sep 17 00:00:00 2001 From: Jacob McSwain Date: Sun, 20 Oct 2019 19:52:13 -0500 Subject: [PATCH] Thermostat eco preset support --- custom_components/badnest/api.py | 14 ++++++++---- custom_components/badnest/climate.py | 34 ++++++++-------------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index b651734..d40db12 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -92,7 +92,6 @@ class NestThermostatAPI(NestAPI): self.can_cool = None self.has_fan = None self.fan = None - self.away = None self.current_temperature = None self.target_temperature = None self.target_temperature_high = None @@ -151,7 +150,8 @@ class NestThermostatAPI(NestAPI): ] self._hvac_ac_state = thermostat_data["hvac_ac_state"] self._hvac_heater_state = thermostat_data["hvac_heater_state"] - self.mode = thermostat_data["target_temperature_type"] + if self.mode != 'eco': + self.mode = thermostat_data["target_temperature_type"] self.target_temperature_high = thermostat_data[ "target_temperature_high" ] @@ -166,7 +166,10 @@ class NestThermostatAPI(NestAPI): self.has_fan = thermostat_data["has_fan"] self.fan = thermostat_data["fan_timer_timeout"] > 0 self.current_humidity = thermostat_data["current_humidity"] - self.away = thermostat_data["home_away_input"] + if thermostat_data["eco"]["mode"] == 'manual-eco': + self.mode = 'eco' + else: + self.mode = 'unknown' def set_temp(self, temp, temp_high=None): if temp_high is None: @@ -231,7 +234,8 @@ class NestThermostatAPI(NestAPI): headers={"Authorization": f"Basic {self._access_token}"}, ) - def set_eco_mode(self): + def set_eco_mode(self, state): + mode = 'manual-eco' if state else 'schedule' self._session.post( f"{self._czfe_url}/v5/put", json={ @@ -239,7 +243,7 @@ class NestThermostatAPI(NestAPI): { "object_key": f'device.{self._device_id}', "op": "MERGE", - "value": {"eco": {"mode": "manual-eco"}}, + "value": {"eco": {"mode": mode}}, } ] }, diff --git a/custom_components/badnest/climate.py b/custom_components/badnest/climate.py index 01a5099..95cf65c 100644 --- a/custom_components/badnest/climate.py +++ b/custom_components/badnest/climate.py @@ -13,9 +13,9 @@ from homeassistant.components.climate.const import ( HVAC_MODE_HEAT, HVAC_MODE_OFF, SUPPORT_FAN_MODE, + SUPPORT_PRESET_MODE, SUPPORT_TARGET_TEMPERATURE, SUPPORT_TARGET_TEMPERATURE_RANGE, - PRESET_AWAY, PRESET_ECO, PRESET_NONE, CURRENT_HVAC_HEAT, @@ -53,9 +53,7 @@ ACTION_NEST_TO_HASS = { MODE_NEST_TO_HASS = {v: k for k, v in MODE_HASS_TO_NEST.items()} -PRESET_AWAY_AND_ECO = "Away and Eco" - -PRESET_MODES = [PRESET_NONE, PRESET_AWAY, PRESET_ECO, PRESET_AWAY_AND_ECO] +PRESET_MODES = [PRESET_NONE, PRESET_ECO] _LOGGER = logging.getLogger(__name__) @@ -100,8 +98,7 @@ class NestClimate(ClimateDevice): self.device_id = device_id # Set the default supported features - self._support_flags = SUPPORT_TARGET_TEMPERATURE - # | SUPPORT_PRESET_MODE + self._support_flags = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE # Not all nest devices support cooling and heating remove unused self._operation_list = [] @@ -195,7 +192,9 @@ class NestClimate(ClimateDevice): @property def hvac_mode(self): """Return hvac target hvac state.""" - if self.device.mode is None or self.device.mode == NEST_MODE_ECO: + if self.device.mode == 'unknown' \ + or self.device.mode == NEST_MODE_ECO \ + or self.device.mode is None: # We assume the first operation in operation list is the main one return self._operation_list[0] @@ -209,16 +208,10 @@ class NestClimate(ClimateDevice): @property def preset_mode(self): """Return current preset mode.""" - if self.device.away and self.device.mode == NEST_MODE_ECO: - return PRESET_AWAY_AND_ECO - - if self.device.away: - return PRESET_AWAY - if self.device.mode == NEST_MODE_ECO: return PRESET_ECO - return None + return PRESET_NONE @property def preset_modes(self): @@ -268,20 +261,11 @@ class NestClimate(ClimateDevice): def set_preset_mode(self, preset_mode): """Set preset mode.""" - need_away = preset_mode in (PRESET_AWAY, PRESET_AWAY_AND_ECO) - need_eco = preset_mode in (PRESET_ECO, PRESET_AWAY_AND_ECO) - is_away = self.device.away + need_eco = preset_mode in (PRESET_ECO) is_eco = self.device.mode == NEST_MODE_ECO - if is_away != need_away: - pass - # self.device.set_away() - if is_eco != need_eco: - if need_eco: - self.device.set_eco_mode() - else: - self.device.mode = MODE_HASS_TO_NEST[self._operation_list[0]] + self.device.set_eco_mode(need_eco) def update(self): """Updates data"""