2019-04-03 15:40:03 +00:00
|
|
|
"""Demo lock platform that has two fake locks."""
|
2021-07-20 16:12:56 +00:00
|
|
|
import asyncio
|
|
|
|
|
2020-04-25 16:02:41 +00:00
|
|
|
from homeassistant.components.lock import SUPPORT_OPEN, LockEntity
|
2021-07-20 16:12:56 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_JAMMED,
|
|
|
|
STATE_LOCKED,
|
|
|
|
STATE_LOCKING,
|
|
|
|
STATE_UNLOCKED,
|
|
|
|
STATE_UNLOCKING,
|
|
|
|
)
|
2015-11-17 15:17:57 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Demo lock platform."""
|
2019-11-13 15:37:31 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
DemoLock("Front Door", STATE_LOCKED),
|
|
|
|
DemoLock("Kitchen Door", STATE_UNLOCKED),
|
2021-07-20 16:12:56 +00:00
|
|
|
DemoLock("Poorly Installed Door", STATE_UNLOCKED, False, True),
|
2019-07-31 19:25:30 +00:00
|
|
|
DemoLock("Openable Lock", STATE_LOCKED, True),
|
|
|
|
]
|
|
|
|
)
|
2015-11-17 15:17:57 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Demo config entry."""
|
|
|
|
await async_setup_platform(hass, {}, async_add_entities)
|
|
|
|
|
|
|
|
|
2020-04-25 16:02:41 +00:00
|
|
|
class DemoLock(LockEntity):
|
2016-08-22 12:20:04 +00:00
|
|
|
"""Representation of a Demo lock."""
|
2016-03-07 21:13:18 +00:00
|
|
|
|
2021-06-17 09:25:33 +00:00
|
|
|
_attr_should_poll = False
|
2015-11-17 15:17:57 +00:00
|
|
|
|
2021-07-20 16:12:56 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
state: str,
|
|
|
|
openable: bool = False,
|
|
|
|
jam_on_operation: bool = False,
|
|
|
|
) -> None:
|
2021-06-17 09:25:33 +00:00
|
|
|
"""Initialize the lock."""
|
|
|
|
self._attr_name = name
|
|
|
|
if openable:
|
|
|
|
self._attr_supported_features = SUPPORT_OPEN
|
2021-07-20 16:12:56 +00:00
|
|
|
self._state = state
|
|
|
|
self._openable = openable
|
|
|
|
self._jam_on_operation = jam_on_operation
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locking(self):
|
|
|
|
"""Return true if lock is locking."""
|
|
|
|
return self._state == STATE_LOCKING
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_unlocking(self):
|
|
|
|
"""Return true if lock is unlocking."""
|
|
|
|
return self._state == STATE_UNLOCKING
|
2015-11-17 15:17:57 +00:00
|
|
|
|
2021-07-20 16:12:56 +00:00
|
|
|
@property
|
|
|
|
def is_jammed(self):
|
|
|
|
"""Return true if lock is jammed."""
|
|
|
|
return self._state == STATE_JAMMED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self):
|
|
|
|
"""Return true if lock is locked."""
|
|
|
|
return self._state == STATE_LOCKED
|
|
|
|
|
|
|
|
async def async_lock(self, **kwargs):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Lock the device."""
|
2021-07-20 16:12:56 +00:00
|
|
|
self._state = STATE_LOCKING
|
|
|
|
self.async_write_ha_state()
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
if self._jam_on_operation:
|
|
|
|
self._state = STATE_JAMMED
|
|
|
|
else:
|
|
|
|
self._state = STATE_LOCKED
|
|
|
|
self.async_write_ha_state()
|
2015-11-17 15:17:57 +00:00
|
|
|
|
2021-07-20 16:12:56 +00:00
|
|
|
async def async_unlock(self, **kwargs):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Unlock the device."""
|
2021-07-20 16:12:56 +00:00
|
|
|
self._state = STATE_UNLOCKING
|
|
|
|
self.async_write_ha_state()
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
self._state = STATE_UNLOCKED
|
|
|
|
self.async_write_ha_state()
|
2018-03-25 21:25:28 +00:00
|
|
|
|
2021-07-20 16:12:56 +00:00
|
|
|
async def async_open(self, **kwargs):
|
2018-03-25 21:25:28 +00:00
|
|
|
"""Open the door latch."""
|
2021-07-20 16:12:56 +00:00
|
|
|
self._state = STATE_UNLOCKED
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
if self._openable:
|
|
|
|
return SUPPORT_OPEN
|
2021-08-09 03:33:40 +00:00
|
|
|
return 0
|