Add image services

This commit is contained in:
2026-06-13 16:50:46 +02:00
parent a49fc43ae5
commit 2ce592827c
+145 -9
View File
@@ -5,11 +5,11 @@ from buttonplus_generator.constants import *
from buttonplus_generator.utils import bar_id, display_id
def config_services(version: int) -> Dict[str, Any]:
def config_services(version: int, token: str) -> Dict[str, Any]:
# Toast needs to be after notification, so that the toast is temporarily shown over a notification if needed
config = notification_service(version=version)
config = deep_merge(config, toast_service())
# config = deep_merge(config, image_service())
config = deep_merge(config, image_service(token=token))
return config
@@ -18,9 +18,9 @@ def toast_service() -> Dict[str, Any]:
generated_cpp = (
"if(id(internal_toast).size() > 0){\n"
" // Draw white box over existing text\n"
f" id({display_id(IdSuffix.DISPLAY)}).filled_rectangle(0, 200, 320, 40, white);\n"
f" id({display_id(IdSuffix.DISPLAY)}).filled_rectangle(0, 200, 320, 40, white);\n"
" // Show toast message\n"
f" id({display_id(IdSuffix.DISPLAY)}).print(160, 235, id(font_arial20), id(black), TextAlign::BOTTOM_CENTER, id(internal_toast).c_str());\n"
f" id({display_id(IdSuffix.DISPLAY)}).print(160, 235, id(font_arial20), id(black), TextAlign::BOTTOM_CENTER, id(internal_toast).c_str());\n"
"}\n"
)
@@ -28,7 +28,12 @@ def toast_service() -> Dict[str, Any]:
"globals": [
{"id": "internal_toast", "type": "std::string", "initial_value": ""}
],
"script": [{"id": display_id(IdSuffix.SCRIPT_DRAW), "then": [{"lambda": generated_cpp}]}],
"script": [
{
"id": display_id(IdSuffix.SCRIPT_DRAW),
"then": [{"lambda": generated_cpp}],
}
],
"api": {
"actions": [
{
@@ -61,10 +66,10 @@ def notification_service(version: int) -> Dict[str, Any]:
generated_cpp = (
"if(id(internal_notify).size() > 0){\n"
" // Draw white box over existing text\n"
f" id({display_id(IdSuffix.DISPLAY)}).filled_rectangle(0, 200, 320, 40, white);\n"
f" id({display_id(IdSuffix.DISPLAY)}).rectangle(0, 200, 320, 40, accent);\n"
f" id({display_id(IdSuffix.DISPLAY)}).filled_rectangle(0, 200, 320, 40, white);\n"
f" id({display_id(IdSuffix.DISPLAY)}).rectangle(0, 200, 320, 40, accent);\n"
" // Show notification message\n"
f" id({display_id(IdSuffix.DISPLAY)}).print(160, 235, id(font_arial20), id(black), TextAlign::BOTTOM_CENTER, id(internal_notify).c_str());\n"
f" id({display_id(IdSuffix.DISPLAY)}).print(160, 235, id(font_arial20), id(black), TextAlign::BOTTOM_CENTER, id(internal_notify).c_str());\n"
"}\n"
)
@@ -84,7 +89,12 @@ def notification_service(version: int) -> Dict[str, Any]:
"globals": [
{"id": "internal_notify", "type": "std::string", "initial_value": ""}
],
"script": [{"id": display_id(IdSuffix.SCRIPT_DRAW), "then": [{"lambda": generated_cpp}]}],
"script": [
{
"id": display_id(IdSuffix.SCRIPT_DRAW),
"then": [{"lambda": generated_cpp}],
}
],
"api": {
"actions": [
{
@@ -191,3 +201,129 @@ def notification_service(version: int) -> Dict[str, Any]:
)
return service_dict
def image_service(token: str) -> Dict[str, Any]:
image_config = _clear_image_service(len(token) > 0)
image_config = deep_merge(image_config, _image_service("web"))
if(len(token) > 0):
image_config = deep_merge(image_config, _image_service("ha", token=token))
return image_config
def _clear_image_service(ha_image_service: bool) -> Dict[str, Any]:
action_list = [
{
"globals.set": {
"id": "show_web_image",
"value": YAMLLambda("return false;"),
},
},
{"online_image.release": "web_image"}
]
ha_list = []
if ha_image_service:
ha_list = [
{
"globals.set": {
"id": f"show_ha_image",
"value": YAMLLambda("return false;"),
}
},
{"online_image.release": "ha_image"}
]
action_list.extend(ha_list)
action_list.append({"script.execute": display_id(IdSuffix.SCRIPT_UPDATE)})
image_dict = {
"api": {
"actions": [
{
"action": "clear_image",
"then": action_list
}
]
},
}
return image_dict
def _image_service(key: str, token: str = "") -> Dict[str, Any]:
generated_cpp = (
f"if(id(show_{key}_image)){{\n"
f" // Draw {key} Image\n"
f" id({display_id(IdSuffix.DISPLAY)}).image(0, 0, id({key}_image));\n"
"}\n"
)
headers = {}
if(len(token) > 0):
headers = {"Authorization": f"Bearer {token}"}
image_dict = {
"globals": [
{"id": f"show_{key}_image", "type": "bool", "initial_value": "'false'"}
],
"script": [
{
"id": display_id(IdSuffix.SCRIPT_DRAW),
"then": [{"lambda": generated_cpp}],
}
],
"api": {
"actions": [
{
"action": f"show_{key}_jpeg_image",
"variables": {"timeout": "int", "image_url": "string"},
"then": [
{
"online_image.set_url": {
"id": f"{key}_image",
"url": YAMLLambda("return image_url;"),
}
},
{
"globals.set": {
"id": f"show_{key}_image",
"value": YAMLLambda("return true;"),
}
},
{"delay": YAMLLambda("return timeout * 1000;")},
{
"globals.set": {
"id": f"show_{key}_image",
"value": YAMLLambda("return false;"),
}
},
{"online_image.release": f"{key}_image"},
{"script.execute": display_id(IdSuffix.SCRIPT_UPDATE)},
],
}
]
},
"http_request": {},
"online_image": [
{
"id": f"{key}_image",
"url": "https://invalid.jpg",
"type": "RGB565",
"format": "JPG",
"resize": "320x240",
"update_interval": "never",
"on_download_finished":{
"then": [
{"script.execute": display_id(IdSuffix.SCRIPT_UPDATE)},
]
},
"request_headers": headers
}
],
}
return image_dict