Skip to content

Commit

Permalink
add doc & run_add_change
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed May 11, 2024
1 parent 854be8c commit cbad311
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MyDaemon(BaseDaemon):
"""
pass

def on_stop(self):
async def on_stop(self):
"""
This callback will be called when daemon need to stop`
You need to close your remote connexions and cancel background tasks if any here.
Expand Down
8 changes: 8 additions & 0 deletions jeedomdaemon/aio_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ async def send_to_jeedom(self, payload):
return False
return True

def run_add_change(self, key: str, value):
"""
Will run coroutine add a key/value pair to the payload of the next cycle.
A running loop must exist
"""
_loop = asyncio.get_running_loop()
return asyncio.run_coroutine_threadsafe(self.add_change(key, value), _loop)

async def add_change(self, key: str, value):
"""
Add a key/value pair to the payload of the next cycle, several level can be provided at once by separating key with `::`
Expand Down
13 changes: 8 additions & 5 deletions jeedomdaemon/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class MyOwnConfig(BaseConfig):
def __init__(self):
super().__init__()
self.add_argument("--user", help="username", type=str)
self.add_argument("--password", help="password", type=str)
self.add_argument("--user", type=str, default='john')
self.add_argument("--password", type=str)
```
The first arg is the expected argument from Jeedom, then it's interesting to specify the expected type and if a default value if needed
"""
def __init__(self):
self._args = None
Expand All @@ -42,9 +43,11 @@ def add_argument(self, *args, **kwargs):
"""Add a, argurment to parse.
e.g. from your child class:
* self.add_argument("--clientId", type=str)
* self.add_argument("--intValue", type=int)
```
self.add_argument("--clientId", type=str)
self.add_argument("--intValue", type=int, default=5)
```
The first arg is the expected argument from Jeedom, then it's interesting to specify the expected type and a default value if needed
"""
return self.__parser.add_argument(*args, **kwargs)

Expand Down
43 changes: 38 additions & 5 deletions jeedomdaemon/base_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,40 @@
from .base_config import BaseConfig

class BaseDaemon:
"""Base daemon class"""
"""Base daemon class for your daemon
You should inherit your daemon from it and implement the needed functions amongst on_start, on_message and on_stop
```
class MyDaemon(BaseDaemon):
def __init__(self) -> None:
# Standard initialisation
super().__init__(on_start_cb=self.on_start, on_message_cb=self.on_message, on_stop_cb=self.on_stop)
# Add here any initialisation your daemon would need
async def on_start(self):
# if you don't have specific action to do on start, do not create this method
pass
async def on_message(self, message: list):
pass
async def on_stop(self):
# if you don't have specific action to do on stop, do not create this method
pass
```
To send feeback to Jeedom you have 4 possibilities depending your use case.
If you are in an async method:
* await self._publisher.send_to_jeedom(payload) will send a single message with the given payload
* await self._publisher.add_change(key, value) will add the key/value to the payload of the next cycle
If not:
* self._publisher.run_send_to_jeedom(payload) will send a single message with the given payload
* self._publisher.run_add_change(key, value) will add the key/value to the payload of the next cycle
"""
def __init__(self,
config: BaseConfig = BaseConfig(),
on_start_cb: Callable[..., Awaitable[None]] | None = None,
Expand All @@ -26,7 +59,7 @@ def __init__(self,
self._config.parse()
self.__listen_task: asyncio.Task[None] | None = None
self._loop = None
self._jeedom_publisher: Publisher | None = None
self._publisher: Publisher | None = None
self._logger = logging.getLogger(__name__)
self.__log_level = Utils.convert_log_level(self._config.log_level)
self.__on_start_cb = on_start_cb
Expand Down Expand Up @@ -73,15 +106,15 @@ async def __run(self):
self._loop = asyncio.get_running_loop()
self.__listen_task = Listener.create_listen_task(self._config.socket_host, self._config.socket_port, self.__on_socket_message)

async with Publisher(self._config.callback_url, self._config.api_key) as self._jeedom_publisher:
async with Publisher(self._config.callback_url, self._config.api_key) as self._publisher:
# self._jeedom_publisher = Publisher(self._config.callback_url, self._config.api_key)
if not await self._jeedom_publisher.test_callback():
if not await self._publisher.test_callback():
return

if self.__on_start_cb is not None and asyncio.iscoroutinefunction(self.__on_start_cb):
await self.__on_start_cb()

self._jeedom_publisher.create_send_task()
self._publisher.create_send_task()

await self.__add_signal_handler()
await asyncio.sleep(1) # allow all tasks to start
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Needed for dependencies
install_requires=['aiohttp'],
# *strongly* suggested for sharing
version='0.8.4',
version='0.8.5',
# The license can be anything you like
license='MIT',
description='A base to implement Jeedom daemon in python',
Expand Down

0 comments on commit cbad311

Please sign in to comment.