Git Product home page Git Product logo

pynuki's Introduction

pynuki

PyPI PyPI - Downloads PyPI - License CI

Python library for interacting with Nuki locks and openers

Installation

pip install -U pynuki

Usage

from pynuki import NukiBridge

bridges = NukiBridge.discover()
br = bridges[0]
br.token = "YOUR_TOKEN"

# Locks
br.locks[0].lock()
br.locks[0].unlock()

# Openers
br.openers[0].activate_rto()
br.openers[0].deactivate_rto()

pynuki's People

Contributors

chpego avatar damarco avatar dependabot[bot] avatar fabiankielkopf avatar pree avatar pschmitt avatar pyup-bot avatar savjee avatar steinerl avatar thomkaufmann avatar vauriga avatar ziceva avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pynuki's Issues

Home assistant 0.111.1 : cannot read nuki state

Logger: homeassistant.helpers.entity
Source: components/nuki/lock.py:124
First occurred: 11:40:21 (377 occurrences)
Last logged: 14:56:51

Update for lock.porta fails
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 472, in async_device_update
await self.hass.async_add_executor_job(self.update)
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/nuki/lock.py", line 124, in update
self._nuki_device.update(aggressive=level)
File "/usr/local/lib/python3.7/site-packages/pynuki/device.py", line 90, in update
"Failed to update data for lock. "
AssertionError: Failed to update data for lock. Nuki ID None volatized.

Lock connected to software bridge detected as NukiDevice

I've just updated Home Assistant to latest version (0.111), and now the Nuki lock is not detected (connected to Android Nuki bridge).
I've seen that bridge.locks is empty, due to the software bridge doesn't return "deviceType" in list command and then is append as a NukiDevice to the devices list.
[{"nukiId":XXXXXX"name":"Cerradura","lastKnownState":{"state":1,"stateName":"locked","batteryCritical":false,"timestamp":"2020-06-11T07:26:03+02:00"}}]
So, I think for backwards compatibility, if there is no "deviceType" when retrieving _get_devices, it should be created as NukiLock instead of a NukiDevice.
What do you think?

Implement new device types (available since 2 weeks)

Nuki Bridge HTTP API V1.13.1 introduced 2 new device types, which the actual version does not support. As I found out, the implementation of Nuki integration in Home Assistant relies on your work, so probably update this library will fix the issues in HA ... thank you!

https://developer.nuki.io/page/nuki-bridge-http-api-1-13/4/

Nuki device type
0 => smartlock (Nuki Smart Lock 1.0/2.0)
2 => opener (Nuki Opener)
3 => smartdoor (Nuki Smart Door)
4 => smartlock 3 (Nuki Smart Lock 3.0 (Pro))

My Python skills are not good enough to supply a complete fix, but this could be a simple option. A better option would be to add an array that contains all supported device types, but that's probably more complicated...

pynuki/constants.py
After line 22, add:
DEVICE_TYPE_SMARTDOOR = 3
DEVICE_TYPE_NEWLOCK = 4

pynuki/bridge.py
After Line 218, add
elif dev_type == const.DEVICE_TYPE_NEWLOCK:
//modify data that device_data.get("deviceType") gives back 2 instead of 3 ... no idea how that is done in Phyton :-(
dev = NukiLock(self, data)
elif dev_type == const.DEVICE_TYPE_NEWLOCK:
//modify data that device_data.get("deviceType") gives back 2 instead of 4 ...no idea how that is done in Phyton :-(
dev = NukiLock(self, data)

Nuki opener

No idea if this goes here. But i get an error with the nuki opener. As far as i could read from a HA forum post by you, you don't have one? If i can test stuff for you, please do tell. The error is as follows.

404 Client Error: Not Found for url: http://[MYip]:8080/lockAction?token=[MyToken]&nukiId=[MyNukiID]&action=3&noWait=1

Improvement: use the callback instead of polling

