From c82866a5b8b56a95f3c5f6b10bdafa062a5d9b30 Mon Sep 17 00:00:00 2001 From: acockburn Date: Wed, 6 Dec 2023 15:35:30 -0500 Subject: [PATCH] Stubs for stream replacement of RESt calls in hass --- .vscode/launch.json | 2 +- .vscode/tasks.json | 2 +- appdaemon/appdaemon.py | 23 +++---- appdaemon/plugins/hass/hassplugin.py | 90 +++++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 683c25bc4..9f95a994f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "module": "appdaemon", "justMyCode": true, - "args": "-c /home/hass/ad_config/dev_test --toml" + "args": "-c /home/hass/src/home_automation/ad_config/dev_test --toml" }, ] } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 6ea43bb67..fdb6bf2a3 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,7 +5,7 @@ { "label": "Run Dev", "command": "${command:python.interpreterPath}", - "args": ["-m", "appdaemon", "-c", "/home/hass/appdaemon/dev_test", "--toml"], + "args": ["-m", "appdaemon", "-c", "/home/hass/src/home_automation/ad_config/dev_test", "--toml"], "type": "shell", "presentation": { diff --git a/appdaemon/appdaemon.py b/appdaemon/appdaemon.py index 5366d8c3d..07e1771d8 100755 --- a/appdaemon/appdaemon.py +++ b/appdaemon/appdaemon.py @@ -1,6 +1,6 @@ +import concurrent.futures import os import os.path -import concurrent.futures import threading @@ -10,19 +10,19 @@ def __init__(self, logging, loop, **kwargs): # Import various AppDaemon bits and pieces now to avoid circular import # - import appdaemon.utils as utils - import appdaemon.thread_async as appq - import appdaemon.utility_loop as utility - import appdaemon.plugin_management as plugins - import appdaemon.threading import appdaemon.app_management as apps import appdaemon.callbacks as callbacks - import appdaemon.futures as futures - import appdaemon.state as state import appdaemon.events as events - import appdaemon.services as services - import appdaemon.sequences as sequences + import appdaemon.futures as futures + import appdaemon.plugin_management as plugins import appdaemon.scheduler as scheduler + import appdaemon.sequences as sequences + import appdaemon.services as services + import appdaemon.state as state + import appdaemon.thread_async as appq + import appdaemon.threading + import appdaemon.utility_loop as utility + import appdaemon.utils as utils self.logging = logging self.logging.register_ad(self) @@ -157,6 +157,9 @@ def __init__(self, logging, loop, **kwargs): self.use_dictionary_unpacking = False utils.process_arg(self, "use_dictionary_unpacking", kwargs) + self.use_stream = False + utils.process_arg(self, "use_stream", kwargs) + self.namespaces = {} utils.process_arg(self, "namespaces", kwargs) diff --git a/appdaemon/plugins/hass/hassplugin.py b/appdaemon/plugins/hass/hassplugin.py index 7b38ca277..c56b031a3 100644 --- a/appdaemon/plugins/hass/hassplugin.py +++ b/appdaemon/plugins/hass/hassplugin.py @@ -456,6 +456,17 @@ def utility(self): @hass_check async def set_plugin_state(self, namespace, entity_id, **kwargs): + # if self.AD.use_stream is True: + if False is True: + return await self.set_plugin_state_stream(namespace, entity_id, **kwargs) + else: + return await self.set_plugin_state_rest(namespace, entity_id, **kwargs) + + async def set_plugin_state_stream(self, namespace, entity_id, **kwargs): + self.logger.warning("set_plugin_state_stream() called - not yet implemented]") + return None + + async def set_plugin_state_rest(self, namespace, entity_id, **kwargs): self.logger.debug("set_plugin_state() %s %s %s", namespace, entity_id, kwargs) # if we get a request for not our namespace something has gone very wrong @@ -495,6 +506,17 @@ async def set_plugin_state(self, namespace, entity_id, **kwargs): @hass_check # noqa: C901 async def call_plugin_service(self, namespace, domain, service, data): + # if self.AD.use_stream is True: + if False is True: + return await self.call_plugin_service_stream(namespace, domain, service, data) + else: + return await self.call_plugin_service_rest(namespace, domain, service, data) + + async def call_plugin_service_stream(self, namespace, domain, service, data): + self.logger.warning("all_plugin_service_stream() called - not yet implemented]") + return None + + async def call_plugin_service_rest(self, namespace, domain, service, data): self.logger.debug( "call_plugin_service() namespace=%s domain=%s service=%s data=%s", namespace, @@ -563,6 +585,17 @@ async def call_plugin_service(self, namespace, domain, service, data): return None async def get_history(self, **kwargs): + # if self.AD.use_stream is True: + if False is True: + return await self.get_history_stream(**kwargs) + else: + return await self.get_history_rest(**kwargs) + + async def get_history_stream(self, **kwargs): + self.logger.warning("get_history_stream() called - not yet implemented]") + return None + + async def get_history_rest(self, **kwargs): """Used to get HA's History""" try: @@ -655,6 +688,17 @@ def as_datetime(args, key): return apiurl async def get_hass_state(self, entity_id=None): + # if self.AD.use_stream is True: + if False is True: + return await self.get_hass_state_stream(entity_id) + else: + return await self.get_hass_state_rest(entity_id) + + async def get_hass_state_stream(self, entity_id): + self.logger.warning("get_hass_state_stream() called - not yet implemented]") + return None + + async def get_hass_state_rest(self, entity_id): if entity_id is None: api_url = f"{self.ha_url}/api/states" else: @@ -701,6 +745,17 @@ def validate_tz(self, meta): raise async def get_hass_config(self): + # if self.AD.use_stream is True: + if False is True: + return await self.get_hass_config_stream() + else: + return await self.get_hass_config_rest() + + async def get_hass_config_stream(self): + self.logger.warning("get_hass_config_stream() called - not yet implemented]") + return None + + async def get_hass_config_rest(self): try: self.logger.debug("get_ha_config()") api_url = f"{self.ha_url}/api/config" @@ -722,7 +777,18 @@ async def get_hass_config(self): self.logger.warning("Error getting metadata - retrying: %s", str(ex)) raise - async def get_hass_services(self) -> dict: + async def get_hass_services(self): + # if self.AD.use_stream is True: + if False is True: + return await self.get_hass_services_stream() + else: + return await self.get_hass_services_rest() + + async def get_hass_services_stream(self): + self.logger.warning("get_hass_services_stream() called - not yet implemented]") + return None + + async def get_hass_services_rest(self) -> dict: try: self.logger.debug("get_hass_services()") @@ -831,6 +897,17 @@ async def check_register_service(self, domain: str, services: Union[dict, str]) @hass_check async def fire_plugin_event(self, event, namespace, **kwargs): + # if self.AD.use_stream is True: + if False is True: + return await self.fire_plugin_event_stream(event, namespace, **kwargs) + else: + return await self.fire_plugin_event_rest(event, namespace, **kwargs) + + async def fire_plugin_event_stream(self, event, namespace, **kwargs): + self.logger.warning("fire_plugin_event_stream() called - not yet implemented]") + return None + + async def fire_plugin_event_rest(self, event, namespace, **kwargs): self.logger.debug("fire_event: %s, %s %s", event, namespace, kwargs) # if we get a request for not our namespace something has gone very wrong @@ -861,6 +938,17 @@ async def fire_plugin_event(self, event, namespace, **kwargs): @hass_check async def remove_entity(self, namespace, entity_id): + # if self.AD.use_stream is True: + if False is True: + return await self.remove_entity_stream(namespace, entity_id) + else: + return await self.remove_entity_rest(namespace, entity_id) + + async def remove_entity_stream(self, namespace, entity_id): + self.logger.warning("remove_entity_stream() called - not yet implemented]") + return None + + async def remove_entity_rest(self, namespace, entity_id): self.logger.debug("remove_entity() %s", entity_id) # if we get a request for not our namespace something has gone very wrong