-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tags and fix & improve type annotation
- Loading branch information
Showing
20 changed files
with
312 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from .ack import ACK | ||
from .nack import NACK | ||
|
||
from .base import MessageType, BaseMessage, parse_message | ||
from .nack import NACK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import re | ||
from typing import Self, Pattern, AnyStr | ||
|
||
from .base import BaseMessage, MessageType | ||
from ..tags import Who, What, Where | ||
|
||
__all__ = [ | ||
"NormalMessage", | ||
] | ||
|
||
|
||
class NormalMessage(BaseMessage): | ||
"""Represent an NACK message""" | ||
_type = MessageType.NORMAL | ||
_tags: tuple[Who, What, Where] | ||
|
||
_regex: Pattern[AnyStr] = re.compile(r"^\*[0-9#]+\*[0-9#]*\*[0-9#]*##$") | ||
|
||
def __init__(self, tags: tuple[Who, What, Where]): | ||
self._tags = tags | ||
|
||
@property | ||
def who(self) -> Who: | ||
return self._tags[0] | ||
|
||
@property | ||
def what(self) -> What: | ||
return self._tags[1] | ||
|
||
@property | ||
def where(self) -> Where: | ||
return self._tags[2] | ||
|
||
@property | ||
def message(self) -> str: | ||
return f"*{self.who}*{self.what}*{self.where}##" | ||
|
||
@classmethod | ||
def parse(cls, tags: list[str]) -> Self: | ||
"""Parse the tags of a message from the OpenWebNet bus.""" | ||
|
||
return cls( | ||
tags=( | ||
Who(tags[0]), | ||
What(tags[1]), | ||
Where(tags[2]) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
from .who import WHO | ||
from .base import Value | ||
from .dimension import Dimension | ||
from .what import What | ||
from .where import Where | ||
from .who import Who |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import Final | ||
|
||
from ..exceptions import InvalidTag | ||
|
||
__all__ = [ | ||
"Tag", | ||
"TagWithParameters", | ||
"Value", | ||
] | ||
|
||
VALID_TAG_CHARS: Final[str] = "0123456789#" | ||
|
||
|
||
class Tag(str): | ||
"""Tag class.""" | ||
|
||
def __init__(self, string: str): | ||
# Check if the string contains only valid characters | ||
if not all(c in VALID_TAG_CHARS for c in string): | ||
raise InvalidTag(string) | ||
|
||
super().__init__() | ||
|
||
@property | ||
def value(self) -> int | None: | ||
"""Return the value of the tag without its parameters or prefix""" | ||
val = self.removeprefix("#") | ||
if len(val) > 0: | ||
return int(val) | ||
else: | ||
return None | ||
|
||
@property | ||
def parameters(self) -> list[str] | None: | ||
"""Return the parameters of the tag""" | ||
return None | ||
|
||
@property | ||
def tag(self) -> str: | ||
"""Return the tag""" | ||
return self | ||
|
||
|
||
class TagWithParameters(Tag): | ||
@property | ||
def value(self) -> int | None: | ||
"""Return the value of the tag without its parameters or prefix""" | ||
val = self.split("#")[0] | ||
if len(val) > 0: | ||
return int(val) | ||
else: | ||
return None | ||
|
||
@property | ||
def parameters(self) -> list[str]: | ||
"""Return the parameters of the tag""" | ||
return self.split("#")[1:] | ||
|
||
|
||
class Value(Tag): | ||
""" | ||
Represent a value tag in a dimension response message. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from .base import Tag | ||
|
||
__all__ = [ | ||
"Dimension", | ||
] | ||
|
||
|
||
class Dimension(Tag): | ||
""" | ||
Represent the DIMENSION tag. | ||
It's not clear in the official documentation what exactly is the DIMENSION tag. | ||
But in many cases it's used to request information about the status of a device, | ||
making it similar to the WHAT tag. | ||
The difference between the two is that the DIMENSION tag does not allow parameters. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from .base import TagWithParameters | ||
|
||
__all__ = [ | ||
"What", | ||
] | ||
|
||
|
||
class What(TagWithParameters): | ||
""" | ||
Represent the WHAT tag. | ||
The tag WHAT, identifies the action to make (ON lights, OFF lights, dimmer at 20%, | ||
shutters UP, shutters DOWN, set program 1 in thermoregulation central, etc...) | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from .base import TagWithParameters | ||
|
||
__all__ = [ | ||
"Where", | ||
] | ||
|
||
|
||
class Where(TagWithParameters): | ||
""" | ||
Represent the WHERE tag. | ||
The tag WHERE detects the objects involved by the frame (environment, room, single | ||
object, whole system). | ||
""" | ||
pass |
Oops, something went wrong.