Nuki to use entity platform (#43774)

This commit is contained in:
Paulus Schoutsen 2020-12-11 18:02:23 +01:00 committed by GitHub
parent e33405a8b0
commit e67809713f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,11 +8,8 @@ from requests.exceptions import RequestException
import voluptuous as vol
from homeassistant.components.lock import PLATFORM_SCHEMA, SUPPORT_OPEN, LockEntity
from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, CONF_TOKEN
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.service import extract_entity_ids
from . import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN
from homeassistant.helpers import config_validation as cv, entity_platform
_LOGGER = logging.getLogger(__name__)
@ -28,8 +25,6 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=30)
NUKI_DATA = "nuki"
SERVICE_LOCK_N_GO = "lock_n_go"
ERROR_STATES = (0, 254, 255)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -40,16 +35,11 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
}
)
LOCK_N_GO_SERVICE_SCHEMA = vol.Schema(
{
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
vol.Optional(ATTR_UNLATCH, default=False): cv.boolean,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Nuki lock platform."""
def get_entities():
bridge = NukiBridge(
config[CONF_HOST],
config[CONF_TOKEN],
@ -58,29 +48,25 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
DEFAULT_TIMEOUT,
)
devices = [NukiLockEntity(lock) for lock in bridge.locks]
entities = [NukiLockEntity(lock) for lock in bridge.locks]
entities.extend([NukiOpenerEntity(opener) for opener in bridge.openers])
return entities
def service_handler(service):
"""Service handler for nuki services."""
entity_ids = extract_entity_ids(hass, service)
unlatch = service.data[ATTR_UNLATCH]
entities = await hass.async_add_executor_job(get_entities)
for lock in devices:
if lock.entity_id not in entity_ids:
continue
lock.lock_n_go(unlatch=unlatch)
async_add_entities(entities)
hass.services.register(
DOMAIN,
SERVICE_LOCK_N_GO,
service_handler,
schema=LOCK_N_GO_SERVICE_SCHEMA,
platform = entity_platform.current_platform.get()
assert platform is not None
platform.async_register_entity_service(
"lock_n_go",
{
vol.Optional(ATTR_UNLATCH, default=False): cv.boolean,
},
"lock_n_go",
)
devices.extend([NukiOpenerEntity(opener) for opener in bridge.openers])
add_entities(devices)
class NukiDeviceEntity(LockEntity, ABC):
"""Representation of a Nuki device."""
@ -172,13 +158,13 @@ class NukiLockEntity(NukiDeviceEntity):
"""Open the door latch."""
self._nuki_device.unlatch()
def lock_n_go(self, unlatch=False, **kwargs):
def lock_n_go(self, unlatch):
"""Lock and go.
This will first unlock the door, then wait for 20 seconds (or another
amount of time depending on the lock settings) and relock.
"""
self._nuki_device.lock_n_go(unlatch, kwargs)
self._nuki_device.lock_n_go(unlatch)
class NukiOpenerEntity(NukiDeviceEntity):
@ -200,3 +186,6 @@ class NukiOpenerEntity(NukiDeviceEntity):
def open(self, **kwargs):
"""Buzz open the door."""
self._nuki_device.electric_strike_actuation()
def lock_n_go(self, unlatch):
"""Stub service."""