Skip to content

Commit

Permalink
Stubs for stream replacement of RESt calls in hass
Browse files Browse the repository at this point in the history
  • Loading branch information
acockburn committed Dec 6, 2023
1 parent 8740033 commit c82866a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
]
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":
{
Expand Down
23 changes: 13 additions & 10 deletions appdaemon/appdaemon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import concurrent.futures
import os
import os.path
import concurrent.futures
import threading


Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
90 changes: 89 additions & 1 deletion appdaemon/plugins/hass/hassplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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()")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c82866a

Please sign in to comment.