From cbd91c8873bc9210369f8ea6ba0e4f6ad350937f Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Thu, 5 Jun 2025 22:37:56 -0500 Subject: [PATCH] added some more examples --- conf/apps/globals.py | 24 +++++++++ conf/apps/scratch.yaml | 36 ------------- conf/apps/simple_app/app_a.py | 12 +++++ conf/apps/simple_app/app_b.py | 11 ++++ conf/apps/simple_app/apps.yaml | 20 ++++++- conf/apps/simple_app/hello.py | 9 ++++ conf/apps/simple_app/simple.py | 98 +++++++++++++++++++++++++++++++++- 7 files changed, 171 insertions(+), 39 deletions(-) create mode 100644 conf/apps/globals.py delete mode 100644 conf/apps/scratch.yaml create mode 100644 conf/apps/simple_app/app_a.py create mode 100644 conf/apps/simple_app/app_b.py diff --git a/conf/apps/globals.py b/conf/apps/globals.py new file mode 100644 index 0000000..d0eb442 --- /dev/null +++ b/conf/apps/globals.py @@ -0,0 +1,24 @@ +from enum import Enum + +GLOBAL_VAR = "Hello, World!" + + +def global_function(): + print('This is a global function.') + + +class GlobalClass: + def __init__(self): + self.value = 'This is a global class instance.' + + def display(self): + print(self.value) + + +class ModeSelect(Enum): + MODE_A = 'mode_a' + MODE_B = 'mode_b' + MODE_C = 'mode_c' + + +GLOBAL_MODE = ModeSelect.MODE_C diff --git a/conf/apps/scratch.yaml b/conf/apps/scratch.yaml deleted file mode 100644 index e0ad7c4..0000000 --- a/conf/apps/scratch.yaml +++ /dev/null @@ -1,36 +0,0 @@ -# scratch1: -# module: "scratch" -# class: "Scratch" -# priority: 1 -# dependencies: -# - scratch2 - -dummy: - module: dummy - class: Dummy - dependencies: - - dev - -eboon: - module: eboon - class: Eboon - -dev: - module: notifications - class: NotifyDev - -# scratch3: -# module: "scratch" -# class: "Scratch" -# scratch4: -# module: "scratch" -# class: "Scratch" -# scratch5: -# module: "scratch" -# class: "Scratch" -# scratch6: -# module: "scratch" -# class: "Scratch" -# scratch7: -# module: "scratch" -# class: "Scratch" diff --git a/conf/apps/simple_app/app_a.py b/conf/apps/simple_app/app_a.py new file mode 100644 index 0000000..a044d3f --- /dev/null +++ b/conf/apps/simple_app/app_a.py @@ -0,0 +1,12 @@ + +from appdaemon.adapi import ADAPI +from globals import GLOBAL_MODE, GLOBAL_VAR + + +class AppA(ADAPI): + def initialize(self) -> None: + self.log(f'{self.__class__.__name__} Initialized') + self.log(GLOBAL_VAR) + self.log(f'Global mode is set to: {GLOBAL_MODE.value}') + + def terminate(self) -> None: ... diff --git a/conf/apps/simple_app/app_b.py b/conf/apps/simple_app/app_b.py new file mode 100644 index 0000000..a4e4b13 --- /dev/null +++ b/conf/apps/simple_app/app_b.py @@ -0,0 +1,11 @@ +from appdaemon.adapi import ADAPI +from globals import GLOBAL_MODE, GLOBAL_VAR + + +class AppB(ADAPI): + def initialize(self) -> None: + self.log(f'{self.__class__.__name__} Initialized') + self.log(GLOBAL_VAR) + self.log(f'Global mode is set to: {GLOBAL_MODE.value}') + + def terminate(self) -> None: ... diff --git a/conf/apps/simple_app/apps.yaml b/conf/apps/simple_app/apps.yaml index e75f5b7..e940ecc 100644 --- a/conf/apps/simple_app/apps.yaml +++ b/conf/apps/simple_app/apps.yaml @@ -1,10 +1,28 @@ hello_world: module: hello class: HelloWorld + extra: abc123 + # disable: true simple_app: module: simple class: SimpleApp extra: 1234 + priority: 10 + # dependencies: + # - hello_world + + +base_app: + module: simple + class: BaseApp + +AppA: + module: app_a + class: AppA dependencies: - - hello_world + - AppB # This is only set to demonstrate forcing it to load after AppB + +AppB: + module: app_b + class: AppB \ No newline at end of file diff --git a/conf/apps/simple_app/hello.py b/conf/apps/simple_app/hello.py index 8c0d10c..e07badf 100644 --- a/conf/apps/simple_app/hello.py +++ b/conf/apps/simple_app/hello.py @@ -2,10 +2,19 @@ from typing import Any from appdaemon.adapi import ADAPI +# fake_name.get() + +# if: +# OUtisde&sdf'asdfasdf' asdfasdfom some html + +# fake/ +# SimpleApp class HelloWorld(ADAPI): def initialize(self): self.log(f'{self.__class__.__name__} Initialized') + self.log('+' * 50) + # fake self.register_service("my_domain/my_exciting_service", self.my_exciting_cb) def my_exciting_cb(self, *args: str, my_arg: int = 0, **kwargs: Any) -> Any: diff --git a/conf/apps/simple_app/simple.py b/conf/apps/simple_app/simple.py index 4a60ccc..1df4191 100644 --- a/conf/apps/simple_app/simple.py +++ b/conf/apps/simple_app/simple.py @@ -1,6 +1,100 @@ -from appdaemon.adapi import ADAPI -class SimpleApp(ADAPI): + +from datetime import datetime +from typing import Any + +from appdaemon import adbase as ad +from appdaemon import utils +from appdaemon.plugins.hass import Hass + +# from globals import GLOBAL_MODE, GLOBAL_VAR + + +class SimpleApp(Hass): def initialize(self): + match self.ping(): + case float() as ping: + ping = utils.format_timedelta(ping) + self.log(f'{self.__class__.__name__} Initialized: {ping}') + case _: + pass + + # self.get_entity('input_button.test_button').listen_state(self.handle_button) + + # self.set_app_pin(True) + + # Listen for a button press event with a specific entity_id + self.listen_event( + self.handle_button, + 'call_service', + service='press', + entity_id='input_button.test_button' + ) + + def handle_button(self, event_type: str, data: dict[str, Any], **kwargs: Any) -> None: + match data: + case {"service_data": {"entity_id": eid}}: + friendly_name = self.get_state(eid, attribute='friendly_name') + self.log(f'pressed {friendly_name}') + case _: + self.log(f'Unhandled button press: {data}', level='WARNING') + + def delayed(self, **kwargs): + self.log(f'This is a delayed log message: {kwargs}') + # self.reload_apps() + # self.stop_app('child_app') + + def my_callback(self, event_type: str, data: dict[str, Any], **kwargs: Any) -> None: + match data: + case { + "service_data": {"entity_id": eid}, + "metadata": {"time_fired": time_fired} + }: + friendly_name = self.get_state(eid, attribute='friendly_name') + time_fired = datetime.fromisoformat(time_fired).astimezone(self.AD.tz) + fmt = "%I:%M:%S %p" + self.log(f'{friendly_name} was pressed at {time_fired.strftime(fmt)}') + self.log(f'Kwargs: {kwargs}') + case _: + self.log(f'Unhandled button press: {data}', level='WARNING') + + @ad.global_lock + def write(self): + return + + # def test_event_handler(self, event_type: str, data: dict[str, Any], **kwargs: Any) -> None: + def test_event_handler(self, *_, value_at_listen, **kwargs: Any) -> None: + # self.log(f' {event_type} '.center(30, '#')) + self.log(f'Data from event: {value_at_listen}') + # self.log(f'Data from registration: {kwargs}') + return + + +class BaseApp(ad.ADBase): + def initialize(self): + self.adapi = self.get_ad_api() + self.log = self.adapi.log + self.hassapi = self.get_plugin_api("HASS") + assert isinstance(self.hassapi, Hass) self.log(f'{self.__class__.__name__} Initialized') + self.config_model = {'name': self.__class__.__name__, 'module': 'simple_app', 'class': 'SimpleApp'} + + self.global_vars['abc'] = 123 + self.config + self.app_config + # self.log(f'Global mode is set to: {GLOBAL_MODE} ({GLOBAL_MODE.value})') + self.ad = self.AD + + # self.adapi.run_in(self.delayed, 0.75) + # self.alexa_notify("Home assistant has been restarted", target="fake") + + self.adapi.listen_event( + self.handle_button, + 'call_service', + service='press', + entity_id='input_button.test_button' + ) + + def handle_button(self, event_type: str, data: dict[str, Any], **kwargs: Any) -> None: + self.adapi.fire_event("test_event", values_at_fire=123)