diff --git a/src/pyatmo/modules/module.py b/src/pyatmo/modules/module.py index 6fe8ef86..415821eb 100644 --- a/src/pyatmo/modules/module.py +++ b/src/pyatmo/modules/module.py @@ -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.""" @@ -414,11 +419,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, }, ], @@ -428,17 +438,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): diff --git a/tests/test_shutter.py b/tests/test_shutter.py index b09d5526..f1a26ee3 100644 --- a/tests/test_shutter.py +++ b/tests/test_shutter.py @@ -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),