Skip to content

Commit

Permalink
20s default timeout
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Fran committed Jul 14, 2019
1 parent 60fe4c9 commit 3678dd4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
65 changes: 47 additions & 18 deletions 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
Expand All @@ -13,6 +15,7 @@
_LOGGER = logging.getLogger(__name__)

DEFAULT_PORT = 8080
DEFAULT_TIMEOUT = 20

ATTR_BATTERY_CRITICAL = 'battery_critical'
ATTR_NUKI_ID = 'nuki_id'
Expand All @@ -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,
Expand All @@ -37,16 +40,18 @@
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
})


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."""
Expand All @@ -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):
Expand All @@ -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."""
Expand Down Expand Up @@ -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."""
Expand All @@ -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.
Expand All @@ -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
2 changes: 1 addition & 1 deletion homeassistant/components/nuki/manifest.json
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Expand Up @@ -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
Expand Down

0 comments on commit 3678dd4

Please sign in to comment.