Skip to content

Commit c6d9f4c

Browse files
committed
[TASK] several fixes
1 parent 3785543 commit c6d9f4c

File tree

7 files changed

+81
-57
lines changed

7 files changed

+81
-57
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Better Thermostat
88

99
**Important Notice: Consider this software as unfinished as it has not reached version 1.0.**
10+
**Don't select master on download, check `show beta version` install the latest one.**
1011

1112
### Requirements
1213

blueprints/night_mode.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ blueprint:
22
name: BT Night mode
33
description: Set BT Thermostats to night mode if Schedule event is active.
44
domain: automation
5-
source_url: https://github.com/KartoffelToby/better_thermostat/tree/master/blueprints/night_mode.yaml
5+
source_url: https://github.com/KartoffelToby/better_thermostat/blob/master/blueprints/night_mode.yaml
66
input:
77
night_times_schedule:
88
name: Schedule helper

custom_components/better_thermostat/climate.py

+58-45
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ async def save_target_temperature(self):
136136
self.async_write_ha_state()
137137

138138
async def restore_temp_temperature(self):
139-
self._target_temp = convert_to_float(
140-
self._saved_temperature, self.name, "service.restore_temp_temperature()"
141-
)
142-
self._saved_temperature = None
143-
self.async_write_ha_state()
144-
await self.control_queue_task.put(self)
139+
if self._saved_temperature is not None:
140+
self._target_temp = convert_to_float(
141+
self._saved_temperature, self.name, "service.restore_temp_temperature()"
142+
)
143+
self._saved_temperature = None
144+
self.async_write_ha_state()
145+
await self.control_queue_task.put(self)
145146

146147
@property
147148
def device_info(self):
@@ -385,11 +386,6 @@ async def startup(self):
385386
await asyncio.sleep(10)
386387
continue
387388

388-
self._target_temp = (
389-
trv_state.attributes.get("temperature")
390-
or trv_state.attributes.get("current_heating_setpoint")
391-
or 5
392-
)
393389
self._trv_hvac_mode = trv_state.state
394390
self._last_reported_valve_position = (
395391
trv_state.attributes.get("valve_position", None) or None
@@ -432,46 +428,50 @@ async def startup(self):
432428

433429
self._config["has_system_mode"] = has_system_mode
434430
# Check If we have an old state
435-
if (old_state := await self.async_get_last_state()) is not None:
431+
old_state = await self.async_get_last_state()
432+
_LOGGER.debug(old_state)
433+
434+
if old_state is not None:
436435
# If we have no initial temperature, restore
437-
if self._target_temp is None:
438-
# If we have a previously saved temperature
439-
if old_state.attributes.get(ATTR_TEMPERATURE) is None:
440-
self._target_temp = self._min_temp
441-
_LOGGER.debug(
442-
"better_thermostat %s: Undefined target temperature, falling back to %s",
436+
# If we have a previously saved temperature
437+
if old_state.attributes.get(ATTR_TEMPERATURE) is None:
438+
self._target_temp = (
439+
trv_state.attributes.get("current_heating_setpoint")
440+
or trv_state.attributes.get("temperature")
441+
or 5
442+
)
443+
_LOGGER.debug(
444+
"better_thermostat %s: Undefined target temperature, falling back to %s",
445+
self.name,
446+
self._target_temp,
447+
)
448+
else:
449+
_old_target_temperature = float(
450+
old_state.attributes.get(ATTR_TEMPERATURE)
451+
)
452+
# if the saved temperature is lower than the _min_temp, set it to _min_temp
453+
if _old_target_temperature < self._min_temp:
454+
_LOGGER.warning(
455+
"better_thermostat %s: Saved target temperature %s is lower than _min_temp %s, setting to _min_temp",
443456
self.name,
444-
self._target_temp,
457+
_old_target_temperature,
458+
self._min_temp,
445459
)
446-
else:
447-
_old_target_temperature = float(
448-
old_state.attributes.get(ATTR_TEMPERATURE)
460+
_old_target_temperature = self._min_temp
461+
# if the saved temperature is higher than the _max_temp, set it to _max_temp
462+
elif _old_target_temperature > self._max_temp:
463+
_LOGGER.warning(
464+
"better_thermostat %s: Saved target temperature %s is higher than _max_temp %s, setting to _max_temp",
465+
self.name,
466+
_old_target_temperature,
467+
self._min_temp,
449468
)
450-
# if the saved temperature is lower than the _min_temp, set it to _min_temp
451-
if _old_target_temperature < self._min_temp:
452-
_LOGGER.warning(
453-
"better_thermostat %s: Saved target temperature %s is lower than _min_temp %s, setting to _min_temp",
454-
self.name,
455-
_old_target_temperature,
456-
self._min_temp,
457-
)
458-
_old_target_temperature = self._min_temp
459-
# if the saved temperature is higher than the _max_temp, set it to _max_temp
460-
elif _old_target_temperature > self._max_temp:
461-
_LOGGER.warning(
462-
"better_thermostat %s: Saved target temperature %s is higher than _max_temp %s, setting to _max_temp",
463-
self.name,
464-
_old_target_temperature,
465-
self._min_temp,
466-
)
467-
_old_target_temperature = self._max_temp
468-
self._target_temp = _old_target_temperature
469+
_old_target_temperature = self._max_temp
470+
self._target_temp = _old_target_temperature
469471
if not self._bt_hvac_mode and old_state.state:
470472
self._bt_hvac_mode = old_state.state
471473
if not old_state.attributes.get(ATTR_STATE_LAST_CHANGE):
472474
self.last_change = old_state.attributes.get(ATTR_STATE_LAST_CHANGE)
473-
else:
474-
self.last_change = datetime.now()
475475
if not old_state.attributes.get(ATTR_STATE_DAY_SET_TEMP):
476476
self.last_daytime_temp = old_state.attributes.get(
477477
ATTR_STATE_DAY_SET_TEMP
@@ -484,19 +484,32 @@ async def startup(self):
484484
# No previous state, try and restore defaults
485485
if self._target_temp is None:
486486
_LOGGER.info(
487-
"better_thermostat %s: No previously saved temperature found on startup, turning heat off",
487+
"better_thermostat %s: No previously saved temperature found on startup, get it from the TRV",
488488
self.name,
489489
)
490+
self._target_temp = (
491+
trv_state.attributes.get("current_heating_setpoint")
492+
or trv_state.attributes.get("temperature")
493+
or 5
494+
)
490495
# if hvac mode could not be restored, turn heat off
491496
if self._trv_hvac_mode is None:
492497
self._trv_hvac_mode = HVAC_MODE_OFF
493-
if self._bt_hvac_mode is None:
498+
if self._bt_hvac_mode in (STATE_UNAVAILABLE, STATE_UNKNOWN, None):
494499
_LOGGER.warning(
495500
"better_thermostat %s: No previously hvac mode found on startup, turn heat off",
496501
self.name,
497502
)
498503
self._bt_hvac_mode = mode_remap(self, self._trv_hvac_mode, True)
499504

505+
_LOGGER.debug(
506+
"better_thermostat %s: Startup config, TRV hvac mode is %s, BT hvac mode is %s, Target temp %s",
507+
self.name,
508+
self._trv_hvac_mode,
509+
self._bt_hvac_mode,
510+
self._target_temp,
511+
)
512+
500513
self._config["system_mode"] = self._bt_hvac_mode
501514
self._last_window_state = self.window_open
502515
self._available = True

custom_components/better_thermostat/controlling.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ async def control_queue(self):
3333
if self.ignore_states:
3434
await asyncio.sleep(1)
3535
continue
36-
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+
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()
4040

4141

4242
async def control_trv(self, force_mode_change: bool = False):
@@ -267,10 +267,10 @@ async def set_trv_values(self, key, value, hvac_mode=None):
267267

268268
max_calibration = self.hass.states.get(
269269
self.local_temperature_calibration_entity
270-
).attributes.get("max")
270+
).attributes.get("max", 127)
271271
min_calibration = self.hass.states.get(
272272
self.local_temperature_calibration_entity
273-
).attributes.get("min")
273+
).attributes.get("min", -128)
274274
if value > max_calibration:
275275
value = max_calibration
276276
if value < min_calibration:

custom_components/better_thermostat/events/trv.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ async def trigger_trv_change(self, event):
143143
if _updated_needed or child_lock:
144144
self.async_write_ha_state()
145145
# make sure we only update the latest user interaction
146+
"""
146147
try:
147148
if self.control_queue_task.qsize() > 0:
148149
while self.control_queue_task.qsize() > 1:
149150
self.control_queue_task.task_done()
150151
except AttributeError:
151152
pass
153+
"""
152154
await self.control_queue_task.put(self)
153155

154156

@@ -273,8 +275,6 @@ def convert_outbound_states(self, hvac_mode) -> Union[dict, None]:
273275
_new_local_calibration = round_to_half_degree(
274276
calculate_local_setpoint_delta(self)
275277
)
276-
if _new_local_calibration is None:
277-
return None
278278
else:
279279
_new_local_calibration = calculate_local_setpoint_delta(self)
280280

@@ -329,6 +329,16 @@ 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+
332342
return {
333343
"current_heating_setpoint": _new_heating_setpoint,
334344
"local_temperature": state.get("current_temperature"),

docs/schedule.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ Services you can call from Home Assistant to set a temporary target temperature
2121
Basically you can setup a automation that triggers a service call for every climate entity.
2222
As a example you can use this blueprint:
2323

24-
<a href="https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fgithub.com%2FKartoffelToby%2Fbetter_thermostat%2Ftree%2Fmaster%2Fblueprints%2Fnight_mode.yaml" target="_blank"><img src="https://my.home-assistant.io/badges/blueprint_import.svg" alt="Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled." /></a>
24+
<a href="https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https://github.com/KartoffelToby/better_thermostat/blob/master/blueprints/night_mode.yaml" target="_blank"><img src="https://my.home-assistant.io/badges/blueprint_import.svg" alt="Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled." /></a>
2525

2626
```yaml
2727
blueprint:
2828
name: BT Night mode
2929
description: Set BT Thermostats to night mode if Schedule event is active.
3030
domain: automation
31-
source_url: https://github.com/KartoffelToby/better_thermostat/tree/master/blueprints/night_mode.yaml
31+
source_url: https://github.com/KartoffelToby/better_thermostat/blob/master/blueprints/night_mode.yaml
3232
input:
3333
night_times_schedule:
3434
name: Schedule helper

hacs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"name": "Better Thermostat",
33
"render_readme": true,
44
"homeassistant": "2021.12.0",
5-
"hide_default_branch": true
5+
"hide_default_branch": false
66
}

0 commit comments

Comments
 (0)