Skip to content

Commit

Permalink
fix: rework system property, remove ranged property
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed Nov 23, 2024
1 parent 4613918 commit c969714
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
83 changes: 47 additions & 36 deletions src/pysaleryd/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Utils"""
from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import Any
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Optional, Union
from .const import DataKeyEnum

from .const import DataKeyEnum, MessageTypeEnum, PayloadSeparatorEnum
from .const import MessageTypeEnum, PayloadSeparatorEnum

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -62,47 +68,52 @@ def from_str(msg: str) -> tuple[DataKeyEnum, str, MessageTypeEnum]:
raise ParseError(f"Failed to parse message {msg}") from exc


@dataclass
class SystemProperty:
"""HRV system property"""

key: DataKeyEnum = None
value: Any = None

@classmethod
def from_str(cls, key, raw_value):
"""Create instance from string"""
return cls(key=key, value=raw_value.strip())
class BaseSystemProperty:
"""HRV System property"""


@dataclass
class RangedSystemProperty(SystemProperty):
"""HRV System property with min max"""
class SystemProperty(BaseSystemProperty):
"""HRV System property with value, min, max"""

min_value: int = None
max_value: int = None
time_left: int = None
def __init__(
self,
key: DataKeyEnum,
value: Optional[int | str] = None,
min_value: Optional[int | str] = None,
max_value: Optional[int | str] = None,
*_args,
):
self.key = key
self.value = value
self.min_value = min_value
self.max_value = max_value

@classmethod
def from_str(cls, key, raw_value: str):
def from_str(cls, key: DataKeyEnum, raw_value: str):
"""Create instance from from string"""
[value, min_value, max_value, time_left] = [
int(v) if v.isnumeric() else int(v.strip()) for v in raw_value.split("+")
]
return cls(
key=key,
value=value,
min_value=min_value,
max_value=max_value,
time_left=time_left,

def maybe_cast(x: str) -> Union[int, float, str, None]:
"""Cast value if it is numeric"""
if x is None:
return x
if x.isdigit():
return int(x)
if x.replace(".", "", 1).isdigit():
return float(x)
return x

[*positions] = (
[maybe_cast(v.strip()) for v in raw_value.split("+")]
if raw_value is not None
else []
)

return cls(key, *positions)

@dataclass
class ErrorSystemProperty(SystemProperty):
key: DataKeyEnum
value: list[str]

@classmethod
def from_str(cls, key, raw_value):
raise NotImplementedError()
class ErrorSystemProperty(BaseSystemProperty):
"""HRV System error property"""

def __init__(self, key: DataKeyEnum, value: Optional[list[str]] = None):
self.key = key
self.value = value
35 changes: 13 additions & 22 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

import pytest

from pysaleryd.utils import (
DataKeyEnum,
IncomingMessage,
MessageTypeEnum,
ParseError,
RangedSystemProperty,
SystemProperty,
)
from pysaleryd.const import DataKeyEnum, MessageTypeEnum
from pysaleryd.utils import IncomingMessage, ParseError, SystemProperty

__author__ = "Björn Dalfors"
__copyright__ = "Björn Dalfors"
Expand Down Expand Up @@ -71,23 +65,20 @@ def test_parse_error():
IncomingMessage.from_str("wer")


def test_parse_ranged_system_property():
"""Test parse RangedSystemProperty from str"""
key = "MF"
value_str = "1+ 2+ 3+ 10"
parsed = RangedSystemProperty.from_str(key, value_str)
assert isinstance(parsed, RangedSystemProperty)
assert parsed.key == DataKeyEnum.MODE_FAN
assert parsed.value == 1
assert parsed.min_value == 2
assert parsed.max_value == 3
assert parsed.time_left == 10


def test_parse_system_property():
"""Test parse SystemProperty"""
key = DataKeyEnum.INSTALLER_PASSWORD
value_str = "1"
parsed = SystemProperty.from_str(key, value_str)
assert parsed.key == DataKeyEnum.INSTALLER_PASSWORD
assert parsed.value == "1"
assert parsed.value == 1

value_str = "1+ 2+ 3+0"
parsed = SystemProperty.from_str(key, value_str)
assert parsed.value == 1
assert parsed.min_value == 2
assert parsed.max_value == 3

value_str = "test"
parsed = SystemProperty.from_str(key, value_str)
assert parsed.value == "test"

0 comments on commit c969714

Please sign in to comment.