diff --git a/README.md b/README.md index c3ec05b..4c68f63 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/jeedomdaemon/aio_connector.py b/jeedomdaemon/aio_connector.py index 6680a12..608bdbc 100644 --- a/jeedomdaemon/aio_connector.py +++ b/jeedomdaemon/aio_connector.py @@ -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 `::` diff --git a/jeedomdaemon/base_config.py b/jeedomdaemon/base_config.py index 515d1ff..eb21369 100644 --- a/jeedomdaemon/base_config.py +++ b/jeedomdaemon/base_config.py @@ -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 @@ -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) diff --git a/jeedomdaemon/base_daemon.py b/jeedomdaemon/base_daemon.py index 6d8f800..92a4337 100644 --- a/jeedomdaemon/base_daemon.py +++ b/jeedomdaemon/base_daemon.py @@ -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, @@ -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 @@ -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 diff --git a/setup.py b/setup.py index 1b2f677..d1ac2bf 100644 --- a/setup.py +++ b/setup.py @@ -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',