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

Improvements #6

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Simple verisure client to read Yale Doorman lock states and forward values to MQ

See commonn environment variables from [MQTT-Framework](https://github.com/paulianttila/MQTT-Framework).

| **Variable** | **Default** | **Descrition** |
|----------------------------|------------------------|---------------------------------------------------------------------------------------------------|
| CFG_APP_NAME | verisure2mqtt | Name of the app. |
| CFG_VERISURE_USERNAME | | Username for Verisure login. At least read access required. |
| CFG_VERISURE_PASSWORD | | Password for Verisure login. |
| CFG_VERISURE_TOKEN_FILE | /data/.verisure-cookie | Token file for Verisure login. See [Create Verisure token](#create-verisure-token). |
| CFG_VERISURE_INSTALLATION | | Verisure installation name to use. If only one installation exits, it will be used automatically. |
| **Variable** | **Default** | **Descrition** |
|--------------------------------|------------------------|---------------------------------------------------------------------------------------------------|
| CFG_APP_NAME | verisure2mqtt | Name of the app. |
| CFG_VERISURE_USERNAME | | Username for Verisure login. At least read access required. |
| CFG_VERISURE_PASSWORD | | Password for Verisure login. |
| CFG_VERISURE_TOKEN_FILE | /data/.verisure-cookie | Token file for Verisure login. See [Create Verisure token](#create-verisure-token). |
| CFG_VERISURE_INSTALLATION | | Verisure installation name to use. If only one installation exits, it will be used automatically. |
| CFG_VERISURE_RATE_LIMIT | 1 | Number of requests allowed within period to Verisure site. |
| CFG_VERISURE_RATE_LIMIT_PERIOD | 300 | Time period for rate limit in seconds. |

## Example docker-compose.yaml

Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ tzlocal==4.2
# via apscheduler
urllib3==1.26.14
# via requests
vsure==2.6.1
vsure==2.6.2
# via -r requirements.txt
werkzeug==2.2.2
# via flask
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
vsure==2.6.1
vsure==2.6.2
ratelimit==2.2.1
22 changes: 15 additions & 7 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
LoginError as VerisureLoginError,
ResponseError as VerisureResponseError,
)
from ratelimit import limits
from pyrate_limiter import RequestRate, Limiter


class MfaRequired(Exception):
Expand All @@ -30,6 +30,8 @@ def __init__(self):
VERISURE_PASSWORD = None
VERISURE_TOKEN_FILE = "/data/.verisure-cookie" # nosec
VERISURE_INSTALLATION = None
VERISURE_RATE_LIMIT = 1
VERISURE_RATE_LIMIT_PERIOD = 300


class MyApp:
Expand Down Expand Up @@ -61,9 +63,15 @@ def init(self, callbacks: Callbacks) -> None:
self.mfa_validate_ongoing = False
self.installations = None
self.giid = None
self.limiter = Limiter(
RequestRate(
self.config["VERISURE_RATE_LIMIT"],
self.config["VERISURE_RATE_LIMIT_PERIOD"],
)
)

def get_version(self) -> str:
return "2.0.2"
return "2.1.0"

def stop(self) -> None:
self.logger.debug("Exit")
Expand All @@ -86,10 +94,10 @@ def do_update(self, trigger_source: TriggerSource) -> None:
self.logger.debug(f"Update called, trigger_source={trigger_source}")
self.update()

@limits(1, period=300)
def update(self):
self.logger.debug("Fetch data from verisure")
try:
self.logger.debug("Fetch data from verisure")
self.limiter.try_acquire("Verisure update")
self.login()
overview = self.fecth_data_from_verisure()
self.logger.debug(f"Received overview: {overview}")
Expand All @@ -106,11 +114,11 @@ def login(self) -> None:

if self.login_done:
try:
self.installations = self.verisure.login_cookie()
self.handle_succesfull_login()
self.logger.debug("Update token")
self.verisure.update_cookie()
return
except VerisureLoginError as e:
self.handle_failed_login(e, "Login with token failed")
self.handle_failed_login(e, "Token update failed")

self.logger.debug("Login")
self.login_metric.inc()
Expand Down