Skip to content

Commit

Permalink
Update states immediately, without waiting for update interval. I'd s…
Browse files Browse the repository at this point in the history
…omehow removed this from lights in particular.
  • Loading branch information
optiluca committed Dec 28, 2022
1 parent 0020057 commit 2651269
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions custom_components/comelit/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ 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()

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
Expand Down
1 change: 1 addition & 0 deletions custom_components/comelit/comelit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def available(self):
return self._is_available

def update_state(self, state):
#TODO optiluca: make this more general and use it for all derived classes
old = self._state
self._state = state
if old != state:
Expand Down
16 changes: 10 additions & 6 deletions custom_components/comelit/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ def set_cover_position(self, position, **kwargs):
_LOGGER.debug(f"Trying to SET POSITION {position} cover {self.name}! _state={self._state}")
self._hub.cover_position(self._id, position)

def open_cover(self, **kwargs):
def open_cover(self, stopping=False, **kwargs):
_LOGGER.debug(f"Trying to OPEN cover {self.name}! _state={self._state}")
self._hub.cover_up(self._id)
# self._state == STATE_OPENING
if not stopping:
self._state == STATE_OPENING
self.async_schedule_update_ha_state()

def close_cover(self, **kwargs):
def close_cover(self, stopping=False, **kwargs):
_LOGGER.debug(f"Trying to CLOSE cover {self.name}! _state={self._state}")
self._hub.cover_down(self._id)
# self._state = STATE_CLOSING
if not stopping:
self._state = STATE_CLOSING
self.async_schedule_update_ha_state()

def update_state(self, state, position):
super().update_state(state)
Expand All @@ -68,6 +72,6 @@ def update_state(self, state, position):
def stop_cover(self, **kwargs):
_LOGGER.debug(f"Trying to STOP cover {self.name}! is_opening={self.is_opening}, is_closing={self.is_closing}")
if self.is_opening:
self.close_cover()
self.close_cover(stopping=True)
elif self.is_closing:
self.open_cover()
self.open_cover(stopping=True)
19 changes: 14 additions & 5 deletions custom_components/comelit/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def update_light(self, id, description, data):
self.lights[id] = light
_LOGGER.info("added the light %s %s", description, light.entity_name)
else:
_LOGGER.debug("updating the light %s %s", description, light.entity_name)
_LOGGER.debug(f"updating the light {description} {light.entity_name} with state {state}")
self.lights[id].update_state(state)
except Exception as e:
_LOGGER.exception("Error updating light %s", e)
Expand Down Expand Up @@ -495,6 +495,13 @@ def status(self, payload):
_LOGGER.error("Status error")
_LOGGER.error(e)

def update_status(hub):
try:
req = {"req_type": RequestType.STATUS, "req_sub_type": -1, "obj_id": "GEN#17#13#1", "detail_level": 1}
hub.publish(req)
except Exception as e:
_LOGGER.error("Error updating status")
_LOGGER.error(e)

# Make a request for status
class StatusUpdater (Thread):
Expand All @@ -505,19 +512,21 @@ def __init__(self, name, scan_interval, hub):
self.hub = hub

def run(self):
parameters_timer = 0
# parameters_timer = 0
_LOGGER.debug("Comelit Hub status snapshot started")
while True:
if self.hub.sessiontoken == "":
continue

# optiluca: does not do anything?
'''
if parameters_timer == 0:
{"req_type": 8, "seq_id": 5, "req_sub_type": 23, "param_type": 2, "agent_type": 0,
"sessiontoken": "1367343208"}
parameters_timer = 30
'''

req = {"req_type": RequestType.STATUS, "req_sub_type": -1, "obj_id": "GEN#17#13#1", "detail_level": 1}
self.hub.publish(req)
update_status(self.hub)
time.sleep(self._scan_interval)
parameters_timer = parameters_timer - 1
# parameters_timer = parameters_timer - 1

7 changes: 6 additions & 1 deletion custom_components/comelit/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Import the device class from the component that you want to support
from homeassistant.components.light import (
ATTR_BRIGHTNESS, PLATFORM_SCHEMA, LightEntity, COLOR_MODE_BRIGHTNESS)
from homeassistant.const import STATE_ON
from homeassistant.const import STATE_ON, STATE_OFF

from .const import DOMAIN
from .comelit_device import ComelitDevice
Expand Down Expand Up @@ -50,6 +50,11 @@ def turn_on(self, **kwargs):
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
self._light.light_on(self._id, self._brightness)
self._state = STATE_ON # Immediately update the state, don't wait for the next update
self.schedule_update_ha_state()


def turn_off(self, **kwargs):
self._light.light_off(self._id)
self._state = STATE_OFF # Immediately update the state, don't wait for the next update
self.schedule_update_ha_state()

0 comments on commit 2651269

Please sign in to comment.