From 3678dd420f5b922624b30b3e6f3fadd23e6e9876 Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 7 Apr 2019 19:43:55 +0200 Subject: [PATCH] 20s default timeout 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 --- homeassistant/components/nuki/lock.py | 65 +++++++++++++++------ homeassistant/components/nuki/manifest.json | 2 +- requirements_all.txt | 2 +- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/nuki/lock.py b/homeassistant/components/nuki/lock.py index 0d0452378583b7..2f151e3a532867 100644 --- a/homeassistant/components/nuki/lock.py +++ b/homeassistant/components/nuki/lock.py @@ -1,10 +1,12 @@ """Nuki.io lock platform.""" from datetime import timedelta import logging +import requests 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 ( ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, CONF_TOKEN) import homeassistant.helpers.config_validation as cv @@ -13,6 +15,7 @@ _LOGGER = logging.getLogger(__name__) DEFAULT_PORT = 8080 +DEFAULT_TIMEOUT = 20 ATTR_BATTERY_CRITICAL = 'battery_critical' ATTR_NUKI_ID = 'nuki_id' @@ -23,8 +26,8 @@ NUKI_DATA = 'nuki' -SERVICE_LOCK_N_GO = 'nuki_lock_n_go' -SERVICE_UNLATCH = 'nuki_unlatch' +SERVICE_LOCK_N_GO = 'lock_n_go' +SERVICE_CHECK_CONNECTION = "check_connection" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_HOST): cv.string, @@ -37,7 +40,7 @@ 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 }) @@ -45,8 +48,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Nuki lock platform.""" from pynuki import NukiBridge - bridge = NukiBridge(config.get(CONF_HOST), config.get(CONF_TOKEN)) - add_entities([NukiLock(lock) for lock in bridge.locks]) + bridge = NukiBridge(config[CONF_HOST], config[CONF_TOKEN], + config[CONF_PORT], DEFAULT_TIMEOUT) + add_entities([NukiLock(lock) + for lock in bridge.locks]) def service_handler(service): """Service handler for nuki services.""" @@ -63,15 +68,15 @@ def service_handler(service): if service.service == SERVICE_LOCK_N_GO: unlatch = service.data[ATTR_UNLATCH] lock.lock_n_go(unlatch=unlatch) - elif service.service == SERVICE_UNLATCH: - lock.unlatch() + elif service.service == SERVICE_CHECK_CONNECTION: + lock.check_connection() hass.services.register( - DOMAIN, SERVICE_LOCK_N_GO, service_handler, + 'nuki', SERVICE_LOCK_N_GO, service_handler, schema=LOCK_N_GO_SERVICE_SCHEMA) hass.services.register( - DOMAIN, SERVICE_UNLATCH, service_handler, - schema=UNLATCH_SERVICE_SCHEMA) + 'nuki', SERVICE_CHECK_CONNECTION, service_handler, + schema=CHECK_CONNECTION_SERVICE_SCHEMA) class NukiLock(LockDevice): @@ -83,6 +88,7 @@ def __init__(self, nuki_lock): self._locked = nuki_lock.is_locked self._name = nuki_lock.name self._battery_critical = nuki_lock.battery_critical + self._available = nuki_lock.state != 255 async def async_added_to_hass(self): """Call when entity is added to hass.""" @@ -110,12 +116,26 @@ def device_state_attributes(self): ATTR_NUKI_ID: self._nuki_lock.nuki_id} 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): """Update the nuki lock properties.""" - self._nuki_lock.update(aggressive=False) - self._name = self._nuki_lock.name - self._locked = self._nuki_lock.is_locked - self._battery_critical = self._nuki_lock.battery_critical + try: + self._nuki_lock.update(aggressive=False) + except requests.exceptions.RequestException: + 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): """Lock the device.""" @@ -125,6 +145,10 @@ def unlock(self, **kwargs): """Unlock the device.""" self._nuki_lock.unlock() + def open(self, **kwargs): + """Open the door latch.""" + self._nuki_lock.unlatch() + def lock_n_go(self, unlatch=False, **kwargs): """Lock and go. @@ -133,6 +157,11 @@ def lock_n_go(self, unlatch=False, **kwargs): """ self._nuki_lock.lock_n_go(unlatch, kwargs) - def unlatch(self, **kwargs): - """Unlatch door.""" - self._nuki_lock.unlatch() + def check_connection(self, **kwargs): + """Update the nuki lock properties.""" + try: + self._nuki_lock.update(aggressive=True) + except requests.exceptions.RequestException: + self._available = False + else: + self._available = self._nuki_lock.state != 255 diff --git a/homeassistant/components/nuki/manifest.json b/homeassistant/components/nuki/manifest.json index d031cf6ce5ff7b..932b80690c4d2c 100644 --- a/homeassistant/components/nuki/manifest.json +++ b/homeassistant/components/nuki/manifest.json @@ -3,7 +3,7 @@ "name": "Nuki", "documentation": "https://www.home-assistant.io/components/nuki", "requirements": [ - "pynuki==1.3.2" + "pynuki==1.3.3" ], "dependencies": [], "codeowners": [ diff --git a/requirements_all.txt b/requirements_all.txt index b0a034b36a1aee..8b0783a84fcef8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1277,7 +1277,7 @@ pynetgear==0.6.1 pynetio==0.1.9.1 # homeassistant.components.nuki -pynuki==1.3.2 +pynuki==1.3.3 # homeassistant.components.nut pynut2==2.1.2