Skip to content

Commit

Permalink
Merge pull request #8 from paulianttila/command-support
Browse files Browse the repository at this point in the history
Command support
  • Loading branch information
paulianttila authored Jul 10, 2023
2 parents be9e62f + 8c93957 commit 3509d31
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from mqtt_framework import Framework
from mqtt_framework import Config
from mqtt_framework.callbacks import Callbacks
Expand Down Expand Up @@ -71,20 +72,40 @@ def init(self, callbacks: Callbacks) -> None:
)

def get_version(self) -> str:
return "2.1.1"
return "2.2.0"

def stop(self) -> None:
self.logger.debug("Exit")

def subscribe_to_mqtt_topics(self) -> None:
self.subscribe_to_mqtt_topic("requestMFA")
self.subscribe_to_mqtt_topic("validateMFA")
self.subscribe_to_mqtt_topic("sendCommand")

def mqtt_message_received(self, topic: str, message: str) -> None:
if topic == "requestMFA" and message.lower() == "true":
self.request_mfa()
if topic == "validateMFA" and message != "":
self.validate_mfa(message)
# sourcery skip: raise-specific-error
self.logger.debug(f"MQTT message received: topic='{topic}' message='{message}'")

try:
if topic == "requestMFA" and message.lower() == "true":
self.request_mfa()
elif topic == "validateMFA" and message != "":
self.validate_mfa(message)
elif topic == "sendCommand" and message != "":
self.handle_command(message)
else:
raise Exception(f"Unknown command '{topic}'")
except Exception as e:
err = (
f"Error occured while processing message "
f"'{message}' from topic '{topic}'",
)
self.handle_exception(err, e)

def handle_exception(self, msg: str, e: Exception) -> None:
self.logger.error(f"{msg}: {e}")
self.logger.debug(f"Exception: {e}", exc_info=True)
self.publish_value_to_mqtt_topic("lastError", msg)

def do_healthy_check(self) -> bool:
return True
Expand Down Expand Up @@ -234,6 +255,21 @@ def validate_mfa(self, message: str) -> None:
else:
self.logger.info("Validate MFA already ongoing")

def handle_command(self, message: str) -> None:
data = json.loads(message)
sn = data["deviceLabel"]
command = data["command"]

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

def update_data_to_mqtt(self, overview: dict) -> None:
self.publish_broadband(overview)
self.publish_locks(overview)
Expand Down Expand Up @@ -301,6 +337,7 @@ def unpack(overview: list, value: str) -> dict | list:
self.verisure.door_window(),
self.verisure.smart_lock(),
self.verisure.smartplugs(),
self.verisure.door_lock_configuration(),
)
self.logger.debug(f"Received data: {overview}")
return {
Expand Down

0 comments on commit 3509d31

Please sign in to comment.