Skip to content

Commit

Permalink
Merge pull request #16 from jotonedev/6-implement-gateway
Browse files Browse the repository at this point in the history
Implement gateway management
  • Loading branch information
jotonedev authored Oct 27, 2024
2 parents 09948f3 + 217f3a4 commit 2cc07dd
Show file tree
Hide file tree
Showing 14 changed files with 695 additions and 7 deletions.
19 changes: 19 additions & 0 deletions docs/api/items/gateway.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Gateway module
summary: Provides the main interface to the OpenWebNet gateway.
---

The Gateway module provides the main interface to the OpenWebNet gateway.

::: pyown.items.gateway.gateway.WhatGateway
options:
show_inheritance_diagram: true

::: pyown.items.gateway.gateway.GatewayModel
options:
show_inheritance_diagram: true

::: pyown.items.gateway.gateway.Gateway
options:
show_inheritance_diagram: true

3 changes: 1 addition & 2 deletions examples/automation_02/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import asyncio
import logging

from pyown.client import Client
from pyown.client import Client, SessionType
from pyown.items.automation import Automation, WhatAutomation
from pyown.protocol import SessionType

log = logging.getLogger(__name__)

Expand Down
3 changes: 3 additions & 0 deletions examples/gateway_01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# light_01

This example demonstrates how to create a simple light source and control it.
62 changes: 62 additions & 0 deletions examples/gateway_01/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import asyncio
import logging

from pyown.client import Client
from pyown.items import Gateway


async def run(host: str, port: int, password: str):
client = Client(
host=host,
port=port,
password=password,
)

await client.start()

gateway = Gateway(
client=client
)

# get ip address of the gateway
ip = await gateway.get_ip()
print(ip)

# get the model of the gateway
model = await gateway.get_model()
print(model.name)

# get datetime of the gateway
datetime = await gateway.get_datetime()
print(datetime)

# get the kernel version of the gateway
kernel = await gateway.get_kernel_version()
print(kernel)

await client.close()


def main(host: str, port: int, password: str):
# Set the logging level to DEBUG
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

# Run the asyncio event loop
asyncio.run(run(host, port, password))


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, help="The host to connect to", default="192.168.1.35")
parser.add_argument("--port", type=int, help="The port to connect to", default=20000)
parser.add_argument("--password", type=str, help="The password to authenticate with", default="12345")

args = parser.parse_args()

main(args.host, args.port, args.password)
3 changes: 3 additions & 0 deletions examples/gateway_02/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# light_01

This example demonstrates how to create a simple light source and control it.
53 changes: 53 additions & 0 deletions examples/gateway_02/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import asyncio
import logging

from black import datetime

from pyown.client import Client, SessionType
from pyown.items import Gateway, WhatGateway


async def on_time_change(gateway: Gateway, time: datetime.time):
print(f"Time of the gateway is now {time}")


async def run(host: str, port: int, password: str):
client = Client(
host=host,
port=port,
password=password,
session_type=SessionType.EventSession
)

Gateway.register_callback(
WhatGateway.TIME,
on_time_change
)

await client.start()
await client.loop()


def main(host: str, port: int, password: str):
# Set the logging level to DEBUG
logging.basicConfig(
level=logging.WARN,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

# Run the asyncio event loop
asyncio.run(run(host, port, password))


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, help="The host to connect to", default="192.168.1.35")
parser.add_argument("--port", type=int, help="The port to connect to", default=20000)
parser.add_argument("--password", type=str, help="The password to authenticate with", default="12345")

args = parser.parse_args()

main(args.host, args.port, args.password)
2 changes: 1 addition & 1 deletion pyown/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async def loop(self, *, client: BaseClient | None = None):
for item_obj in BaseItem.__subclasses__():
if message.who == item_obj.who:
try:
tasks = item_obj.call_callbacks(item, message)
tasks = await item_obj.call_callbacks(item, message)
except InvalidMessage as e:
log.warning(f"Message not supported {e.message}")
else:
Expand Down
4 changes: 3 additions & 1 deletion pyown/items/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .lighting import Light, Dimmer
from .lighting import *
from .automation import *
from .gateway import *
2 changes: 1 addition & 1 deletion pyown/items/automation/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def on_status_change(cls, callback: Callable[[Self, WhatAutomation], Coroutine[N
cls._event_callbacks.setdefault(AutomationEvents.ALL, []).append(callback)

@classmethod
def call_callbacks(cls, item: Self, message: BaseMessage) -> list[Task]:
async def call_callbacks(cls, item: Self, message: BaseMessage) -> list[Task]:
tasks: list[Task] = []

if isinstance(message, NormalMessage):
Expand Down
2 changes: 1 addition & 1 deletion pyown/items/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _create_tasks(funcs: list[CoroutineCallback], *args: Any) -> list[Task]:

@classmethod
@abstractmethod
def call_callbacks(cls, item: Self, message: BaseMessage) -> list[Task]:
async def call_callbacks(cls, item: Self, message: BaseMessage) -> list[Task]:
"""
Calls the registered callbacks for the event.
Used internally by the client to dispatch the events to the correct callbacks.
Expand Down
1 change: 1 addition & 0 deletions pyown/items/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .gateway import *
Loading

0 comments on commit 2cc07dd

Please sign in to comment.