Skip to content

Commit

Permalink
Clean up climate implementation. Correctly discern between "off" and …
Browse files Browse the repository at this point in the history
…"idle."
  • Loading branch information
optiluca committed Jan 7, 2023
1 parent 2651269 commit 5f97100
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
43 changes: 21 additions & 22 deletions custom_components/comelit/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from homeassistant.const import (
ATTR_TEMPERATURE,
TEMP_CELSIUS,
STATE_OFF,
STATE_IDLE,
STATE_ON
)

from .const import DOMAIN
Expand All @@ -26,44 +29,41 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
_LOGGER.info("Comelit Climate Integration started")

class ComelitClimate(ComelitDevice, ClimateEntity):
def __init__(self, id, description, state, measured_temperature, target_temperature, measured_humidity, hub):
def __init__(self, id, description, state_dict, hub):
ComelitDevice.__init__(self, id, "climate", description)
self._hub = hub
self._state = state
self._temperature = measured_temperature
self._target_temperature = target_temperature
self._humidity = measured_humidity
self._state = state_dict

@property
def hvac_mode(self):
return HVACMode.HEAT if self._state else HVACMode.OFF
return HVACMode.HEAT if self._state['is_heating'] else HVACMode.OFF

@property
def hvac_modes(self):
return [HVACMode.HEAT, HVACMode.OFF]

@property
def hvac_action(self):
if self._state:
if self._state['is_heating']:
return HVACAction.HEATING
else:
return HVACAction.IDLE if self._target_temperature else HVACAction.OFF
return HVACAction.IDLE if self._state['is_enabled'] else HVACAction.OFF

@property
def target_temperature(self):
return self._target_temperature
return self._state['target_temperature']

@property
def temperature_unit(self):
return TEMP_CELSIUS

@property
def current_temperature(self):
return self._temperature
return self._state['measured_temperature']

@property
def current_humidity(self):
return self._humidity
return self._state['measured_humidity']

@property
def supported_features(self):
Expand All @@ -73,18 +73,17 @@ def set_temperature(self, **kwargs):
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is not None:
self._hub.climate_set_temperature(self._id, temperature)
self.schedule_update_ha_state()
self._state['target_temperature'] = temperature
self.async_schedule_update_ha_state()

def set_hvac_mode(self, hvac_mode):
self._hub.climate_set_state(self._id, hvac_mode == HVACMode.HEAT)
self.schedule_update_ha_state()

def update_state(self, state, temperature, target_temperature, humidity):
self._state = state
self._target_temperature = target_temperature
self._temperature = temperature
self._humidity = humidity
self.async_write_ha_state()

self.async_schedule_update_ha_state()

def update(self):
pass
pass

@property
def state(self):
state_mapping = {HVACAction.HEATING: STATE_ON, HVACAction.IDLE: STATE_IDLE, HVACAction.OFF: STATE_OFF}
return state_mapping[self.hvac_action]
13 changes: 10 additions & 3 deletions custom_components/comelit/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,16 @@ def update_climate(self, id, description, data):

measured_humidity = float(data[HubFields.HUMIDITY])

state = bool(int(data[HubFields.STATUS]))
is_enabled = int(data['auto_man']) == 2
is_heating = bool(int(data[HubFields.STATUS]))

state_dict = {'is_enabled': is_enabled,
'is_heating': is_heating,
'measured_temperature': measured_temp,
'target_temperature': target,
'measured_humidity': measured_humidity}

climate = ComelitClimate(id, description, state, measured_temp, target, measured_humidity, CommandHub(self))
climate = ComelitClimate(id, description, state_dict, CommandHub(self))

name = climate.entity_name
if climate.name not in self.climates: # Add the new sensor
Expand All @@ -345,7 +352,7 @@ def update_climate(self, id, description, data):
self.climates[name] = climate
_LOGGER.info("added the climate %s", name)
else:
self.climates[name].update_state(state, measured_temp, target, measured_humidity)
self.climates[name].update_state(state_dict)
_LOGGER.debug("updated the climate %s", name)
except Exception as e:
_LOGGER.exception("Error updating climate %s", e)
Expand Down

0 comments on commit 5f97100

Please sign in to comment.