Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation and comments typo #7

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Description

This library provide everything needed to build a daemon for a plugin for Jeedom in python.
This library provides everything needed to build a daemon for a plugin for Jeedom in python.
It's possible to get a daemon skeleton by typing literally less than 5 lines of code.

## Requirements
Expand Down Expand Up @@ -76,8 +76,8 @@ class MyDaemon(BaseDaemon):

async def on_start(self):
"""
This method will be called when your daemon start.
This is the place where you should create yours tasks, login to remote system, etc
This method will be called when your daemon starts.
This is the place where you should create your tasks, login to remote system, etc
"""
# if you don't have specific action to do on start, do not create this method
pass
Expand All @@ -92,7 +92,7 @@ class MyDaemon(BaseDaemon):

async def on_stop(self):
"""
This callback will be called when daemon need to stop`
This callback will be called when the daemon needs to stop`
You need to close your remote connexions and cancel background tasks if any here.
"""
# if you don't have specific action to do on stop, do not create this method
Expand All @@ -107,11 +107,11 @@ Without additional work, your daemon will accept following argument when started

* --loglevel - a string (Jeedom format) giving the log Level for the daemon
* --sockethost - usually not needed, default is '127.0.0.1'
* --socketport - port on which the daemon will open a tcp socket to listen for incomming message from your php code
* --socketport - port on which the daemon will open a tcp socket to listen for incomming messages from your php code
* --callback - callback url to use by your daemon to send data to your php code
* --apikey - the apikey use to valid communication
* --apikey - the api key to validate communication
* --pid - the pid filename
* --cycle - a float value giving at which frequency daemon should send request to your PHP code, by default every 0.5s (max)
* --cycle - a float value giving at which frequency the daemon should send requests to your PHP code, by default every 0.5s (max)

It will happen that you need to receive some additional values from Jeedom to be able to start your daemon, like a user & password to login somewhere. In that case create a child class like in this example and provide it during daemon initialisation:

Expand Down Expand Up @@ -141,4 +141,4 @@ class MyDaemon(BaseDaemon):

## What's next

