Skip to content

Propositions for enchancements #5

@yawor

Description

@yawor

Hi,

The script works great, but there's a lot of space for making it better.

Right now it can't be used as a library in other software, for example as a library used by HA integration. This is because all code is executed as soon as the file is imported. For this to work as a library, the main logic should also be inside a function and it should be called in a guarding condition like this:

if __name__ == "__main__":
    main()

if the main logic is in the main() function.

For this to work great as a library (especially if used in the HA integration) the requests library should be replaced by some async http library like aiohttp or httpx. HA already uses httpx and has it installed, so I'd opt for using this one also.

RIght now the script doesn't use refresh token, which is actually really helpful. I'd change whole authentication part of the script. First of all I'd separate authentication from opening the doors.
The user would first use auth command. Providing username and password in the command line could left as an option, but I'd use prompts if either one or both of them is not provided. So user would just call auth and the script would first as for username and then for password (in safe way, hiding the input). The auth would then save both access token and refresh token, together with max age (expiry timestamp could also be calculated right away ans stored).
The door opening would not accept username and password anymore. Instead it would need to check if the access token is almost expired (for example if it expires in 2 days or less, which is half of the max age for the token) - if yes, then before opening the doors, it would use refresh_token to refresh access token by POSTing grant_type=refresh_token&refresh_token=<current_refresh_token> to /oauth/token. This is a correct way of using OAuth2 and I've tested it using curl and this works correctly on Fermax server.
There could also be a command to only check if access token expires shortly and refresh it if it's needed - this could be run from crontab to always keep the access token fresh.

The last thing is to properly package this script as a library. I propose using Poetry as a packaging tool.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions