Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nuki lock'n'go and unlatch services and add attributes #8687

Merged
merged 4 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unlatch service
  • Loading branch information
pschmitt committed Jul 28, 2017
commit 15fbef91c601471cec709f433a6c34ee6b671efd
31 changes: 23 additions & 8 deletions homeassistant/components/lock/nuki.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from homeassistant.helpers.service import extract_entity_ids
from homeassistant.util import Throttle

REQUIREMENTS = ['pynuki==1.3.0']
REQUIREMENTS = ['pynuki==1.3.1']

_LOGGER = logging.getLogger(__name__)

Expand All @@ -30,6 +30,7 @@
ATTR_UNLATCH = 'unlatch'
DOMAIN = 'nuki'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Platforms always inherit the domain from their base component, in this case that's lock. You should call this constant something else, like NUKI_DATA. You can import DOMAIN too from homeassistant.components.lock and use that below in the service handler.

SERVICE_LOCK_N_GO = 'nuki_lock_n_go'
SERVICE_UNLATCH = 'nuki_unlatch'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
Expand All @@ -42,6 +43,10 @@
vol.Optional(ATTR_UNLATCH, default=False): cv.boolean
})

UNLATCH_SERVICE_SCHEMA = vol.Schema({
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
})

MIN_TIME_BETWEEN_SCANS = timedelta(seconds=30)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=5)

Expand All @@ -53,9 +58,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
bridge = NukiBridge(config.get(CONF_HOST), config.get(CONF_TOKEN))
add_devices([NukiLock(lock, hass) for lock in bridge.locks])

def lock_n_go(service):
"""Service handler for nuki.lock_n_go."""
unlatch = service.data.get(ATTR_UNLATCH, False)
def service_handler(service):
"""Service handler for nuki services."""
entity_ids = extract_entity_ids(hass, service)
all_locks = hass.data[DOMAIN]['lock']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to use the new name for 'nuki' eg:

all_locks = hass.data[NUKI_DATA][DOMAIN]

target_locks = []
Expand All @@ -66,14 +70,21 @@ def lock_n_go(service):
if lock.entity_id in entity_ids:
target_locks.append(lock)
for lock in target_locks:
lock.lock_n_go(unlatch=unlatch)
if service.service == SERVICE_LOCK_N_GO:
unlatch = service.data.get(ATTR_UNLATCH, False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service schema will already have validated the service data.

unlatch = service.data[ATTR_UNLATCH]

lock.lock_n_go(unlatch=unlatch)
elif service.service == SERVICE_UNLATCH:
lock.unlatch()

descriptions = load_yaml_config_file(
path.join(path.dirname(__file__), 'services.yaml'))

hass.services.register(
DOMAIN, SERVICE_LOCK_N_GO, lock_n_go,
DOMAIN, SERVICE_LOCK_N_GO, service_handler,
descriptions.get(SERVICE_LOCK_N_GO), schema=LOCK_N_GO_SERVICE_SCHEMA)
hass.services.register(
DOMAIN, SERVICE_UNLATCH, service_handler,
descriptions.get(SERVICE_UNLATCH), schema=UNLATCH_SERVICE_SCHEMA)


class NukiLock(LockDevice):
Expand All @@ -90,9 +101,9 @@ def __init__(self, nuki_lock, hass):
@asyncio.coroutine
def async_added_to_hass(self):
"""Callback when entity is added to hass."""
if not DOMAIN in self.hass.data:
if DOMAIN not in self.hass.data:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use NUKI_DATA instead of DOMAIN, and DOMAIN instead of 'lock' in this method.

self.hass.data[DOMAIN] = {}
if not 'lock' in self.hass.data[DOMAIN]:
if 'lock' not in self.hass.data[DOMAIN]:
self.hass.data[DOMAIN]['lock'] = []
self.hass.data[DOMAIN]['lock'].append(self)

Expand Down Expand Up @@ -137,3 +148,7 @@ def lock_n_go(self, **kwargs):
amount of time depending on the lock settings) and relock.
"""
self._nuki_lock.lock_n_go(kwargs)

def unlatch(self, **kwargs):
"""Unlatch door."""
self._nuki_lock.unlatch()
8 changes: 8 additions & 0 deletions homeassistant/components/lock/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ nuki_lock_n_go:
description: Whether to unlatch the lock
example: false

nuki_unlatch:
description: "Unlatch"

fields:
entity_id:
description: Entity id of the Nuki lock
example: 'lock.front_door'

lock:
description: Lock all or specified locks

Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ pynetgear==0.3.3
pynetio==0.1.6

# homeassistant.components.lock.nuki
pynuki==1.3.0
pynuki==1.3.1

# homeassistant.components.sensor.nut
pynut2==2.1.2
Expand Down