Improve Nuki lock (#22888)

Using port on bridge initialization
Service: check_connection
Attribute: available
Updated requeriments_all.txt
Change unlatch service for open service

Removed extra info

nuki_lock_n_go renamed to lock_n_go
nuki_check_connection renamed to check_connection
This commit is contained in:
Fran 2019-07-16 17:06:47 +02:00 committed by Martin Hjelmare
parent 91d065314c
commit 4afc19ff3a
3 changed files with 49 additions and 20 deletions

View File

@ -1,10 +1,12 @@
"""Nuki.io lock platform.""" """Nuki.io lock platform."""
from datetime import timedelta from datetime import timedelta
import logging import logging
import requests
import voluptuous as vol import voluptuous as vol
from homeassistant.components.lock import DOMAIN, PLATFORM_SCHEMA, LockDevice from homeassistant.components.lock import (
DOMAIN, PLATFORM_SCHEMA, LockDevice, SUPPORT_OPEN)
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, CONF_TOKEN) ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, CONF_TOKEN)
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -13,6 +15,7 @@ from homeassistant.helpers.service import extract_entity_ids
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEFAULT_PORT = 8080 DEFAULT_PORT = 8080
DEFAULT_TIMEOUT = 20
ATTR_BATTERY_CRITICAL = 'battery_critical' ATTR_BATTERY_CRITICAL = 'battery_critical'
ATTR_NUKI_ID = 'nuki_id' ATTR_NUKI_ID = 'nuki_id'
@ -23,8 +26,8 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=30)
NUKI_DATA = 'nuki' NUKI_DATA = 'nuki'
SERVICE_LOCK_N_GO = 'nuki_lock_n_go' SERVICE_LOCK_N_GO = 'lock_n_go'
SERVICE_UNLATCH = 'nuki_unlatch' SERVICE_CHECK_CONNECTION = "check_connection"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string, vol.Required(CONF_HOST): cv.string,
@ -37,7 +40,7 @@ LOCK_N_GO_SERVICE_SCHEMA = vol.Schema({
vol.Optional(ATTR_UNLATCH, default=False): cv.boolean vol.Optional(ATTR_UNLATCH, default=False): cv.boolean
}) })
UNLATCH_SERVICE_SCHEMA = vol.Schema({ CHECK_CONNECTION_SERVICE_SCHEMA = vol.Schema({
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
}) })
@ -45,8 +48,10 @@ UNLATCH_SERVICE_SCHEMA = vol.Schema({
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Nuki lock platform.""" """Set up the Nuki lock platform."""
from pynuki import NukiBridge from pynuki import NukiBridge
bridge = NukiBridge(config.get(CONF_HOST), config.get(CONF_TOKEN)) bridge = NukiBridge(config[CONF_HOST], config[CONF_TOKEN],
add_entities([NukiLock(lock) for lock in bridge.locks]) config[CONF_PORT], DEFAULT_TIMEOUT)
add_entities([NukiLock(lock)
for lock in bridge.locks])
def service_handler(service): def service_handler(service):
"""Service handler for nuki services.""" """Service handler for nuki services."""
@ -63,15 +68,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if service.service == SERVICE_LOCK_N_GO: if service.service == SERVICE_LOCK_N_GO:
unlatch = service.data[ATTR_UNLATCH] unlatch = service.data[ATTR_UNLATCH]
lock.lock_n_go(unlatch=unlatch) lock.lock_n_go(unlatch=unlatch)
elif service.service == SERVICE_UNLATCH: elif service.service == SERVICE_CHECK_CONNECTION:
lock.unlatch() lock.check_connection()
hass.services.register( hass.services.register(
DOMAIN, SERVICE_LOCK_N_GO, service_handler, 'nuki', SERVICE_LOCK_N_GO, service_handler,
schema=LOCK_N_GO_SERVICE_SCHEMA) schema=LOCK_N_GO_SERVICE_SCHEMA)
hass.services.register( hass.services.register(
DOMAIN, SERVICE_UNLATCH, service_handler, 'nuki', SERVICE_CHECK_CONNECTION, service_handler,
schema=UNLATCH_SERVICE_SCHEMA) schema=CHECK_CONNECTION_SERVICE_SCHEMA)
class NukiLock(LockDevice): class NukiLock(LockDevice):
@ -83,6 +88,7 @@ class NukiLock(LockDevice):
self._locked = nuki_lock.is_locked self._locked = nuki_lock.is_locked
self._name = nuki_lock.name self._name = nuki_lock.name
self._battery_critical = nuki_lock.battery_critical self._battery_critical = nuki_lock.battery_critical
self._available = nuki_lock.state != 255
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Call when entity is added to hass.""" """Call when entity is added to hass."""
@ -110,12 +116,26 @@ class NukiLock(LockDevice):
ATTR_NUKI_ID: self._nuki_lock.nuki_id} ATTR_NUKI_ID: self._nuki_lock.nuki_id}
return data return data
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_OPEN
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
def update(self): def update(self):
"""Update the nuki lock properties.""" """Update the nuki lock properties."""
self._nuki_lock.update(aggressive=False) try:
self._name = self._nuki_lock.name self._nuki_lock.update(aggressive=False)
self._locked = self._nuki_lock.is_locked except requests.exceptions.RequestException:
self._battery_critical = self._nuki_lock.battery_critical self._available = False
else:
self._name = self._nuki_lock.name
self._locked = self._nuki_lock.is_locked
self._battery_critical = self._nuki_lock.battery_critical
def lock(self, **kwargs): def lock(self, **kwargs):
"""Lock the device.""" """Lock the device."""
@ -125,6 +145,10 @@ class NukiLock(LockDevice):
"""Unlock the device.""" """Unlock the device."""
self._nuki_lock.unlock() self._nuki_lock.unlock()
def open(self, **kwargs):
"""Open the door latch."""
self._nuki_lock.unlatch()
def lock_n_go(self, unlatch=False, **kwargs): def lock_n_go(self, unlatch=False, **kwargs):
"""Lock and go. """Lock and go.
@ -133,6 +157,11 @@ class NukiLock(LockDevice):
""" """
self._nuki_lock.lock_n_go(unlatch, kwargs) self._nuki_lock.lock_n_go(unlatch, kwargs)
def unlatch(self, **kwargs): def check_connection(self, **kwargs):
"""Unlatch door.""" """Update the nuki lock properties."""
self._nuki_lock.unlatch() try:
self._nuki_lock.update(aggressive=True)
except requests.exceptions.RequestException:
self._available = False
else:
self._available = self._nuki_lock.state != 255

View File

@ -3,7 +3,7 @@
"name": "Nuki", "name": "Nuki",
"documentation": "https://www.home-assistant.io/components/nuki", "documentation": "https://www.home-assistant.io/components/nuki",
"requirements": [ "requirements": [
"pynuki==1.3.2" "pynuki==1.3.3"
], ],
"dependencies": [], "dependencies": [],
"codeowners": [ "codeowners": [

View File

@ -1283,7 +1283,7 @@ pynetgear==0.6.1
pynetio==0.1.9.1 pynetio==0.1.9.1
# homeassistant.components.nuki # homeassistant.components.nuki
pynuki==1.3.2 pynuki==1.3.3
# homeassistant.components.nut # homeassistant.components.nut
pynut2==2.1.2 pynut2==2.1.2