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

Improved autolock functionality #12

Merged
merged 1 commit into from
Jul 12, 2023
Merged
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
46 changes: 34 additions & 12 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def init(self, callbacks: Callbacks) -> None:
)

def get_version(self) -> str:
return "2.2.3"
return "2.2.4"

def stop(self) -> None:
self.logger.debug("Exit")
Expand Down Expand Up @@ -259,28 +259,50 @@ def handle_command(self, message: str) -> None:
data = json.loads(message)
sn = data["deviceLabel"]
command = data["command"]
req = None

match command:
case "enableAutolock":
req = self.verisure.set_autolock_enabled(
device_label=sn, auto_lock_enabled=True, giid=self.giid
)
self.set_autolock(sn, True)
case "disableAutolock":
req = self.verisure.set_autolock_enabled(
device_label=sn, auto_lock_enabled=False, giid=self.giid
)
self.set_autolock(sn, False)
case "updateAutolockState":
req = self.verisure.door_lock_configuration(
device_label=sn, giid=self.giid
)
self.update_lock_config(sn)

def set_autolock(
self, device_label: str, state: bool, update_state: bool = True
) -> None:
req = self.verisure.set_autolock_enabled(
device_label=device_label, auto_lock_enabled=state, giid=self.giid
)
self.send_request(req)
if update_state:
self.update_lock_config(device_label)

def update_lock_config(self, device_label: str) -> dict:
req = self.verisure.door_lock_configuration(
device_label=device_label, giid=self.giid
)
res = self.send_request(req)
locks = res.get("data", {}).get("installation", {}).get("smartLocks", [])
self.publish_lock_configs(locks)

def publish_lock_configs(self, smartLocks: dict) -> None:
for lock in smartLocks:
area = lock["device"]["area"]
autoLockEnabled = lock["configuration"]["autoLockEnabled"]
self.publish_value_to_mqtt_topic(
f"locks/{area}/autoLockEnabled", autoLockEnabled, True
)

def send_request(self, req: dict) -> dict:
if req is not None:
self.logger.debug(f"Sending request: {req}")
res = self.verisure.request(req)
self.logger.debug(f"Received response to request: {res}")
return res
else:
self.logger.error(f"No request to send for '{message}'")
self.logger.error("No request to send")
return {}

def update_data_to_mqtt(self, overview: dict) -> None:
self.publish_broadband(overview)
Expand Down
Loading