Skip to content

Commit

Permalink
Refactor cover api (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ authored Sep 3, 2023
1 parent f062175 commit 59b353c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 2023.9.0 (2023-09-03)

- Refactor cover api

# Version 2023.8.14 (2023-08-30)

- Reduce visibility of local constants
Expand Down
36 changes: 18 additions & 18 deletions hahomematic/platforms/custom/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ def current_position(self) -> int:

@bind_collector
async def set_position(
self, position: float, collector: CallParameterCollector | None = None
self,
position: float | None = None,
tilt_position: float | None = None,
collector: CallParameterCollector | None = None,
) -> None:
"""Move the cover to a specific position."""
if not self.is_state_change(position=position):
return
level = min(100.0, max(0.0, position)) / 100.0
level = min(100.0, max(0.0, position)) / 100.0 if position is not None else None
await self._set_level(level=level, collector=collector)

async def _set_level(
Expand Down Expand Up @@ -244,27 +247,19 @@ def current_tilt_position(self) -> int:
return int(self._channel_tilt_level * 100)

@bind_collector
async def set_tilt_position(
self, tilt_position: float, collector: CallParameterCollector | None = None
) -> None:
"""Move the blind to a specific tilt position."""
if not self.is_state_change(tilt_position=tilt_position):
return
tilt_level = min(100.0, max(0.0, tilt_position)) / 100.0
await self._set_level(tilt_level=tilt_level, collector=collector)

@bind_collector
async def set_combined_position(
async def set_position(
self,
position: float,
tilt_position: float,
position: float | None = None,
tilt_position: float | None = None,
collector: CallParameterCollector | None = None,
) -> None:
"""Move the blind to a specific position."""
if not self.is_state_change(position=position, tilt_position=tilt_position):
return
level = min(100.0, max(0.0, position)) / 100.0
tilt_level = min(100.0, max(0.0, tilt_position)) / 100.0
level = min(100.0, max(0.0, position)) / 100.0 if position is not None else None
tilt_level = (
min(100.0, max(0.0, tilt_position)) / 100.0 if tilt_position is not None else None
)
await self._set_level(level=level, tilt_level=tilt_level, collector=collector)

async def _set_level(
Expand Down Expand Up @@ -428,9 +423,14 @@ def current_position(self) -> int | None:

@bind_collector
async def set_position(
self, position: float, collector: CallParameterCollector | None = None
self,
position: float | None = None,
tilt_position: float | None = None,
collector: CallParameterCollector | None = None,
) -> None:
"""Move the garage door to a specific position."""
if position is None:
return
if 50.0 < position <= 100.0:
await self.open(collector=collector)
if 10.0 < position <= 50.0:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hahomematic"
version = "2023.8.14"
version = "2023.9.0"
license = {text = "MIT License"}
description = "Homematic interface for Home Assistant running on Python 3."
readme = "README.md"
Expand Down
38 changes: 19 additions & 19 deletions tests/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def test_cecover(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover._channel_level == _HM_CLOSED
assert cover.is_closed is True
await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU8537918:4",
paramset_key="VALUES",
Expand Down Expand Up @@ -96,7 +96,7 @@ async def test_cecover(factory: helper.Factory) -> None:

central.event(const.INTERFACE_ID, "VCU8537918:3", "LEVEL", 0.4)
call_count = len(mock_client.method_calls)
await cover.set_position(40)
await cover.set_position(position=40)
assert call_count == len(mock_client.method_calls)


Expand All @@ -111,7 +111,7 @@ async def test_ceipblind_dr(factory: helper.Factory) -> None:
assert cover._channel_level == _HM_CLOSED
assert cover.channel_operation_mode == "SHUTTER"
assert cover.is_closed is True
await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU7807849:2",
paramset_key="VALUES",
Expand Down Expand Up @@ -162,7 +162,7 @@ async def test_cewindowdrive(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover._channel_level == _HM_WD_CLOSED
assert cover.is_closed is True
await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU0000350:1",
paramset_key="VALUES",
Expand Down Expand Up @@ -191,12 +191,12 @@ async def test_cewindowdrive(factory: helper.Factory) -> None:
assert cover._channel_level == _HM_WD_CLOSED
assert cover.is_closed is True

await cover.set_position(1)
await cover.set_position(position=1)
assert cover.current_position == 1
assert cover._channel_level == _HM_CLOSED
assert cover.is_closed is False

await cover.set_position(0.0)
await cover.set_position(position=0.0)
assert cover.current_position == 0
assert cover._channel_level == _HM_WD_CLOSED
assert cover.is_closed is True
Expand All @@ -211,7 +211,7 @@ async def test_ceblind(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover.current_tilt_position == 0

await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU0000145:1",
paramset_key="VALUES",
Expand Down Expand Up @@ -255,7 +255,7 @@ async def test_ceblind(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover.current_tilt_position == 100

await cover.set_tilt_position(45)
await cover.set_position(tilt_position=45)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU0000145:1",
paramset_key="VALUES",
Expand All @@ -277,7 +277,7 @@ async def test_ceblind(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover.current_tilt_position == 0

await cover.set_combined_position(position=10, tilt_position=20)
await cover.set_position(position=10, tilt_position=20)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU0000145:1",
paramset_key="VALUES",
Expand Down Expand Up @@ -320,7 +320,7 @@ async def test_ceblind(factory: helper.Factory) -> None:

central.event(const.INTERFACE_ID, "VCU0000145:1", "LEVEL_SLATS", 0.4)
call_count = len(mock_client.method_calls)
await cover.set_tilt_position(40)
await cover.set_position(tilt_position=40)
assert call_count == len(mock_client.method_calls)


Expand All @@ -333,7 +333,7 @@ async def test_ceipblind(factory: helper.Factory) -> None:

assert cover.current_position == 0
assert cover.current_tilt_position == 0
await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU1223813:4",
paramset_key="VALUES",
Expand Down Expand Up @@ -379,7 +379,7 @@ async def test_ceipblind(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover.current_tilt_position == 100

await cover.set_tilt_position(45)
await cover.set_position(tilt_position=45)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU1223813:4",
paramset_key="VALUES",
Expand All @@ -402,7 +402,7 @@ async def test_ceipblind(factory: helper.Factory) -> None:
assert cover.current_position == 0
assert cover.current_tilt_position == 0

await cover.set_combined_position(position=10, tilt_position=20)
await cover.set_position(position=10, tilt_position=20)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU1223813:4",
paramset_key="VALUES",
Expand Down Expand Up @@ -439,7 +439,7 @@ async def test_cegarageho(factory: helper.Factory) -> None:
assert cover.usage == HmEntityUsage.CE_PRIMARY

assert cover.current_position is None
await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU3574044:1",
paramset_key="VALUES",
Expand All @@ -458,7 +458,7 @@ async def test_cegarageho(factory: helper.Factory) -> None:
central.event(const.INTERFACE_ID, "VCU3574044:1", "DOOR_STATE", 0)
assert cover.current_position == 0
assert cover.is_closed is True
await cover.set_position(11)
await cover.set_position(position=11)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU3574044:1",
paramset_key="VALUES",
Expand All @@ -468,7 +468,7 @@ async def test_cegarageho(factory: helper.Factory) -> None:
central.event(const.INTERFACE_ID, "VCU3574044:1", "DOOR_STATE", 2)
assert cover.current_position == 10

await cover.set_position(5)
await cover.set_position(position=5)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU3574044:1",
paramset_key="VALUES",
Expand Down Expand Up @@ -532,7 +532,7 @@ async def test_cegaragetm(factory: helper.Factory) -> None:
assert cover.usage == HmEntityUsage.CE_PRIMARY

assert cover.current_position is None
await cover.set_position(81)
await cover.set_position(position=81)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU6166407:1",
paramset_key="VALUES",
Expand All @@ -551,7 +551,7 @@ async def test_cegaragetm(factory: helper.Factory) -> None:
central.event(const.INTERFACE_ID, "VCU6166407:1", "DOOR_STATE", 0)
assert cover.current_position == 0
assert cover.is_closed is True
await cover.set_position(11)
await cover.set_position(position=11)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU6166407:1",
paramset_key="VALUES",
Expand All @@ -561,7 +561,7 @@ async def test_cegaragetm(factory: helper.Factory) -> None:
central.event(const.INTERFACE_ID, "VCU6166407:1", "DOOR_STATE", 2)
assert cover.current_position == 10

await cover.set_position(5)
await cover.set_position(position=5)
assert mock_client.method_calls[-1] == call.set_value(
channel_address="VCU6166407:1",
paramset_key="VALUES",
Expand Down

0 comments on commit 59b353c

Please sign in to comment.