As far as I can tell this library is using a polling mechanism to get the lockState. As the resource from the bridge are limited this often results in 503 errors. A better approach would be to use the callback mechanism. You can set an endpoint which the bridge calls when a change in state has been detected. This is more reliable, gives faster feedback and reduces the network traffic and load on the bridge.

https://developer.nuki.io/page/nuki-bridge-http-api-1-12/4#heading--callback

It would be a big improvement to have this in Home Assistant.

If you know any javascript you could have a look at this library which was initially developed by me and uses the callbacks: https://github.com/pfreguia/nuki.homey . I can answer questions about this all but I dont know Phyton so I cant actually code it.

AssertionError: Failed to update data for lock. Nuki ID None volatized.

Hi @pschmitt, I'm seeing these errors in my HA logs, and the lock is no longer available in HA until I restart the HA server. The opener continues to work just fine.

This is with pynuki 1.3.7 and homeassistant-0.110.1.

Thanks!

may 22 00:33:24 hass hass[170685]: 2020-05-22 00:33:24 INFO (SyncWorker_2) [pynetgear] Get attached devices
may 22 00:33:35 hass hass[170685]: 2020-05-22 00:33:35 INFO (SyncWorker_3) [pynetgear] Get attached devices
may 22 00:33:46 hass hass[170685]: 2020-05-22 00:33:46 INFO (SyncWorker_3) [pynetgear] Get attached devices
may 22 00:33:48 hass hass[170685]: 2020-05-22 00:33:48 ERROR (MainThread) [homeassistant.helpers.entity] Update for lock.hodor fails
may 22 00:33:48 hass hass[170685]: Traceback (most recent call last):
may 22 00:33:48 hass hass[170685]: File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
may 22 00:33:48 hass hass[170685]: await self.async_device_update()
may 22 00:33:48 hass hass[170685]: File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/entity.py", line 472, in async_device_update
may 22 00:33:48 hass hass[170685]: await self.hass.async_add_executor_job(self.update)
may 22 00:33:48 hass hass[170685]: File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
may 22 00:33:48 hass hass[170685]: result = self.fn(*self.args, **self.kwargs)
may 22 00:33:48 hass hass[170685]: File "/home/homeassistant/.homeassistant/custom_components/nuki/lock.py", line 124, in update
may 22 00:33:48 hass hass[170685]: self._nuki_device.update(aggressive=level)
may 22 00:33:48 hass hass[170685]: File "/srv/homeassistant/lib/python3.8/site-packages/pynuki/device.py", line 89, in update
may 22 00:33:48 hass hass[170685]: assert data, (
may 22 00:33:48 hass hass[170685]: AssertionError: Failed to update data for lock. Nuki ID None volatized.

Bridge seems to not support "secure" tokens

I have a Nuki-Lock and a Nuki-Bridge connected to Home Assistant.

{
   "bridgeType":1,
   "ids":{
      "hardwareId": ####,
      "serverId": ####
   },
   "versions":{
      "firmwareVersion":"2.14.0",
      "wifiFirmwareVersion":"2.3.0"
   },
   "uptime":43432,
   "currentTime":"2022-07-30T06:35:40+00:00",
   "wlanConnected":true,
   "serverConnected":true,
   "scanResults":[
      {
         "deviceType":4,
         "nukiId": ####,
         "name":"Nuki_####",
         "rssi":-50,
         "paired":true
      }
   ]
}

But at some time, pynuki wasnt able to call the api due to an invalid token... after some investigation i found out, that the line:

get_params = hash_token(self.token)

causes the error ... using the plaintext-token as in
get_params = {"token": self.token}

works fine.

Unfortunately i cannot disable "secure"-tokens via Home Assistant.. but on the other hand it would be fine if i know why hased tokens don't work on my setup (and how i can fix that)... now everytime pynuki get's updated, i need to "patch" it to get my setup work again.

Thanks in advance,
Benedikt

locks() returns both device types

The problem seems to be in _get_devices() where the condition on self.list(device_type=device_type) is ignored when device_type is 0.
It works properly for openers(), returning just devices of device_type 2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.