I suggest you to take a look at this [demo plugin](https://github.com/Mips2648/jeedom-aiodemo) which implement this library
I suggest you to take a look at this [demo plugin](https://github.com/Mips2648/jeedom-aiodemo) which implements this library
14 changes: 7 additions & 7 deletions jeedomdaemon/aio_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

class Listener():
"""
This class allow to create an asyncio task that will open a socket server and listen to it until task is canceled.
`on_message` call_back will be call with the message as a list as argument
This class allows to create an asyncio task that will open a socket server and listen to it until the task is canceled.
`on_message` callback will be called with the message as a list as argument
"""
def __new__(cls, *args, **kwargs):
if not hasattr(cls, 'instance'):
Expand All @@ -31,7 +31,7 @@ def create_listen_task(socket_host: str, socket_port: int, on_message_cb: Callab
return asyncio.create_task(listener.listen())

async def listen(self):
""" listen function, as task should be made out of it. Don't use this function directly by use `create_listen_task()` instead"""
""" listen function, a task should be made out of it. Don't use this function directly but use `create_listen_task()` instead"""
try:
server = await asyncio.start_server(self.__handle_read, self._socket_host, port=self._socket_port)

Expand All @@ -53,7 +53,7 @@ async def __handle_read(self, reader: asyncio.StreamReader, writer: asyncio.Stre
await self._on_message_cb(json.loads(message))

class Publisher():
"""This class allow to push information to Jeedom either immediately by calling function `send_to_jeedom` or in cycle by calling function `add_change`. For the "cycle" mode, a task must be created by calling `create_send_task` and awaited"""
"""This class allows to push information to Jeedom either immediately by calling function `send_to_jeedom` or in cycle by calling function `add_change`. For the "cycle" mode, a task must be created by calling `create_send_task` and awaited"""
def __init__(self, callback_url: str, api_key: str, cycle: float = 0.5) -> None:
self._jeedom_session = aiohttp.ClientSession()
self._callback_url = callback_url
Expand All @@ -77,7 +77,7 @@ def create_send_task(self):
return asyncio.create_task(self.__send_task())

async def test_callback(self):
"""test_callback will return true if communication with Jeedom is sucessfull or false otherwise"""
"""test_callback will return true if the communication with Jeedom is sucessfull or false otherwise"""
try:
async with self._jeedom_session.get(self._callback_url + '?test=1&apikey=' + self._api_key) as resp:
if resp.status != 200:
Expand Down Expand Up @@ -116,7 +116,7 @@ async def __send_task(self):
async def send_to_jeedom(self, payload):
"""
Will send the payload provided.
return true or false if successful
return true if successful or false otherwise
"""
self._logger.debug('Send to jeedom: %s', payload)
try:
Expand All @@ -132,7 +132,7 @@ async def send_to_jeedom(self, payload):

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 `::`
Add a key/value pair to the payload of the next cycle, several levels can be provided at once by separating keys with `::`
If a key already exists the value will be replaced by the newest
"""
if key.find('::') != -1:
Expand Down
26 changes: 13 additions & 13 deletions jeedomdaemon/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from typing import Sequence

class BaseConfig():
"""Base config class, if you need custom configuration you can inherit from this class
"""Base config class, if you need a custom configuration you can inherit from this class

This class support following argument, you don't need to declare them:
This class supports following arguments, you don't need to declare them:
* --loglevel
* --sockethost
* --socketport
Expand All @@ -17,7 +17,7 @@ class BaseConfig():
* --pid
* --cycle

If you need additionals arguments then simply create a child class and them in your constructor, e.g.:
If you need additional arguments then simply create a child class and add them in your constructor, e.g.:
```
class MyOwnConfig(BaseConfig):
def __init__(self):
Expand All @@ -26,7 +26,7 @@ def __init__(self):
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
The first arg is the expected argument from Jeedom, then it's interesting to specify the expected type and a default value if needed
"""
def __init__(self):
self._args = None
Expand All @@ -40,7 +40,7 @@ def __init__(self):
self.add_argument("--cycle", help="cycle", type=float, default=0.5)

def add_argument(self, *args, **kwargs):
"""Add a, argurment to parse.
"""Add an argurment to parse.

e.g. from your child class:
```
Expand All @@ -52,7 +52,7 @@ def add_argument(self, *args, **kwargs):
return self.__parser.add_argument(*args, **kwargs)

def parse(self, args: Sequence[str] | None = None):
"""Actually parse de config, it will be done for you at daemon start."""
"""Actually parses the config, it will be done for you at daemon start."""
if self._args is None:
self._args = self.__parser.parse_args(args)

Expand All @@ -61,35 +61,35 @@ def __getattr__(self, name):

@property
def callback_url(self):
"""Return the callback url to Jeedom."""
"""Returns the callback url to Jeedom."""
return str(self._args.callback)

@property
def socket_host(self):
"""Return the daemon socket host."""
"""Returns the daemon socket host."""
return str(self._args.sockethost)

@property
def socket_port(self):
"""Return the daemon socket port."""
"""Returns the daemon socket port."""
return int(self._args.socketport)

@property
def log_level(self):
"""Return the log level."""
"""Returns the log level."""
return str(self._args.loglevel)

@property
def api_key(self):
"""Return the api key."""
"""Returns the api key."""
return str(self._args.apikey)

@property
def pid_filename(self):
"""Return the pid."""
"""Returns the pid."""
return str(self._args.pid)

@property
def cycle(self):
"""Return the cycle."""
"""Returns the cycle."""
return float(self._args.cycle)
16 changes: 8 additions & 8 deletions jeedomdaemon/base_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def on_stop(self):
pass
```

To send feeback to Jeedom you have 4 possibilities depending your use case.
To send feeback to Jeedom you have 4 possibilities depending on your use case.
If you are in an async method:
* await self.send_to_jeedom(payload) will send a single message with the given payload
* await self.add_change(key, value) will add the key/value to the payload of the next cycle
Expand Down Expand Up @@ -75,11 +75,11 @@ def set_logger_log_level(self, logger_name: str):

@property
def log_level(self):
""" Return the log level"""
""" Returns the log level"""
return self.__log_level

def run(self):
""" Run your daemon, this is the function you should call! """
""" Runs your daemon, this is the function you should call!"""
try:
self._logger.info('Starting daemon with log level: %s', self._config.log_level)
Utils.write_pid(str(self._config.pid_filename))
Expand Down Expand Up @@ -124,7 +124,7 @@ async def __run(self):
await self.__listen_task

async def stop(self):
""" Stop your daemon if need be"""
"""Stops your daemon if it needs to be"""

if self.__on_stop_cb is not None:
await self.__on_stop_cb()
Expand Down Expand Up @@ -162,25 +162,25 @@ async def __on_socket_message(self, message):
async def send_to_jeedom(self, payload):
"""
Will send the payload provided.
return true or false if successful
returns true if successful or false otherwise
"""
return await self._publisher.send_to_jeedom(payload)

def create_task_send_to_jeedom(self, payload):
"""
Will create a task to with coroutine to send the payload provided.
Will create a task with coroutine to send the payload provided.
"""
self._loop.create_task(self._publisher.send_to_jeedom(payload))

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 `::`
Add a key/value pair to the payload of the next cycle, several levels can be provided at once by separating keys with `::`
If a key already exists the value will be replaced by the newest
"""
await self._publisher.add_change(key, value)

def create_task_add_change(self, key: str, value):
"""
Will create a task to with coroutine to add a key/value pair to the payload of the next cycle.
Will create a task with coroutine to add a key/value pair to the payload of the next cycle.
"""
self._loop.create_task(self._publisher.add_change(key, value))
8 changes: 4 additions & 4 deletions tests/base_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
class TestBaseConfig(unittest.TestCase):
def test_base_config_creation(self):
"""
Test that it can create a basic config parser
Tests if it can create a basic config parser
"""
config = BaseConfig()
config.parse([])
self.assertEqual(config.socket_host, "127.0.0.1")

def test_base_config_parse(self):
"""
Test that it can parse config
Tests if it can parse config
"""
config = BaseConfig()
config.parse(['--loglevel', 'info', '--socketport', '42000', '--callback', 'http://localhost/path', '--apikey', 'cnysltyql', '--pid', '123'])
Expand All @@ -33,7 +33,7 @@ def test_base_config_parse(self):

def test_custom_config_parse_with_property(self):
"""
Test that it can parse config
Tests if it can parse config
"""
class TestConfig(BaseConfig):
def __init__(self):
Expand All @@ -50,7 +50,7 @@ def client_id(self):

def test_custom_config_parse_without_property(self):
"""
Test that it can parse config
Tests if it can parse config
"""
class TestConfig(BaseConfig):
def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/base_daemon_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _get_test_config(self):
@patch("builtins.open", new_callable=mock_open)
def test_base_daemon_creation(self, dummy_file):
"""
Test that it can create a basic daemon
Tests if it can create a basic daemon
"""
config = self._get_test_config()
testdaemon = BaseDaemon(config)
Expand Down