Skip to content

Commit

Permalink
Fix error creating/updating cover with a not valid position (legacy c…
Browse files Browse the repository at this point in the history
…overs).
  • Loading branch information
gicamm committed Sep 21, 2022
1 parent af5d581 commit b4622a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
12 changes: 7 additions & 5 deletions custom_components/comelit/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self, id, description, closed, position, hub):
ComelitDevice.__init__(self, id, None, description)
self._state = closed
self._hub = hub
self._position = position
if position != -1:
self._position = position

@property
def is_closed(self):
Expand Down Expand Up @@ -58,10 +59,11 @@ def close_cover(self, **kwargs):
def update_state(self, state, position):
super().update_state(state)

old = self._position
self._position = position
if old != position:
self.async_schedule_update_ha_state()
if position != -1:
old = self._position
self._position = position
if old != position:
self.async_schedule_update_ha_state()

def stop_cover(self, **kwargs):
_LOGGER.debug(f"Trying to STOP cover {self.name}! is_opening={self.is_opening}, is_closing={self.is_closing}")
Expand Down
31 changes: 18 additions & 13 deletions custom_components/comelit/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import paho.mqtt.client as mqtt
from threading import Thread

from homeassistant.const import STATE_CLOSED, STATE_OPEN, STATE_CLOSING, STATE_OPENING, STATE_ON, STATE_OFF
from homeassistant.const import STATE_CLOSED, STATE_OPEN, STATE_CLOSING, STATE_OPENING, STATE_ON, STATE_OFF, \
STATE_UNKNOWN

from .scene import ComelitScenario
from .sensor import PowerSensor, TemperatureSensor, HumiditySensor
Expand Down Expand Up @@ -321,19 +322,23 @@ def update_light(self, id, description, data):

def update_cover(self, id, description, data, status_key):
try:
if data['status'] == '0':
# Not moving
if data['open_status'] == '1':
state = STATE_OPEN
else:
state = STATE_CLOSED
elif data['status'] == '1':
state = STATE_OPENING
elif data['status'] == '2':
state = STATE_CLOSING
if 'position' in data:
if data['status'] == '0':
# Not moving
if data['open_status'] == '1':
state = STATE_OPEN
else:
state = STATE_CLOSED
elif data['status'] == '1':
state = STATE_OPENING
elif data['status'] == '2':
state = STATE_CLOSING

position = int(100 * float(data['position']) / 255)
else: # unable to define the position. works for legacy cover
state = STATE_UNKNOWN
position = -1

position = int(100*float(data['position'])/255)

if id not in self.covers: # Add the new cover
if hasattr(self, 'cover_add_entities'):
cover = ComelitCover(id, description, state, position, CommandHub(self))
Expand Down

0 comments on commit b4622a3

Please sign in to comment.