Compare commits
2 Commits
5fca821637
...
4655b93b64
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4655b93b64 | ||
|
|
cbd91c8873 |
24
conf/apps/globals.py
Normal file
24
conf/apps/globals.py
Normal file
@@ -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
|
||||
@@ -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"
|
||||
12
conf/apps/simple_app/app_a.py
Normal file
12
conf/apps/simple_app/app_a.py
Normal file
@@ -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: ...
|
||||
11
conf/apps/simple_app/app_b.py
Normal file
11
conf/apps/simple_app/app_b.py
Normal file
@@ -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: ...
|
||||
@@ -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
|
||||
@@ -2,10 +2,19 @@ from typing import Any
|
||||
|
||||
from appdaemon.adapi import ADAPI
|
||||
|
||||
# fake_name.get()
|
||||
|
||||
# if:
|
||||
# OUtisde&sdf'asdfasdf'<txF> asdfasdfom some html</.dstd>
|
||||
|
||||
# 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:
|
||||
|
||||
9
conf/apps/simple_app/malformed.py
Normal file
9
conf/apps/simple_app/malformed.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from appdaemon.adapi import ADAPI
|
||||
|
||||
|
||||
class SimpleApp(ADAPI):
|
||||
def initialize(self):
|
||||
self.log(f'{self.__class__.__name__} Initialized')
|
||||
|
||||
if:
|
||||
OUtisde&sdf'asdfasdf'<txF> asdfasdfom some html</.dstd>
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user