|
1 | 1 | """Utils"""
|
| 2 | +from __future__ import annotations |
| 3 | + |
2 | 4 | import logging
|
3 | 5 | from dataclasses import dataclass
|
4 |
| -from typing import Any |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from typing import Optional, Union |
| 10 | + from .const import DataKeyEnum |
5 | 11 |
|
6 |
| -from .const import DataKeyEnum, MessageTypeEnum, PayloadSeparatorEnum |
| 12 | +from .const import MessageTypeEnum, PayloadSeparatorEnum |
7 | 13 |
|
8 | 14 | _LOGGER: logging.Logger = logging.getLogger(__package__)
|
9 | 15 |
|
@@ -62,47 +68,52 @@ def from_str(msg: str) -> tuple[DataKeyEnum, str, MessageTypeEnum]:
|
62 | 68 | raise ParseError(f"Failed to parse message {msg}") from exc
|
63 | 69 |
|
64 | 70 |
|
65 |
| -@dataclass |
66 |
| -class SystemProperty: |
67 |
| - """HRV system property""" |
68 |
| - |
69 |
| - key: DataKeyEnum = None |
70 |
| - value: Any = None |
71 |
| - |
72 |
| - @classmethod |
73 |
| - def from_str(cls, key, raw_value): |
74 |
| - """Create instance from string""" |
75 |
| - return cls(key=key, value=raw_value.strip()) |
| 71 | +class BaseSystemProperty: |
| 72 | + """HRV System property""" |
76 | 73 |
|
77 | 74 |
|
78 |
| -@dataclass |
79 |
| -class RangedSystemProperty(SystemProperty): |
80 |
| - """HRV System property with min max""" |
| 75 | +class SystemProperty(BaseSystemProperty): |
| 76 | + """HRV System property with value, min, max""" |
81 | 77 |
|
82 |
| - min_value: int = None |
83 |
| - max_value: int = None |
84 |
| - time_left: int = None |
| 78 | + def __init__( |
| 79 | + self, |
| 80 | + key: DataKeyEnum, |
| 81 | + value: Optional[int | str] = None, |
| 82 | + min_value: Optional[int | str] = None, |
| 83 | + max_value: Optional[int | str] = None, |
| 84 | + *_args, |
| 85 | + ): |
| 86 | + self.key = key |
| 87 | + self.value = value |
| 88 | + self.min_value = min_value |
| 89 | + self.max_value = max_value |
85 | 90 |
|
86 | 91 | @classmethod
|
87 |
| - def from_str(cls, key, raw_value: str): |
| 92 | + def from_str(cls, key: DataKeyEnum, raw_value: str): |
88 | 93 | """Create instance from from string"""
|
89 |
| - [value, min_value, max_value, time_left] = [ |
90 |
| - int(v) if v.isnumeric() else int(v.strip()) for v in raw_value.split("+") |
91 |
| - ] |
92 |
| - return cls( |
93 |
| - key=key, |
94 |
| - value=value, |
95 |
| - min_value=min_value, |
96 |
| - max_value=max_value, |
97 |
| - time_left=time_left, |
| 94 | + |
| 95 | + def maybe_cast(x: str) -> Union[int, float, str, None]: |
| 96 | + """Cast value if it is numeric""" |
| 97 | + if x is None: |
| 98 | + return x |
| 99 | + if x.isdigit(): |
| 100 | + return int(x) |
| 101 | + if x.replace(".", "", 1).isdigit(): |
| 102 | + return float(x) |
| 103 | + return x |
| 104 | + |
| 105 | + [*positions] = ( |
| 106 | + [maybe_cast(v.strip()) for v in raw_value.split("+")] |
| 107 | + if raw_value is not None |
| 108 | + else [] |
98 | 109 | )
|
99 | 110 |
|
| 111 | + return cls(key, *positions) |
100 | 112 |
|
101 |
| -@dataclass |
102 |
| -class ErrorSystemProperty(SystemProperty): |
103 |
| - key: DataKeyEnum |
104 |
| - value: list[str] |
105 | 113 |
|
106 |
| - @classmethod |
107 |
| - def from_str(cls, key, raw_value): |
108 |
| - raise NotImplementedError() |
| 114 | +class ErrorSystemProperty(BaseSystemProperty): |
| 115 | + """HRV System error property""" |
| 116 | + |
| 117 | + def __init__(self, key: DataKeyEnum, value: Optional[list[str]] = None): |
| 118 | + self.key = key |
| 119 | + self.value = value |
0 commit comments