Skip to content

Commit bc705bd

Browse files
committed
Add force_pid_refresh timer for PID calc.
1 parent 4cbd8e0 commit bc705bd

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

custom_components/smart_thermostat/climate.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
CONF_MIN_CYCLE_DURATION = "min_cycle_duration"
8484
CONF_MIN_OFF_CYCLE_DURATION = "min_off_cycle_duration"
8585
CONF_KEEP_ALIVE = "keep_alive"
86+
CONF_FORCE_PID_REFRESH = "force_pid_refresh"
8687
CONF_SAMPLING_PERIOD = "sampling_period"
8788
CONF_INITIAL_HVAC_MODE = "initial_hvac_mode"
8889
CONF_AWAY_TEMP = "away_temp"
@@ -120,6 +121,7 @@
120121
vol.Optional(CONF_MIN_OFF_CYCLE_DURATION): vol.All(
121122
cv.time_period, cv.positive_timedelta),
122123
vol.Required(CONF_KEEP_ALIVE): vol.All(cv.time_period, cv.positive_timedelta),
124+
vol.Optional(CONF_FORCE_PID_REFRESH): vol.All(cv.time_period, cv.positive_timedelta),
123125
vol.Optional(CONF_SAMPLING_PERIOD, default=DEFAULT_SAMPLING_PERIOD): vol.All(
124126
cv.time_period, cv.positive_timedelta),
125127
vol.Optional(CONF_INITIAL_HVAC_MODE): vol.In(
@@ -172,6 +174,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
172174
'min_cycle_duration': config.get(CONF_MIN_CYCLE_DURATION),
173175
'min_off_cycle_duration': config.get(CONF_MIN_OFF_CYCLE_DURATION),
174176
'keep_alive': config.get(CONF_KEEP_ALIVE),
177+
'force_pid_refresh': config.get(CONF_FORCE_PID_REFRESH),
175178
'sampling_period': config.get(CONF_SAMPLING_PERIOD),
176179
'initial_hvac_mode': config.get(CONF_INITIAL_HVAC_MODE),
177180
'away_temp': config.get(CONF_AWAY_TEMP),
@@ -239,6 +242,7 @@ def __init__(self, **kwargs):
239242
self._unique_id = slugify(f"{DOMAIN}_{self._name}_{self._heater_entity_id}")
240243
self._ac_mode = kwargs.get('ac_mode')
241244
self._keep_alive = kwargs.get('keep_alive')
245+
self._force_pid_refresh = kwargs.get('force_pid_refresh')
242246
self._sampling_period = kwargs.get('sampling_period').seconds
243247
self._hvac_mode = kwargs.get('initial_hvac_mode', None)
244248
self._saved_target_temp = kwargs.get('target_temp', None) or kwargs.get('away_temp', None)
@@ -324,6 +328,8 @@ async def async_added_to_hass(self):
324328

325329
if self._keep_alive:
326330
async_track_time_interval(self.hass, self._async_control_heating, self._keep_alive)
331+
if self._force_pid_refresh:
332+
async_track_time_interval(self.hass, self._async_pid_refresh, self._force_pid_refresh)
327333

328334
@callback
329335
def _async_startup(event):
@@ -685,6 +691,16 @@ def _async_update_temp(self, state):
685691
except ValueError as ex:
686692
_LOGGER.debug("Unable to update from sensor: %s", ex)
687693

694+
async def _async_pid_refresh(self, time_func=None):
695+
"""Refresh sensor state and run PID"""
696+
sensor_state = self.hass.states.get(self._sensor_entity_id)
697+
if sensor_state and sensor_state.state != STATE_UNKNOWN:
698+
self._previous_temp_time = self._cur_temp_time
699+
self._async_update_temp(sensor_state)
700+
self._cur_temp_time = time.time()
701+
_LOGGER.debug("Forcing PID refresh")
702+
await self._async_control_heating(calc_pid=True)
703+
688704
async def _async_control_heating(self, time_func=None, calc_pid=False):
689705
"""Run PID controller, optional autotune for faster integration"""
690706
async with self._temp_lock:

0 commit comments

Comments
 (0)