Update homekit controller lock to support locking, unlocking, jammed (#52821)
parent
ee242764a1
commit
564a505486
|
@ -2,18 +2,28 @@
|
||||||
from aiohomekit.model.characteristics import CharacteristicsTypes
|
from aiohomekit.model.characteristics import CharacteristicsTypes
|
||||||
from aiohomekit.model.services import ServicesTypes
|
from aiohomekit.model.services import ServicesTypes
|
||||||
|
|
||||||
from homeassistant.components.lock import LockEntity
|
from homeassistant.components.lock import STATE_JAMMED, LockEntity
|
||||||
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED
|
from homeassistant.const import (
|
||||||
|
ATTR_BATTERY_LEVEL,
|
||||||
|
STATE_LOCKED,
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
STATE_UNLOCKED,
|
||||||
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
from . import KNOWN_DEVICES, HomeKitEntity
|
from . import KNOWN_DEVICES, HomeKitEntity
|
||||||
|
|
||||||
STATE_JAMMED = "jammed"
|
CURRENT_STATE_MAP = {
|
||||||
|
0: STATE_UNLOCKED,
|
||||||
CURRENT_STATE_MAP = {0: STATE_UNLOCKED, 1: STATE_LOCKED, 2: STATE_JAMMED, 3: None}
|
1: STATE_LOCKED,
|
||||||
|
2: STATE_JAMMED,
|
||||||
|
3: STATE_UNKNOWN,
|
||||||
|
}
|
||||||
|
|
||||||
TARGET_STATE_MAP = {STATE_UNLOCKED: 0, STATE_LOCKED: 1}
|
TARGET_STATE_MAP = {STATE_UNLOCKED: 0, STATE_LOCKED: 1}
|
||||||
|
|
||||||
|
REVERSED_TARGET_STATE_MAP = {v: k for k, v in TARGET_STATE_MAP.items()}
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up Homekit lock."""
|
"""Set up Homekit lock."""
|
||||||
|
@ -46,8 +56,44 @@ class HomeKitLock(HomeKitEntity, LockEntity):
|
||||||
def is_locked(self):
|
def is_locked(self):
|
||||||
"""Return true if device is locked."""
|
"""Return true if device is locked."""
|
||||||
value = self.service.value(CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE)
|
value = self.service.value(CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE)
|
||||||
|
if CURRENT_STATE_MAP[value] == STATE_UNKNOWN:
|
||||||
|
return None
|
||||||
return CURRENT_STATE_MAP[value] == STATE_LOCKED
|
return CURRENT_STATE_MAP[value] == STATE_LOCKED
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_locking(self):
|
||||||
|
"""Return true if device is locking."""
|
||||||
|
current_value = self.service.value(
|
||||||
|
CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE
|
||||||
|
)
|
||||||
|
target_value = self.service.value(
|
||||||
|
CharacteristicsTypes.LOCK_MECHANISM_TARGET_STATE
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
CURRENT_STATE_MAP[current_value] == STATE_UNLOCKED
|
||||||
|
and REVERSED_TARGET_STATE_MAP.get(target_value) == STATE_LOCKED
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_unlocking(self):
|
||||||
|
"""Return true if device is unlocking."""
|
||||||
|
current_value = self.service.value(
|
||||||
|
CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE
|
||||||
|
)
|
||||||
|
target_value = self.service.value(
|
||||||
|
CharacteristicsTypes.LOCK_MECHANISM_TARGET_STATE
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
CURRENT_STATE_MAP[current_value] == STATE_LOCKED
|
||||||
|
and REVERSED_TARGET_STATE_MAP.get(target_value) == STATE_UNLOCKED
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_jammed(self):
|
||||||
|
"""Return true if device is jammed."""
|
||||||
|
value = self.service.value(CharacteristicsTypes.LOCK_MECHANISM_CURRENT_STATE)
|
||||||
|
return CURRENT_STATE_MAP[value] == STATE_JAMMED
|
||||||
|
|
||||||
async def async_lock(self, **kwargs):
|
async def async_lock(self, **kwargs):
|
||||||
"""Lock the device."""
|
"""Lock the device."""
|
||||||
await self._set_lock_state(STATE_LOCKED)
|
await self._set_lock_state(STATE_LOCKED)
|
||||||
|
|
|
@ -57,3 +57,23 @@ async def test_switch_read_lock_state(hass, utcnow):
|
||||||
helper.characteristics[LOCK_TARGET_STATE].value = 1
|
helper.characteristics[LOCK_TARGET_STATE].value = 1
|
||||||
state = await helper.poll_and_get_state()
|
state = await helper.poll_and_get_state()
|
||||||
assert state.state == "locked"
|
assert state.state == "locked"
|
||||||
|
|
||||||
|
helper.characteristics[LOCK_CURRENT_STATE].value = 2
|
||||||
|
helper.characteristics[LOCK_TARGET_STATE].value = 1
|
||||||
|
state = await helper.poll_and_get_state()
|
||||||
|
assert state.state == "jammed"
|
||||||
|
|
||||||
|
helper.characteristics[LOCK_CURRENT_STATE].value = 3
|
||||||
|
helper.characteristics[LOCK_TARGET_STATE].value = 1
|
||||||
|
state = await helper.poll_and_get_state()
|
||||||
|
assert state.state == "unknown"
|
||||||
|
|
||||||
|
helper.characteristics[LOCK_CURRENT_STATE].value = 0
|
||||||
|
helper.characteristics[LOCK_TARGET_STATE].value = 1
|
||||||
|
state = await helper.poll_and_get_state()
|
||||||
|
assert state.state == "locking"
|
||||||
|
|
||||||
|
helper.characteristics[LOCK_CURRENT_STATE].value = 1
|
||||||
|
helper.characteristics[LOCK_TARGET_STATE].value = 0
|
||||||
|
state = await helper.poll_and_get_state()
|
||||||
|
assert state.state == "unlocking"
|
||||||
|
|
Loading…
Reference in New Issue