Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preferred position functionnality to shutter #499

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/pyatmo/modules/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ async def async_set_fan_speed(self, speed: int) -> bool:
class ShutterMixin(EntityBase):
"""Mixin for shutter data."""

__open_position = 100
__close_position = 0
__stop_position = -1
__preferred_position = -2

def __init__(self, home: Home, module: ModuleT):
"""Initialize shutter mixin."""

Expand All @@ -413,11 +418,16 @@ def __init__(self, home: Home, module: ModuleT):
async def async_set_target_position(self, target_position: int) -> bool:
"""Set shutter to target position."""

# in case of a too low value, we default to stop and not the preferred position
# We check against __preferred_position that is the lower known value
if target_position < self.__preferred_position:
target_position = self.__stop_position

json_roller_shutter = {
"modules": [
{
"id": self.entity_id,
"target_position": max(min(100, target_position), -1),
"target_position": min(self.__open_position, target_position),
"bridge": self.bridge,
},
],
Expand All @@ -427,17 +437,22 @@ async def async_set_target_position(self, target_position: int) -> bool:
async def async_open(self) -> bool:
"""Open shutter."""

return await self.async_set_target_position(100)
return await self.async_set_target_position(self.__open_position)

async def async_close(self) -> bool:
"""Close shutter."""

return await self.async_set_target_position(0)
return await self.async_set_target_position(self.__close_position)

async def async_stop(self) -> bool:
"""Stop shutter."""

return await self.async_set_target_position(-1)
return await self.async_set_target_position(self.__stop_position)

async def async_move_to_preferred_position(self) -> bool:
"""Move shutter to preferred position."""

return await self.async_set_target_position(self.__preferred_position)


class CameraMixin(EntityBase):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_shutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def gen_json_data(position):
endpoint="api/setstate",
)

assert await module.async_move_to_preferred_position()
mock_resp.assert_awaited_with(
params=gen_json_data(-2),
endpoint="api/setstate",
)

assert await module.async_set_target_position(47)
mock_resp.assert_awaited_with(
params=gen_json_data(47),
Expand Down