diff --git a/haco/__init__.py b/haco/__init__.py index 2ea5f09..e2c345f 100644 --- a/haco/__init__.py +++ b/haco/__init__.py @@ -1,5 +1,6 @@ from haco import tuya from .button import Button +from .callback import Response from .number import Number from .pulldown import Select from .sensor import Sensor @@ -15,5 +16,6 @@ __all__ = [ "Number", "Switch", "Update", + "Response", "tuya" ] diff --git a/haco/callback.py b/haco/callback.py index 7f8bb1d..dab8018 100644 --- a/haco/callback.py +++ b/haco/callback.py @@ -107,3 +107,9 @@ class Callback: if self.platform == schema.Tasmota.PLATFORM: if not self.trigger: raise ValueError(f'Callback "{self.function_name}" must have a trigger.') + + +class Response: + def __init__(self, value=None, send=True): + self.value = value + self.send = send diff --git a/haco/control.py b/haco/control.py index 20bb0b8..4aa9551 100644 --- a/haco/control.py +++ b/haco/control.py @@ -101,6 +101,12 @@ class Control: platform = self.schema.get(capability_id).get(platform_id) output = platform.handle(message) + + if output == 'DO_NOT_SEND': + logging.warning( + f'Topic {message.topic.value} callback responded with send=False. Will not publish response.') + return + topic = str(platform.get_topic_publish()) await log_publish(client, topic, output) diff --git a/haco/schema.py b/haco/schema.py index d55d853..e3089d0 100644 --- a/haco/schema.py +++ b/haco/schema.py @@ -4,7 +4,7 @@ import typing from pathlib import Path from typing import List -from haco import tools, constants +from haco import tools, constants, callback DEVICE = 'development-eth' CONTROL = 'DEVELOPMENT-DEHUMIDIFIER' @@ -108,10 +108,15 @@ class Platform: payload_str = message.payload.decode('utf-8') payload = self.deserialize(payload_str) - output_raw = self.get_callback_output(payload) + response = self.get_callback_output(payload) - output = self.serialize(self.convert_out(output_raw)) + if type(response) is not callback.Response: + response = callback.Response(value=response) + if not response.send: + return 'DO_NOT_SEND' + + output = self.serialize(self.convert_out(response.value)) return output def get_callback_output(self, data): diff --git a/haco/tools.py b/haco/tools.py index 48a086a..a2b3c73 100644 --- a/haco/tools.py +++ b/haco/tools.py @@ -99,3 +99,20 @@ def hash_string(input_string, size=16): hash_result = hash_object.hexdigest()[:size] return hash_result + + +def get_latest(org, repo): + import requests + url = f"https://api.github.com/repos/{org}/{repo}/releases/latest" + + headers = { + 'Accept': 'application/vnd.github+json', + 'X-GitHub-Api-Version': '2022-11-28' + } + + response = requests.request("GET", url, headers=headers) + response_json = response.json() + + tag = response_json.get('tag_name') + + return tag