async def calc_output(self): """calculate control output and handle autotune""" if self.autotune != "none": if self.pidAutotune.run(self._cur_temp): if self.autotune_control_type == 'none': params = self.pidAutotune.get_pid_parameters( self.autotune, True) else: params = self.pidAutotune.get_pid_parameters( self.autotune, False, self.autotune_control_type) self.kp = params.Kp self.ki = params.Ki self.kd = params.Kd _LOGGER.warning( "Set Kp, Ki, Kd. " "Smart thermostat now runs on PID Controller: %s, %s, %s", self.kp, self.ki, self.kd) self.pidController = pid_controller.PIDArduino( self._keep_alive.seconds, self.kp, self.ki, self.kd, self.minOut, self.maxOut, time.time) self.autotune = "none" self.control_output = self.pidAutotune.output else: self.control_output = self.pidController.calc( self._cur_temp, self._target_temp) _LOGGER.info("Obtained current control output: %s", self.control_output) await self.set_controlvalue()
def __init__(self, name, heater_entity_id, sensor_entity_id, min_temp, max_temp, target_temp, ac_mode, min_cycle_duration, cold_tolerance, hot_tolerance, keep_alive, initial_hvac_mode, away_temp, precision, unit, difference, kp, ki, kd, pwm, autotune, noiseband): """Initialize the thermostat.""" self._name = name self.heater_entity_id = heater_entity_id self.sensor_entity_id = sensor_entity_id self.ac_mode = ac_mode self.min_cycle_duration = min_cycle_duration self._cold_tolerance = cold_tolerance self._hot_tolerance = hot_tolerance self._keep_alive = keep_alive self._hvac_mode = initial_hvac_mode self._saved_target_temp = target_temp or away_temp self._temp_precision = precision if self.ac_mode: self._hvac_list = [HVAC_MODE_COOL, HVAC_MODE_OFF] self.minOut = -difference self.maxOut = 0 else: self._hvac_list = [HVAC_MODE_HEAT, HVAC_MODE_OFF] self.minOut = 0 self.maxOut = difference self._active = False self._cur_temp = None self._temp_lock = asyncio.Lock() self._min_temp = min_temp self._max_temp = max_temp self._target_temp = target_temp self._unit = unit self._support_flags = SUPPORT_FLAGS if away_temp: self._support_flags = SUPPORT_FLAGS | SUPPORT_PRESET_MODE self._away_temp = away_temp self._is_away = False self.difference = difference self.kp = kp self.ki = ki self.kd = kd self.pwm = pwm self.autotune = autotune self.sensor_entity_id = sensor_entity_id self.time_changed = time.time() if self.autotune != "none": self.pidAutotune = pid_controller.PIDAutotune( self._target_temp, self.difference, self._keep_alive.seconds, self._keep_alive.seconds, self.minOut, self.maxOut, noiseband, time.time) _LOGGER.warning( "Autotune will run with the next Setpoint Value you set." "changes, submited after doesn't have any effekt until it's finished" ) else: self.pidController = pid_controller.PIDArduino( self._keep_alive.seconds, self.kp, self.ki, self.kd, self.minOut, self.maxOut, time.time)