Compare commits

..

2 Commits

Author SHA1 Message Date
Jean-Marc Collin
3d977c4981 Failure to initialize #39 2023-02-01 10:43:21 +01:00
Jean-Marc Collin
115df52f67 Issue#36 - Error at initialisation
Issue#32 - Error with Fan Modes on thermostat over climate type
2023-01-31 22:19:43 +01:00
4 changed files with 176 additions and 45 deletions

View File

@@ -34,17 +34,15 @@ from homeassistant.helpers import (
entity_platform, entity_platform,
) # , config_validation as cv ) # , config_validation as cv
from homeassistant.components.climate.const import ( from homeassistant.components.climate import (
DOMAIN as CLIMATE_DOMAIN, DOMAIN as CLIMATE_DOMAIN,
ATTR_PRESET_MODE, ATTR_PRESET_MODE,
# ATTR_FAN_MODE, # ATTR_FAN_MODE,
CURRENT_HVAC_COOL, HVACMode,
CURRENT_HVAC_HEAT, HVACAction,
CURRENT_HVAC_IDLE, # HVAC_MODE_COOL,
CURRENT_HVAC_OFF, # HVAC_MODE_HEAT,
HVAC_MODE_COOL, # HVAC_MODE_OFF,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_ACTIVITY, PRESET_ACTIVITY,
# PRESET_AWAY, # PRESET_AWAY,
PRESET_BOOST, PRESET_BOOST,
@@ -53,7 +51,8 @@ from homeassistant.components.climate.const import (
# PRESET_HOME, # PRESET_HOME,
PRESET_NONE, PRESET_NONE,
# PRESET_SLEEP, # PRESET_SLEEP,
SUPPORT_PRESET_MODE, ClimateEntityFeature,
# ClimateEntityFeature.PRESET_MODE,
# SUPPORT_TARGET_TEMPERATURE, # SUPPORT_TARGET_TEMPERATURE,
SERVICE_SET_FAN_MODE, SERVICE_SET_FAN_MODE,
SERVICE_SET_HUMIDITY, SERVICE_SET_HUMIDITY,
@@ -63,6 +62,13 @@ from homeassistant.components.climate.const import (
SERVICE_SET_TEMPERATURE, SERVICE_SET_TEMPERATURE,
) )
# from homeassistant.components.climate import (
# CURRENT_HVAC_HEAT,
# HVACAction.IDLE,
# HVACAction.OFF,
# HVACAction.COOLING,
# )
from homeassistant.const import ( from homeassistant.const import (
# UnitOfTemperature, # UnitOfTemperature,
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
@@ -220,6 +226,8 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
self._is_over_climate = False self._is_over_climate = False
self._underlying_climate = None self._underlying_climate = None
self._attr_translation_key = "versatile_thermostat"
self.post_init(entry_infos) self.post_init(entry_infos)
def post_init(self, entry_infos): def post_init(self, entry_infos):
@@ -297,7 +305,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
# TODO if self.ac_mode: # TODO if self.ac_mode:
# self.hvac_list = [HVAC_MODE_COOL, HVAC_MODE_OFF] # self.hvac_list = [HVAC_MODE_COOL, HVAC_MODE_OFF]
# else: # else:
self._hvac_list = [HVAC_MODE_HEAT, HVAC_MODE_OFF] self._hvac_list = [HVACMode.HEAT, HVACMode.OFF]
self._unit = UnitOfTemperature.CELSIUS self._unit = UnitOfTemperature.CELSIUS
# Will be restored if possible # Will be restored if possible
self._hvac_mode = None # HVAC_MODE_OFF self._hvac_mode = None # HVAC_MODE_OFF
@@ -383,7 +391,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
# Calculate all possible presets # Calculate all possible presets
self._attr_preset_modes = [PRESET_NONE] self._attr_preset_modes = [PRESET_NONE]
if len(presets): if len(presets):
self._support_flags = SUPPORT_FLAGS | SUPPORT_PRESET_MODE self._support_flags = SUPPORT_FLAGS | ClimateEntityFeature.PRESET_MODE
for key, val in presets.items(): for key, val in presets.items():
if val != 0.0: if val != 0.0:
@@ -741,7 +749,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
# Set default state to off # Set default state to off
if not self._hvac_mode: if not self._hvac_mode:
self._hvac_mode = HVAC_MODE_OFF self._hvac_mode = HVACMode.OFF
_LOGGER.info( _LOGGER.info(
"%s - restored state is target_temp=%.1f, preset_mode=%s, hvac_mode=%s", "%s - restored state is target_temp=%.1f, preset_mode=%s, hvac_mode=%s",
@@ -840,13 +848,13 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
if self._is_over_climate and self._underlying_climate: if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.hvac_action return self._underlying_climate.hvac_action
if self._hvac_mode == HVAC_MODE_OFF: if self._hvac_mode == HVACMode.OFF:
return CURRENT_HVAC_OFF return HVACAction.OFF
if not self._is_device_active: if not self._is_device_active:
return CURRENT_HVAC_IDLE return HVACAction.IDLE
if self._ac_mode: if self._ac_mode:
return CURRENT_HVAC_COOL return HVACAction.COOLING
return CURRENT_HVAC_HEAT return HVACAction.HEATING
@property @property
def target_temperature(self): def target_temperature(self):
@@ -864,11 +872,14 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
@property @property
def _is_device_active(self): def _is_device_active(self):
"""If the toggleable device is currently active.""" """If the toggleable device is currently active."""
if self._is_over_climate and self._underlying_climate: if self._is_over_climate:
return self._underlying_climate.hvac_action not in [ if self._underlying_climate:
CURRENT_HVAC_IDLE, return self._underlying_climate.hvac_action not in [
CURRENT_HVAC_OFF, HVACAction.IDLE,
] HVACAction.OFF,
]
else:
return None
else: else:
return self.hass.states.is_state(self._heater_entity_id, STATE_ON) return self.hass.states.is_state(self._heater_entity_id, STATE_ON)
@@ -877,10 +888,98 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
"""Return the sensor temperature.""" """Return the sensor temperature."""
return self._cur_temp return self._cur_temp
@property
def target_temperature_step(self) -> float | None:
"""Return the supported step of target temperature."""
if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.target_temperature_step
return None
@property
def target_temperature_high(self) -> float | None:
"""Return the highbound target temperature we try to reach.
Requires ClimateEntityFeature.TARGET_TEMPERATURE_RANGE.
"""
if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.target_temperature_high
return None
@property
def target_temperature_low(self) -> float | None:
"""Return the lowbound target temperature we try to reach.
Requires ClimateEntityFeature.TARGET_TEMPERATURE_RANGE.
"""
if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.target_temperature_low
return None
@property
def is_aux_heat(self) -> bool | None:
"""Return true if aux heater.
Requires ClimateEntityFeature.AUX_HEAT.
"""
if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.is_aux_heat
return None
def turn_aux_heat_on(self) -> None:
"""Turn auxiliary heater on."""
if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.turn_aux_heat_on()
raise NotImplementedError()
async def async_turn_aux_heat_on(self) -> None:
"""Turn auxiliary heater on."""
if self._is_over_climate and self._underlying_climate:
await self._underlying_climate.async_turn_aux_heat_on()
raise NotImplementedError()
def turn_aux_heat_off(self) -> None:
"""Turn auxiliary heater off."""
if self._is_over_climate and self._underlying_climate:
return self._underlying_climate.turn_aux_heat_off()
raise NotImplementedError()
async def async_turn_aux_heat_off(self) -> None:
"""Turn auxiliary heater off."""
if self._is_over_climate and self._underlying_climate:
await self._underlying_climate.async_turn_aux_heat_off()
raise NotImplementedError()
@property
def preset_mode(self) -> str | None:
"""Return the current preset mode, e.g., home, away, temp.
Requires ClimateEntityFeature.PRESET_MODE.
"""
return self._attr_preset_mode
@property
def preset_modes(self) -> list[str] | None:
"""Return a list of available preset modes.
Requires ClimateEntityFeature.PRESET_MODE.
"""
return self._attr_preset_modes
async def async_set_hvac_mode(self, hvac_mode): async def async_set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode.""" """Set new target hvac mode."""
_LOGGER.info("%s - Set hvac mode: %s", self, hvac_mode) _LOGGER.info("%s - Set hvac mode: %s", self, hvac_mode)
if hvac_mode is None:
return
if self._is_over_climate and self._underlying_climate: if self._is_over_climate and self._underlying_climate:
data = {ATTR_ENTITY_ID: self._climate_entity_id, "hvac_mode": hvac_mode} data = {ATTR_ENTITY_ID: self._climate_entity_id, "hvac_mode": hvac_mode}
await self.hass.services.async_call( await self.hass.services.async_call(
@@ -889,14 +988,14 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
# await self._underlying_climate.async_set_hvac_mode(hvac_mode) # await self._underlying_climate.async_set_hvac_mode(hvac_mode)
self._hvac_mode = hvac_mode # self._underlying_climate.hvac_mode self._hvac_mode = hvac_mode # self._underlying_climate.hvac_mode
else: else:
if hvac_mode == HVAC_MODE_HEAT: if hvac_mode == HVACMode.HEAT:
self._hvac_mode = HVAC_MODE_HEAT self._hvac_mode = HVACMode.HEAT
await self._async_control_heating(force=True) await self._async_control_heating(force=True)
elif hvac_mode == HVAC_MODE_COOL: elif hvac_mode == HVACMode.COOL:
self._hvac_mode = HVAC_MODE_COOL self._hvac_mode = HVACMode.COOL
await self._async_control_heating(force=True) await self._async_control_heating(force=True)
elif hvac_mode == HVAC_MODE_OFF: elif hvac_mode == HVACMode.OFF:
self._hvac_mode = HVAC_MODE_OFF self._hvac_mode = HVACMode.OFF
if self._is_device_active: if self._is_device_active:
await self._async_underlying_entity_turn_off() await self._async_underlying_entity_turn_off()
await self._async_control_heating(force=True) await self._async_control_heating(force=True)
@@ -1127,10 +1226,10 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
await self.restore_hvac_mode() await self.restore_hvac_mode()
elif self._window_state == STATE_ON: elif self._window_state == STATE_ON:
_LOGGER.info( _LOGGER.info(
"%s - Window is open. Set hvac_mode to '%s'", self, HVAC_MODE_OFF "%s - Window is open. Set hvac_mode to '%s'", self, HVACMode.OFF
) )
self.save_hvac_mode() self.save_hvac_mode()
await self.async_set_hvac_mode(HVAC_MODE_OFF) await self.async_set_hvac_mode(HVACMode.OFF)
self.update_custom_attributes() self.update_custom_attributes()
if self._window_call_cancel: if self._window_call_cancel:
@@ -1202,7 +1301,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
async def _check_switch_initial_state(self): async def _check_switch_initial_state(self):
"""Prevent the device from keep running if HVAC_MODE_OFF.""" """Prevent the device from keep running if HVAC_MODE_OFF."""
_LOGGER.debug("%s - Calling _check_switch_initial_state", self) _LOGGER.debug("%s - Calling _check_switch_initial_state", self)
if self._hvac_mode == HVAC_MODE_OFF and self._is_device_active: if self._hvac_mode == HVACMode.OFF and self._is_device_active:
_LOGGER.warning( _LOGGER.warning(
"The climate mode is OFF, but the switch device is ON. Turning off device %s", "The climate mode is OFF, but the switch device is ON. Turning off device %s",
self._heater_entity_id, self._heater_entity_id,
@@ -1232,9 +1331,9 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
) )
# old_state = event.data.get("old_state") # old_state = event.data.get("old_state")
if new_state is None or new_state.state not in [ if new_state is None or new_state.state not in [
HVAC_MODE_COOL, HVACMode.OFF,
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_OFF, HVACMode.COOL,
]: ]:
return return
self._hvac_mode = new_state.state self._hvac_mode = new_state.state
@@ -1496,11 +1595,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
self._device_power, self._device_power,
) )
ret = self._current_power + self._device_power >= self._current_power_max ret = self._current_power + self._device_power >= self._current_power_max
if ( if not self._overpowering_state and ret and not self._hvac_mode == HVACMode.OFF:
not self._overpowering_state
and ret
and not self._hvac_mode == HVAC_MODE_OFF
):
_LOGGER.warning( _LOGGER.warning(
"%s - overpowering is detected. Heater preset will be set to 'power'", "%s - overpowering is detected. Heater preset will be set to 'power'",
self, self,
@@ -1548,8 +1643,8 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
or delta_ext_temp > self._security_delay_min or delta_ext_temp > self._security_delay_min
) )
climate_cond: bool = self._is_over_climate and self.hvac_action not in [ climate_cond: bool = self._is_over_climate and self.hvac_action not in [
CURRENT_HVAC_COOL, HVACAction.COOLING,
CURRENT_HVAC_IDLE, HVACAction.IDLE,
] ]
switch_cond: bool = ( switch_cond: bool = (
not self._is_over_climate not self._is_over_climate
@@ -1587,7 +1682,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
self.save_hvac_mode() self.save_hvac_mode()
self.save_preset_mode() self.save_preset_mode()
await self._async_set_preset_mode_internal(PRESET_SECURITY) await self._async_set_preset_mode_internal(PRESET_SECURITY)
await self.async_set_hvac_mode(HVAC_MODE_OFF) await self.async_set_hvac_mode(HVACMode.OFF)
if ( if (
self._security_state self._security_state
@@ -1629,7 +1724,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
return return
# Stop here if we are off # Stop here if we are off
if self._hvac_mode == HVAC_MODE_OFF: if self._hvac_mode == HVACMode.OFF:
_LOGGER.debug("%s - End of cycle (HVAC_MODE_OFF)", self) _LOGGER.debug("%s - End of cycle (HVAC_MODE_OFF)", self)
if self._is_device_active: if self._is_device_active:
await self._async_underlying_entity_turn_off() await self._async_underlying_entity_turn_off()
@@ -1662,7 +1757,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
_LOGGER.debug("%s - End of cycle (2)", self) _LOGGER.debug("%s - End of cycle (2)", self)
return return
if self._hvac_mode == HVAC_MODE_HEAT and on_time_sec > 0: if self._hvac_mode == HVACMode.HEAT and on_time_sec > 0:
async def _turn_on_off_later( async def _turn_on_off_later(
on: bool, time, heater_action, next_cycle_action on: bool, time, heater_action, next_cycle_action
@@ -1672,7 +1767,7 @@ class VersatileThermostat(ClimateEntity, RestoreEntity):
self._async_cancel_cycle = None self._async_cancel_cycle = None
_LOGGER.debug("%s - Stopping cycle during calculation", self) _LOGGER.debug("%s - Stopping cycle during calculation", self)
if self._hvac_mode == HVAC_MODE_OFF: if self._hvac_mode == HVACMode.OFF:
_LOGGER.debug("%s - End of cycle (HVAC_MODE_OFF - 2)", self) _LOGGER.debug("%s - End of cycle (HVAC_MODE_OFF - 2)", self)
if self._is_device_active: if self._is_device_active:
await self._async_underlying_entity_turn_off() await self._async_underlying_entity_turn_off()

View File

@@ -209,5 +209,17 @@
"thermostat_over_climate": "Thermostat over another thermostat" "thermostat_over_climate": "Thermostat over another thermostat"
} }
} }
},
"entity": {
"climate": {
"versatile_thermostat": {
"states_attributes": {
"preset_mode": {
"power": "Shedding",
"security": "Security"
}
}
}
}
} }
} }

View File

@@ -209,5 +209,17 @@
"thermostat_over_climate": "Thermostat over another thermostat" "thermostat_over_climate": "Thermostat over another thermostat"
} }
} }
},
"entity": {
"climate": {
"versatile_thermostat": {
"states_attributes": {
"preset_mode": {
"power": "Shedding",
"security": "Security"
}
}
}
}
} }
} }

View File

@@ -209,5 +209,17 @@
"thermostat_over_climate": "Thermostat sur un autre thermostat" "thermostat_over_climate": "Thermostat sur un autre thermostat"
} }
} }
},
"entity": {
"climate": {
"versatile_thermostat": {
"states_attributes": {
"preset_mode": {
"power": "Délestage",
"security": "Sécurité"
}
}
}
}
} }
} }