Skip to content

Commit

Permalink
Estimate cover moving direction correctly.
Browse files Browse the repository at this point in the history
Fixes #232 and #223
  • Loading branch information
tschamm committed Feb 1, 2025
1 parent 626e048 commit 78679dd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
44 changes: 41 additions & 3 deletions custom_components/bosch_shc/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ class ShutterControlCover(SHCEntity, CoverEntity):
| CoverEntityFeature.STOP
| CoverEntityFeature.SET_POSITION
)
_current_operation_state = None
_l1_position = _l2_position = None

def _update_attr(self) -> None:
"""Recomputes the attributes values either at init or when the device state changes."""
self._attr_current_cover_position = round(self._device.level * 100.0)
self._current_operation_state = self._device.operation_state

if self._l1_position is None:
self._l2_position = self._l1_position = self._attr_current_cover_position

# calculate movement direction between current cover position and second last reported position
if (
self._current_operation_state
== SHCShutterControl.ShutterControlService.State.MOVING
):
if self._l2_position > self._attr_current_cover_position:
self._attr_is_closing = True
else:
self._attr_is_opening = True

if (
self._current_operation_state
== SHCShutterControl.ShutterControlService.State.STOPPED
):
self._attr_is_closing = False
self._attr_is_opening = False

self._l2_position = self._l1_position
self._l1_position = self._attr_current_cover_position

@property
def device_class(self) -> CoverDeviceClass | None:
Expand All @@ -84,20 +114,28 @@ def current_cover_position(self):

def stop_cover(self, **kwargs):
"""Stop the cover."""
self._attr_is_opening = False
self._attr_is_closing = False
self._device.stop()

@property
def is_closed(self):
"""Return if the cover is closed or not."""
return self.current_cover_position == 0
return self._l2_position == 0

def open_cover(self, **kwargs):
"""Open the cover."""
self._device.level = 1.0
# opens effectively only if cover is not already opening and not fully opened
if not self._attr_is_opening and self.current_cover_position != 100:
self._attr_is_opening = True
self._device.level = 1.0

def close_cover(self, **kwargs):
"""Close cover."""
self._device.level = 0.0
# closes effectively only if cover is not already closing and not fully closed
if not self._attr_is_closing and self.current_cover_position != 0:
self._attr_is_closing = True
self._device.level = 0.0

def set_cover_position(self, **kwargs):
"""Move the cover to a specific position."""
Expand Down
6 changes: 6 additions & 0 deletions custom_components/bosch_shc/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,24 @@ def __init__(self, device: SHCDevice, entry_id: str) -> None:
self._entry_id = entry_id
self._attr_name = f"{device.name}"
self._attr_unique_id = f"{device.root_device_id}_{device.id}"
self._update_attr()

def _update_attr(self) -> None:
pass

async def async_added_to_hass(self):
"""Subscribe to SHC events."""
await super().async_added_to_hass()

def on_state_changed():
self._update_attr()
self.schedule_update_ha_state()

def update_entity_information():
if self._device.deleted:
self.hass.add_job(async_remove_devices(self.hass, self, self._entry_id))
else:
self._update_attr()
self.schedule_update_ha_state()

for service in self._device.device_services:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bosch_shc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/tschamm/boschshc-hass/issues",
"requirements": ["boschshcpy==0.2.105"],
"version": "0.4.96",
"version": "0.4.97-dev0",
"zeroconf": [{ "type": "_http._tcp.local.", "name": "bosch shc*" }]
}

0 comments on commit 78679dd

Please sign in to comment.