Skip to content

Commit

Permalink
Use enum value for actions (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ authored Feb 12, 2022
1 parent 5247f96 commit e0b2506
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Version 0.32.3 (2022-02-12)
- Add state to HmIP-MOD-HO
- Use enum value for actions

Version 0.32.2 (2022-02-11)
- Fix HmIP-MOD-HO
Expand Down
10 changes: 5 additions & 5 deletions hahomematic/devices/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
HM_OPENING = "UP"
HM_CLOSING = "DOWN"

GARAGE_DOOR_COMMAND_NOP = 0
GARAGE_DOOR_COMMAND_OPEN = 1
GARAGE_DOOR_COMMAND_STOP = 2
GARAGE_DOOR_COMMAND_CLOSE = 3
GARAGE_DOOR_COMMAND_PARTIAL_OPEN = 4
GARAGE_DOOR_COMMAND_NOP = "NOP"
GARAGE_DOOR_COMMAND_OPEN = "OPEN"
GARAGE_DOOR_COMMAND_STOP = "STOP"
GARAGE_DOOR_COMMAND_CLOSE = "CLOSE"
GARAGE_DOOR_COMMAND_PARTIAL_OPEN = "PARTIAL_OPEN"

GARAGE_DOOR_STATE_CLOSED = "CLOSED"
GARAGE_DOOR_STATE_OPEN = "OPEN"
Expand Down
19 changes: 11 additions & 8 deletions hahomematic/devices/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@

_LOGGER = logging.getLogger(__name__)

HM_LOCKED_TEXT = "LOCKED"
HM_LOCKED = 0
HM_UNLOCKED = 1
HM_OPEN = 2
LOCK_STATE_UNKNOWN = "UNKNOWN"
LOCK_STATE_LOCKED = "LOCKED"
LOCK_STATE_UNLOCKED = "UNLOCKED"

LOCK_TARGET_LEVEL_LOCKED = "LOCKED"
LOCK_TARGET_LEVEL_UNLOCKED = "UNLOCKED"
LOCK_TARGET_LEVEL_OPEN = "OPEN"


class BaseLock(CustomEntity):
Expand Down Expand Up @@ -109,7 +112,7 @@ def _error(self) -> bool | None:
@property
def is_locked(self) -> bool:
"""Return true if lock is on."""
return self._lock_state == HM_LOCKED_TEXT
return self._lock_state == LOCK_STATE_LOCKED

@property
def is_jammed(self) -> bool:
Expand All @@ -118,15 +121,15 @@ def is_jammed(self) -> bool:

async def lock(self) -> None:
"""Lock the lock."""
await self._e_lock_target_level.send_value(HM_LOCKED)
await self._e_lock_target_level.send_value(LOCK_TARGET_LEVEL_LOCKED)

async def unlock(self) -> None:
"""Unlock the lock."""
await self._e_lock_target_level.send_value(HM_UNLOCKED)
await self._e_lock_target_level.send_value(LOCK_TARGET_LEVEL_UNLOCKED)

async def open(self) -> None:
"""Open the lock."""
await self._e_lock_target_level.send_value(HM_OPEN)
await self._e_lock_target_level.send_value(LOCK_TARGET_LEVEL_OPEN)


class CeRfLock(BaseLock):
Expand Down
12 changes: 12 additions & 0 deletions hahomematic/internal/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ def __init__(
parameter_data=parameter_data,
platform=HmPlatform.ACTION,
)

async def send_value(self, value: Any) -> None:
"""Set the value of the entity."""
# We allow setting the value via index as well, just in case.
if (
value is not None
and self._value_list
and isinstance(value, str)
):
await super().send_value(self._value_list.index(value))
else:
await super().send_value(value)
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async def get_hm_custom_entity(
for custom_entity in hm_device.custom_entities.values():
if custom_entity.channel_no == channel_no:
if do_load:
await custom_entity.load_data()
await custom_entity.load_entity_value()
return custom_entity
return None

Expand Down
13 changes: 13 additions & 0 deletions tests/test_central.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from hahomematic.const import HmEntityUsage
from hahomematic.devices.climate import ATTR_TEMPERATURE, CeRfThermostat
from hahomematic.devices.lock import LOCK_TARGET_LEVEL_OPEN


@pytest.mark.asyncio
Expand Down Expand Up @@ -99,6 +100,18 @@ async def test_device_set_data(central, pydev_ccu, loop) -> None:
assert new_value == 19.0


@pytest.mark.asyncio
async def test_action_on_lock(central, pydev_ccu, loop) -> None:
"""Test callback."""
assert central
assert pydev_ccu
lock = await get_hm_custom_entity(central_unit=central, address="VCU9724704", channel_no=1, do_load=True)
assert lock
assert lock.is_locked is False
await lock.lock()
assert lock


@pytest.mark.asyncio
async def test_device_export(central, pydev_ccu, loop) -> None:
"""Test device export."""
Expand Down

0 comments on commit e0b2506

Please sign in to comment.