Skip to content

Commit 464f11c

Browse files
committed
[TASK] fix controller
1 parent c6d9f4c commit 464f11c

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

custom_components/better_thermostat/climate.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def __init__(
231231
self.valve_position_entity = None
232232
self.version = VERSION
233233
self.last_change = datetime.now() - timedelta(hours=2)
234+
self._last_calibration = datetime.now() - timedelta(hours=2)
234235
self._last_window_state = None
235236
self._temp_lock = asyncio.Lock()
236237
self._last_reported_valve_position = None
@@ -241,13 +242,12 @@ def __init__(
241242
self._last_send_target_temp = None
242243
self._last_avg_outdoor_temp = None
243244
self._available = False
244-
self.control_queue_task = asyncio.Queue()
245+
self.control_queue_task = asyncio.Queue(maxsize=-1)
245246
if self.window_id is not None:
246-
self.window_queue_task = asyncio.Queue()
247-
for i in range(3):
248-
asyncio.create_task(control_queue(self))
249-
if self.window_id is not None:
250-
asyncio.create_task(window_queue(self))
247+
self.window_queue_task = asyncio.Queue(maxsize=-1)
248+
asyncio.create_task(control_queue(self))
249+
if self.window_id is not None:
250+
asyncio.create_task(window_queue(self))
251251

252252
async def async_added_to_hass(self):
253253
"""Run when entity about to be added.
@@ -480,6 +480,10 @@ async def startup(self):
480480
self.call_for_heat = old_state.attributes.get(
481481
ATTR_STATE_CALL_FOR_HEAT
482482
)
483+
if not old_state.attributes.get(ATTR_STATE_SAVED_TEMPERATURE):
484+
self._saved_temperature = old_state.attributes.get(
485+
ATTR_STATE_SAVED_TEMPERATURE
486+
)
483487
else:
484488
# No previous state, try and restore defaults
485489
if self._target_temp is None:

custom_components/better_thermostat/controlling.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ async def control_queue(self):
2828
None
2929
"""
3030
while True:
31+
if self.ignore_states:
32+
await asyncio.sleep(1)
33+
continue
3134
controls_to_process = await self.control_queue_task.get()
3235
if controls_to_process is not None:
33-
if self.ignore_states:
34-
await asyncio.sleep(1)
35-
continue
36-
else:
37-
_LOGGER.debug(f"better_thermostat {self.name}: processing controls")
38-
await control_trv(controls_to_process)
39-
self.control_queue_task.task_done()
36+
_LOGGER.debug(f"better_thermostat {self.name}: processing controls")
37+
await control_trv(controls_to_process)
38+
self.control_queue_task.task_done()
4039

4140

4241
async def control_trv(self, force_mode_change: bool = False):
@@ -114,10 +113,10 @@ async def control_trv(self, force_mode_change: bool = False):
114113
calibration = remapped_states.get("local_temperature_calibration") or None
115114

116115
if converted_hvac_mode is not None:
117-
_LOGGER.debug(
118-
f"better_thermostat {self.name}: control_trv: current TRV mode: {_current_TRV_mode} new TRV mode: {converted_hvac_mode}"
119-
)
120116
if _current_TRV_mode != converted_hvac_mode or force_mode_change:
117+
_LOGGER.debug(
118+
f"better_thermostat {self.name}: control_trv: current TRV mode: {_current_TRV_mode} new TRV mode: {converted_hvac_mode}"
119+
)
121120
system_mode_change = True
122121
await set_trv_values(self, "hvac_mode", converted_hvac_mode)
123122
if current_heating_setpoint is not None:
@@ -264,23 +263,37 @@ async def set_trv_values(self, key, value, hvac_mode=None):
264263
)
265264
elif key == "local_temperature_calibration":
266265
value = round_to_hundredth_degree(value)
267-
268-
max_calibration = self.hass.states.get(
269-
self.local_temperature_calibration_entity
270-
).attributes.get("max", 127)
271-
min_calibration = self.hass.states.get(
266+
current_calibration = self.hass.states.get(
272267
self.local_temperature_calibration_entity
273-
).attributes.get("min", -128)
274-
if value > max_calibration:
275-
value = max_calibration
276-
if value < min_calibration:
277-
value = min_calibration
278-
await self.hass.services.async_call(
279-
"number",
280-
SERVICE_SET_VALUE,
281-
{"entity_id": self.local_temperature_calibration_entity, "value": value},
282-
blocking=True,
283-
)
268+
).state
269+
if current_calibration != value and (
270+
(self._last_calibration + timedelta(minutes=5)).timestamp()
271+
< datetime.now().timestamp()
272+
):
273+
max_calibration = self.hass.states.get(
274+
self.local_temperature_calibration_entity
275+
).attributes.get("max", 127)
276+
min_calibration = self.hass.states.get(
277+
self.local_temperature_calibration_entity
278+
).attributes.get("min", -128)
279+
if value > max_calibration:
280+
value = max_calibration
281+
if value < min_calibration:
282+
value = min_calibration
283+
await self.hass.services.async_call(
284+
"number",
285+
SERVICE_SET_VALUE,
286+
{
287+
"entity_id": self.local_temperature_calibration_entity,
288+
"value": value,
289+
},
290+
blocking=True,
291+
)
292+
self._last_calibration = datetime.now()
293+
else:
294+
_LOGGER.debug(
295+
f"better_thermostat {self.name}: set_trv_values: skipping local calibration because of throttling"
296+
)
284297
elif key == "valve_position":
285298
await self.hass.services.async_call(
286299
"number",

custom_components/better_thermostat/events/trv.py

-10
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,6 @@ def convert_outbound_states(self, hvac_mode) -> Union[dict, None]:
329329
_new_heating_setpoint = 5
330330
hvac_mode = None
331331

332-
# check if the last local_calibration was in the last 5 minutes
333-
if _new_local_calibration is not None:
334-
if (
335-
self.last_change + timedelta(minutes=5)
336-
).timestamp() > datetime.now().timestamp():
337-
_LOGGER.debug(
338-
f"better_thermostat {self.name}: last local calibration was in the last 5 minutes, skip calibration"
339-
)
340-
_new_local_calibration = None
341-
342332
return {
343333
"current_heating_setpoint": _new_heating_setpoint,
344334
"local_temperature": state.get("current_temperature"),

0 commit comments

Comments
 (0)