Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
jotonedev committed Oct 27, 2024
1 parent 2cc07dd commit a55550e
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 53 deletions.
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ nav:
- Items:
- Base: api/items/base.md
- Lighting: api/items/lighting.md
- Automation: api/items/automation.md
- Automation: api/items/automation.md
- Gateway: api/items/gateway.md
64 changes: 32 additions & 32 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 22 additions & 13 deletions pyown/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..exceptions import InvalidSession, ParseError, InvalidMessage, InvalidTag
from ..items.base import BaseItem
from ..items.utils import ITEM_TYPES
from ..messages import MessageType
from ..messages import MessageType, BaseMessage
from ..tags import Who, Where

__all__ = [
Expand Down Expand Up @@ -135,18 +135,27 @@ async def loop(self, *, client: BaseClient | None = None):
log.info(f"Item type not supported, WHO={who}, WHERE={where}")
continue

# because callbacks are defined inside items, we need to loop through all items to find the correct one
for item_obj in BaseItem.__subclasses__():
if message.who == item_obj.who:
try:
tasks = await item_obj.call_callbacks(item, message)
except InvalidMessage as e:
log.warning(f"Message not supported {e.message}")
else:
self._loop.create_task(self._check_task_result(tasks))
break
else:
log.warning(f"Item not found for message {message}")
await self._dispatch_callbacks(item, message)

async def _dispatch_callbacks(self, item: BaseItem, message: BaseMessage):
"""
Dispatches the message to the correct callbacks for the item.
Args:
item: The item that triggered the event.
message: The message that triggered the event.
"""
# because callbacks are defined inside items, we need to loop through all items to find the correct one
for item_obj in BaseItem.__subclasses__():
if message.who == item_obj.who:
try:
tasks = await item_obj.call_callbacks(item, message)
except InvalidMessage as e:
log.warning(f"Message not supported {e.message}")
else:
self._loop.create_task(self._check_task_result(tasks))
break
else:
log.warning(f"Item not found for message {message}")

@staticmethod
async def _check_task_result(tasks: list[Task]):
Expand Down
2 changes: 1 addition & 1 deletion pyown/items/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .lighting import *
from .automation import *
from .gateway import *
from .lighting import *
2 changes: 0 additions & 2 deletions pyown/items/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class GatewayModel(StrEnum):
HL4684 = "23"



class WhatGateway(What, StrEnum):
"""
This enum is used to define the various types of data that can be retrieved from a gateway.
Expand Down Expand Up @@ -541,4 +540,3 @@ async def call_callbacks(cls, item: Self, message: BaseMessage) -> list[Task]:
raise InvalidMessage("The message is not a DimensionResponse message.")

return tasks

1 change: 1 addition & 0 deletions pyown/items/lighting/dimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Dimmer(BaseLight):
"""
Dimmer items are items that can have their brightness level changed or colors changed.
"""

async def turn_on(self, speed: int | None = None):
"""
Turns the light on.
Expand Down
1 change: 1 addition & 0 deletions pyown/items/lighting/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Light(BaseLight):
"""
Light items are item with only two states: on and off.
"""

async def turn_on(self):
"""Turns the light on."""
await self.send_normal_message(WhatLight.ON)
Expand Down
1 change: 0 additions & 1 deletion pyown/items/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"ITEM_TYPES"
]


ITEM_TYPES: Final[dict[Who, Type[BaseItem]]] = {
Who.LIGHTING: Light,
Who.AUTOMATION: Automation,
Expand Down
4 changes: 2 additions & 2 deletions pyown/protocol/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def connection_made(self, transport: Transport): # type: ignore[override]
"""
Called by the transport class when the socket is connected to the server.
"""
log.info(f"Connection made")
log.info("Connection made")
self._transport = transport
self._on_connection_start.set_result(transport)

Expand All @@ -52,7 +52,7 @@ def connection_lost(self, exc: Exception | None):
if exc is not None:
log.info(f"Connection lost with exception: {exc}")
else:
log.info(f"Connection lost")
log.info("Connection lost")
if exc is None:
self._on_connection_end.set_result(None)
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyown"
version = "0.1.0"
version = "0.2.0"
description = "Python wrapper for OpenWebNet protocol"
authors = ["John Toniutti <[email protected]>"]
license = "GPL-3.0"
Expand Down

0 comments on commit a55550e

Please sign in to comment.