mirror of
https://github.com/fmtr/haco.git
synced 2026-10-26 10:51:46 -04:00
Implement Response object
This commit is contained in:
parent
afa30b72b4
commit
08097f816c
5 changed files with 39 additions and 3 deletions
|
|
@ -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"
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue