mirror of
https://github.com/fmtr/haco.git
synced 2026-11-22 16:06:24 -05: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 haco import tuya
|
||||||
from .button import Button
|
from .button import Button
|
||||||
|
from .callback import Response
|
||||||
from .number import Number
|
from .number import Number
|
||||||
from .pulldown import Select
|
from .pulldown import Select
|
||||||
from .sensor import Sensor
|
from .sensor import Sensor
|
||||||
|
|
@ -15,5 +16,6 @@ __all__ = [
|
||||||
"Number",
|
"Number",
|
||||||
"Switch",
|
"Switch",
|
||||||
"Update",
|
"Update",
|
||||||
|
"Response",
|
||||||
"tuya"
|
"tuya"
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -107,3 +107,9 @@ class Callback:
|
||||||
if self.platform == schema.Tasmota.PLATFORM:
|
if self.platform == schema.Tasmota.PLATFORM:
|
||||||
if not self.trigger:
|
if not self.trigger:
|
||||||
raise ValueError(f'Callback "{self.function_name}" must have a 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)
|
platform = self.schema.get(capability_id).get(platform_id)
|
||||||
output = platform.handle(message)
|
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())
|
topic = str(platform.get_topic_publish())
|
||||||
await log_publish(client, topic, output)
|
await log_publish(client, topic, output)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import typing
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from haco import tools, constants
|
from haco import tools, constants, callback
|
||||||
|
|
||||||
DEVICE = 'development-eth'
|
DEVICE = 'development-eth'
|
||||||
CONTROL = 'DEVELOPMENT-DEHUMIDIFIER'
|
CONTROL = 'DEVELOPMENT-DEHUMIDIFIER'
|
||||||
|
|
@ -108,10 +108,15 @@ class Platform:
|
||||||
payload_str = message.payload.decode('utf-8')
|
payload_str = message.payload.decode('utf-8')
|
||||||
payload = self.deserialize(payload_str)
|
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
|
return output
|
||||||
|
|
||||||
def get_callback_output(self, data):
|
def get_callback_output(self, data):
|
||||||
|
|
|
||||||
|
|
@ -99,3 +99,20 @@ def hash_string(input_string, size=16):
|
||||||
hash_result = hash_object.hexdigest()[:size]
|
hash_result = hash_object.hexdigest()[:size]
|
||||||
|
|
||||||
return hash_result
|
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