Skip to content
This repository was archived by the owner on Jun 12, 2020. It is now read-only.

Commit e58d624

Browse files
committed
removed async calls to fix I/O inside the event loop.
1 parent c29079c commit e58d624

File tree

1 file changed

+52
-63
lines changed

1 file changed

+52
-63
lines changed

custom_components/tahoma_extended/climate.py

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,10 @@ def __init__(
184184

185185
def update(self):
186186
"""Update method."""
187-
from time import sleep
188187
self.controller.get_states([self.tahoma_device])
189188
sensor_state = self.hass.states.get(self.sensor_entity_id)
190189
if sensor_state and sensor_state.state != STATE_UNKNOWN:
191-
self._async_update_temp(sensor_state)
190+
self.update_temp(sensor_state)
192191
if self._type == "io":
193192
state = self.tahoma_device.active_states["io:TargetHeatingLevelState"]
194193
if state == "off":
@@ -268,6 +267,15 @@ def current_temperature(self):
268267
"""Return the sensor temperature."""
269268
return self._cur_temp
270269

270+
async def _async_sensor_changed(self, entity_id, old_state, new_state):
271+
"""Handle temperature changes."""
272+
if new_state is None:
273+
return
274+
275+
self.update_temp(new_state)
276+
self.control_heating()
277+
self.schedule_update_ha_state()
278+
271279
async def async_added_to_hass(self):
272280
await super().async_added_to_hass()
273281

@@ -280,51 +288,41 @@ def _async_startup(event):
280288
"""Init on startup."""
281289
sensor_state = self.hass.states.get(self.sensor_entity_id)
282290
if sensor_state and sensor_state.state != STATE_UNKNOWN:
283-
self._async_update_temp(sensor_state)
291+
self.update_temp(sensor_state)
284292

285293
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
286294

287-
async def _async_sensor_changed(self, entity_id, old_state, new_state):
288-
"""Handle temperature changes."""
289-
if new_state is None:
290-
return
291-
292-
self._async_update_temp(new_state)
293-
await self._async_control_heating()
294-
await self.async_update_ha_state()
295-
296295
@callback
297-
def _async_update_temp(self, state):
296+
def update_temp(self, state):
298297
"""Update thermostat with latest state from sensor."""
299298
try:
300299
self._cur_temp = float(state.state)
301300
except ValueError as ex:
302301
_LOGGER.error("Unable to update from sensor: %s", ex)
303302

304-
async def _async_control_heating(self):
303+
def control_heating(self):
305304
"""Check if we need to turn heating on or off."""
306-
async with self._temp_lock:
307-
if not self._active and None not in (self._cur_temp, self._target_temp):
308-
self._active = True
309-
_LOGGER.info(
310-
"Obtained current and target temperature. "
311-
"Thermostat active. %s, %s",
312-
self._cur_temp,
313-
self._target_temp,
314-
)
315-
316-
if not self._active:
317-
return
318-
319-
too_cold = self._target_temp - self._cur_temp >= self._cold_tolerance
320-
too_hot = self._cur_temp - self._target_temp >= self._hot_tolerance
321-
322-
if too_hot:
323-
_LOGGER.info("Turning off heater %s", self.name)
324-
await self._async_heater_turn_off()
325-
if too_cold:
326-
_LOGGER.info("Turning on heater %s", self.name)
327-
await self._async_heater_turn_on()
305+
if not self._active and None not in (self._cur_temp, self._target_temp):
306+
self._active = True
307+
_LOGGER.info(
308+
"Obtained current and target temperature. "
309+
"Thermostat active. %s, %s",
310+
self._cur_temp,
311+
self._target_temp,
312+
)
313+
314+
if not self._active:
315+
return
316+
317+
too_cold = self._target_temp - self._cur_temp >= self._cold_tolerance
318+
too_hot = self._cur_temp - self._target_temp >= self._hot_tolerance
319+
320+
if too_hot:
321+
_LOGGER.info("Turning off heater %s", self.name)
322+
self.heater_turn_off()
323+
if too_cold:
324+
_LOGGER.info("Turning on heater %s", self.name)
325+
self.heater_turn_on()
328326

329327
@property
330328
def _is_device_active(self):
@@ -338,61 +336,54 @@ def _apply_action(self, target_temperature):
338336
if target_temperature < 16:
339337
target_temperature = 16
340338
if self.tahoma_device.active_states['core:DerogatedTargetTemperatureState'] != target_temperature:
341-
from time import sleep
342339
self.apply_action([["setModeTemperature", "manualMode", target_temperature],
343340
["setDerogation", target_temperature, "further_notice"],
344341
["refreshState"]])
345-
sleep(20)
342+
from time import sleep
343+
sleep(5)
346344

347-
async def _async_heater_turn_on(self):
345+
def heater_turn_on(self):
348346
"""Turn heater toggleable device on."""
349347
if self._type == "io":
350348
self.apply_action([["setHeatingLevel", "comfort"]])
351349
elif self._type == "thermostat":
352350
self._apply_action(self.target_temperature)
353351
self._current_hvac_mode = CURRENT_HVAC_HEAT
354-
await self.async_update_ha_state()
355-
self._update_caller = "_async_heater_turn_on"
356-
self.update()
352+
self.schedule_update_ha_state()
357353

358-
async def _async_heater_turn_off(self):
354+
def heater_turn_off(self):
359355
"""Turn heater toggleable device off."""
360356
if self._type == "io":
361357
self.apply_action([["setHeatingLevel", "off"]])
362358
elif self._type == "thermostat":
363359
self._apply_action(self.target_temperature)
364360
self._current_hvac_mode = CURRENT_HVAC_IDLE
365-
await self.async_update_ha_state()
366-
self._update_caller = "_async_heater_turn_off"
367-
self.update()
361+
self.schedule_update_ha_state()
368362

369-
async def async_set_hvac_mode(self, hvac_mode):
363+
def set_hvac_mode(self, hvac_mode):
370364
"""Set hvac mode."""
371365
if hvac_mode == HVAC_MODE_HEAT:
372366
self._hvac_mode = HVAC_MODE_HEAT
373-
await self._async_control_heating()
367+
self.control_heating()
374368
elif hvac_mode == HVAC_MODE_OFF:
375369
self._hvac_mode = HVAC_MODE_OFF
376370
if self._is_device_active:
377-
await self._async_heater_turn_off()
371+
self.heater_turn_off()
378372
else:
379373
_LOGGER.error("Unrecognized hvac mode: %s", hvac_mode)
380374
return
381375
# Ensure we update the current operation after changing the mode
382376
self.schedule_update_ha_state()
383-
self._update_caller = "async_set_hvac_mode"
384-
self.update()
385377

386-
async def async_set_temperature(self, **kwargs):
378+
def set_temperature(self, **kwargs):
387379
"""Set new target temperature."""
388380
temperature = kwargs.get(ATTR_TEMPERATURE)
389381
if temperature is None:
390382
return
391383
self._target_temp = temperature
392-
await self._async_control_heating()
393-
# self.update()
384+
self.control_heating()
394385

395-
async def async_set_preset_mode(self, preset_mode: str) -> None:
386+
def set_preset_mode(self, preset_mode: str) -> None:
396387
"""Set new preset mode.
397388
398389
This method must be run in the event loop and returns a coroutine.
@@ -406,32 +397,30 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
406397
self._preset_mode = PRESET_AWAY
407398
self._saved_target_temp = self._target_temp
408399
self._target_temp = self._away_temp
409-
await self._async_control_heating()
400+
self.control_heating()
410401
elif preset_mode == PRESET_ECO and not self._preset_mode == PRESET_ECO:
411402
self._preset_mode = PRESET_ECO
412403
self._saved_target_temp = self._target_temp
413404
self._target_temp = self._eco_temp
414-
await self._async_control_heating()
405+
self.control_heating()
415406
elif preset_mode == PRESET_COMFORT and not self._preset_mode == PRESET_COMFORT:
416407
self._preset_mode = PRESET_COMFORT
417408
self._saved_target_temp = self._target_temp
418409
self._target_temp = self._comfort_temp
419-
await self._async_control_heating()
410+
self.control_heating()
420411
elif (
421412
preset_mode == PRESET_ANTI_FREEZE
422413
and not self._preset_mode == PRESET_ANTI_FREEZE
423414
):
424415
self._preset_mode = PRESET_ANTI_FREEZE
425416
self._saved_target_temp = self._target_temp
426417
self._target_temp = self._anti_freeze_temp
427-
await self._async_control_heating()
418+
self.control_heating()
428419
elif preset_mode == PRESET_NONE and not self._preset_mode == PRESET_NONE:
429420
self._preset_mode = PRESET_NONE
430421
self._target_temp = self._saved_target_temp
431-
await self._async_control_heating()
432-
await self.async_update_ha_state()
433-
self._update_caller = "async_set_preset_mode"
434-
self.update()
422+
self.control_heating()
423+
self.schedule_update_ha_state()
435424

436425
@property
437426
def device_state_attributes(self):

0 commit comments

Comments
 (0)