jbergler / hass-ttlock

Home Assistant integration for TTLock locks
57 stars 13 forks source link

Add basic passcode management features #81

Closed jbergler closed 9 months ago

jbergler commented 9 months ago

Mostly @PedroDelCargo's work from #79

I have added 2 new services: First one to create new pass codes. Second one to delete outdated temporary pass codes.

Closes #77, #79

codecov[bot] commented 9 months ago

Codecov Report

Attention: 14 lines in your changes are missing coverage. Please review.

Comparison is base (ced2c8d) 74.40% compared to head (1b88ded) 76.07%.

Files Patch % Lines
custom_components/ttlock/api.py 22.22% 14 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #81 +/- ## =========================================== + Coverage 74.40% 76.07% +1.66% =========================================== Files 13 13 Lines 633 698 +65 =========================================== + Hits 471 531 +60 - Misses 162 167 +5 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

PedroDelCargo commented 9 months ago

Hello, I tested these changes. The create_passcode service works well. The cleanup_passcodes service doesn't work and generates errors:

2023-12-18 08:56:22.757 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Unexpected error for call_service at pos 1: 1 validation error for Passcode
keyboardPwdType
value is not a valid enumeration member; permitted: 3 (type=type_error.enum; enum_values=[<PasscodeType.temporary: 3>])
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
response_data = await self._async_run_long_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
return long_task.result()
^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2067, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2104, in _execute_service
return await target(service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/ttlock/services.py", line 140, in handle_cleanup_passcodes
codes = await coordinator.api.list_passcodes(coordinator.lock_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/ttlock/api.py", line 243, in list_passcodes
return [Passcode.parse_obj(passcode) for passcode in res["list"]]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/ttlock/api.py", line 243, in <listcomp>
return [Passcode.parse_obj(passcode) for passcode in res["list"]]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Passcode
keyboardPwdType
value is not a valid enumeration member; permitted: 3 (type=type_error.enum; enum_values=[<PasscodeType.temporary: 3>])
2023-12-18 08:56:22.763 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139991667643200] Error handling message: Unknown error (unknown_error) Pierre from 193.248.241.252 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0)
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 26, in _handle_async_response
await func(hass, connection, msg)
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 782, in handle_execute_script
script_result = await script_obj.async_run(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1578, in async_run
return await asyncio.shield(run.async_run())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 420, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 470, in _async_step
self._handle_exception(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 493, in _handle_exception
raise exception
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
response_data = await self._async_run_long_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
return long_task.result()
^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2067, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2104, in _execute_service
return await target(service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/ttlock/services.py", line 140, in handle_cleanup_passcodes
codes = await coordinator.api.list_passcodes(coordinator.lock_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/ttlock/api.py", line 243, in list_passcodes
return [Passcode.parse_obj(passcode) for passcode in res["list"]]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/ttlock/api.py", line 243, in <listcomp>
return [Passcode.parse_obj(passcode) for passcode in res["list"]]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Passcode
keyboardPwdType
value is not a valid enumeration member; permitted: 3 (type=type_error.enum; enum_values=[<PasscodeType.temporary: 3>])
jbergler commented 9 months ago

@PedroDelCargo awesome, I'm going to try and write some unit tests before I merge this, hopefully in the next few days.

PedroDelCargo commented 9 months ago

Great ! 😊 It's still running on my home assistant instance. I'll let you know if I encounter any issue, but so far so good.

purugupta99 commented 8 months ago

Hi, I was trying to access this functionality via the ttlock device dashboard, such that I can set up the passcodes using the HA UI but I didn't see it over there to update/delete the passcodes. Can you help me with how I can use this service to update the passcodes pls? (I'm using the latest release version v0.6.1)