2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 locks."""
|
2021-05-18 19:15:47 +00:00
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
from pyisy.constants import ISY_VALUE_UNKNOWN
|
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
from homeassistant.components.lock import DOMAIN as LOCK, LockEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-23 08:11:58 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
from .const import _LOGGER, DOMAIN as ISY994_DOMAIN, ISY994_NODES, ISY994_PROGRAMS
|
2020-05-05 04:03:12 +00:00
|
|
|
from .entity import ISYNodeEntity, ISYProgramEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from .helpers import migrate_old_unique_ids
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-12 02:32:19 +00:00
|
|
|
VALUE_TO_STATE = {0: False, 100: True}
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-12 07:49:46 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Set up the ISY994 lock platform."""
|
2020-05-09 19:49:00 +00:00
|
|
|
hass_isy_data = hass.data[ISY994_DOMAIN][entry.entry_id]
|
2016-09-11 18:18:53 +00:00
|
|
|
devices = []
|
2020-05-09 19:49:00 +00:00
|
|
|
for node in hass_isy_data[ISY994_NODES][LOCK]:
|
2020-05-05 04:03:12 +00:00
|
|
|
devices.append(ISYLockEntity(node))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
for name, status, actions in hass_isy_data[ISY994_PROGRAMS][LOCK]:
|
2020-05-05 04:03:12 +00:00
|
|
|
devices.append(ISYLockProgramEntity(name, status, actions))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
await migrate_old_unique_ids(hass, LOCK, devices)
|
|
|
|
async_add_entities(devices)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYLockEntity(ISYNodeEntity, LockEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Representation of an ISY994 lock device."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool:
|
|
|
|
"""Get whether the lock is in locked state."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
|
|
|
return VALUE_TO_STATE.get(self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_lock(self, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the lock command to the ISY994 device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.secure_lock():
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Unable to lock device")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_unlock(self, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the unlock command to the ISY994 device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.secure_unlock():
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Unable to lock device")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYLockProgramEntity(ISYProgramEntity, LockEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Representation of a ISY lock program."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool:
|
|
|
|
"""Return true if the device is locked."""
|
2020-05-12 02:32:19 +00:00
|
|
|
return bool(self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_lock(self, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Lock the device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._actions.run_then():
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Unable to lock device")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2021-05-18 19:15:47 +00:00
|
|
|
async def async_unlock(self, **kwargs) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Unlock the device."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._actions.run_else():
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Unable to unlock device")
|