Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Retrying setup: 'batteryLevel' #56

Closed
ecm4runner opened this issue Apr 8, 2023 · 11 comments · Fixed by dknowles2/pyschlage#54 or #60
Closed

Retrying setup: 'batteryLevel' #56

ecm4runner opened this issue Apr 8, 2023 · 11 comments · Fixed by dknowles2/pyschlage#54 or #60

Comments

@ecm4runner
Copy link

I just installed this integration and getting the above error. Below is the log.

Thank you

2023-04-08 08:04:55.471 ERROR (MainThread) [custom_components.schlage] Unexpected error fetching schlage data: 'batteryLevel'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 250, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 206, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/schlage/__init__.py", line 37, in async_update_data
    for lock in await hass.async_add_executor_job(api.locks):
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyschlage/api.py", line 30, in locks
    return [Lock.from_json(self._auth, d) for d in response.json()]
  File "/usr/local/lib/python3.10/site-packages/pyschlage/api.py", line 30, in 
    return [Lock.from_json(self._auth, d) for d in response.json()]
  File "/usr/local/lib/python3.10/site-packages/pyschlage/lock.py", line 72, in from_json
    battery_level=json["attributes"]["batteryLevel"],
KeyError: 'batteryLevel'
2023-04-08 08:04:55.480 DEBUG (MainThread) [custom_components.schlage] Finished fetching schlage data in 1.644 seconds (success: False)
dknowles2 added a commit that referenced this issue Apr 8, 2023
@dknowles2
Copy link
Owner

I added a fix in the upstream library (dknowles2/pyschlage#51) which should hopefully fix the problem. Try updating to the 2023.4.0 release. If it doesn't fix the problem I'll take another look.

Cheers!

@ecm4runner
Copy link
Author

Thank you for the quick update. Now I'm getting "Retrying setup: 'lockState'"

2023-04-08 14:02:04.355 ERROR (MainThread) [custom_components.schlage] Unexpected error fetching schlage data: 'lockState'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 250, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 206, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/schlage/__init__.py", line 37, in async_update_data
    for lock in await hass.async_add_executor_job(api.locks):
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyschlage/api.py", line 30, in locks
    return [Lock.from_json(self._auth, d) for d in response.json()]
  File "/usr/local/lib/python3.10/site-packages/pyschlage/api.py", line 30, in 
    return [Lock.from_json(self._auth, d) for d in response.json()]
  File "/usr/local/lib/python3.10/site-packages/pyschlage/lock.py", line 73, in from_json
    is_locked=json["attributes"]["lockState"] == 1,
KeyError: 'lockState'
2023-04-08 14:02:04.362 DEBUG (MainThread) [custom_components.schlage] Finished fetching schlage data in 1.661 seconds (success: False)

@dknowles2 dknowles2 reopened this Apr 8, 2023
@dknowles2
Copy link
Owner

I suspected that might happen. :-)

If lockState isn't being returned in the API, the integration probably won't be super useful. What lock model is this for?

@ecm4runner
Copy link
Author

ecm4runner commented Apr 8, 2023

encode Smart WiFi

Note: i previously had the mcnutter1 fork loaded and it was working for about 3 weeks I have update home assistant since but don't know exactly when it stopped working. figured id try your repo since it seam to have more features.
thanks again

@dknowles2
Copy link
Owner

Are you able to run the script in this gist? https://gist.github.com/dknowles2/0aecca6963ffc408163763b074896f06

It should output a redacted version of the API output that would be very helpful for me to properly debug the problem.

(I can also attempt to paper over this another way if you can't, but the lock state not being present in the response is surprising to me)

@ecm4runner
Copy link
Author

I'm running HA on a pi4 with the HA OS, don't think python is installed. I can try to run fro my local PC?

@dknowles2
Copy link
Owner

Yes, local PC should be fine. You'll likely need to run:

$ pip install pyschlage

first to make sure it pulls in the necessary library.

@ecm4runner
Copy link
Author

SOLVED! Thanks for the help!
Woke up thinking about this and remembered I had one lock that was not connected to WiFi because it was faulty (waiting for replacement). I removed that lock from my profile and the integration works fine.

Using your debug.py (modified slightly .. see below), I verified that was the problem, that lock did not have all the attributes.

#!/usr/bin/python3

from getpass import getpass
from pprint import pprint
import pyschlage
from pyschlage import Lock

username = input("Schlage username: ")
password = getpass()
auth = pyschlage.Auth(username, password)
resp = auth.request("get", Lock.request_path(), params={"archetype": "lock"})

for lock in resp.json():
	with open('debug.txt', 'a') as f:
		for key, value in lock.items(): 
			f.write('%s:%s\n' % (key, value))

@dknowles2 dknowles2 reopened this Apr 9, 2023
@dknowles2
Copy link
Owner

Ah, that's very useful info! So I need to defend against that a bit more in the code and make sure the integration still works. I'll keep this open to track.

@ecm4runner
Copy link
Author

If it helps here is the JSON output from that device

deviceId:XXX
devicetypeId:XXX
physicalId:XXX
modelName:XXX
serialNumber:XXX
macAddress:XXX
CAT:XXX
SAT:XXX
role:xxx
name:XXX
attributes:{'modelName': 'xxx', 'serialNumber': 'xxx', 'macAddress': 'xxx', 'SAT': 'xxx', 'deviceUid': 'xxx', 'CAT': 'xxx'}
relatedDevices:[]
users:[{'identityId': 'xxx', 'friendlyName': 'xxx', 'email': 'xxx', 'role': 'xxx'}]
lastUpdated:xxx
created:xxx

@dknowles2
Copy link
Owner

I just pushed 2023.4.1 which should handle the case where locks are unavailable. Thanks for helping debug! I was able to use your JSON output to create a test case. Always nice to have realistic output in tests. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants