From 17f880f0442ca8fe88030bd59042d7dbe6c45637 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:14:34 +0200 Subject: [PATCH] Update --- docs/requirements.txt | 4 ++-- readme.md | 4 ++++ requirements.txt | 2 +- requirements_setup.txt | 12 +++++++----- requirements_tests.txt | 4 ++-- src/HABApp/__check_dependency_packages__.py | 1 + src/HABApp/__version__.py | 2 +- .../definitions/helpers/persistence_data.py | 16 +++------------- src/HABApp/openhab/definitions/values.py | 9 ++++----- src/HABApp/openhab/items/dimmer_item.py | 7 +++---- src/HABApp/openhab/items/number_item.py | 7 +++---- src/HABApp/openhab/items/rollershutter_item.py | 7 +++---- src/HABApp/openhab/map_values.py | 18 ++++++++++++------ tests/helpers/log/log_collector.py | 2 +- tests/test_openhab/test_items/test_commands.py | 2 +- tests/test_openhab/test_openhab_datatypes.py | 2 +- 16 files changed, 49 insertions(+), 50 deletions(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index ee2ceeab..71eabc9d 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,7 +1,7 @@ # Packages required to build the documentation sphinx == 7.2.6 -sphinx-autodoc-typehints == 2.0.0 +sphinx-autodoc-typehints == 2.0.1 sphinx_rtd_theme == 2.0.0 sphinx-exec-code == 0.12 -autodoc_pydantic == 2.0.1 +autodoc_pydantic == 2.1.0 sphinx-copybutton == 0.5.2 diff --git a/readme.md b/readme.md index c73e6ac1..ce0a4711 100644 --- a/readme.md +++ b/readme.md @@ -127,6 +127,10 @@ MyOpenhabRule() ``` # Changelog +#### 24.XX.X (2024-XX-XX) +- Updated number parsing logic +- Updated dependencies + #### 24.02.0 (2024-02-14) - For openHAB >= 4.1 it's possible to wait for a minimum openHAB uptime before connecting (defaults to 60s) - Renamed config entry mqtt.connection.client_id to identifier (backwards compatible) diff --git a/requirements.txt b/requirements.txt index 1b803801..73b93cb8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ # Packages for source formatting # ----------------------------------------------------------------------------- pre-commit == 3.5.0 # 3.6.0 requires python >= 3.10 -ruff == 0.2.1 +ruff == 0.3.7 # ----------------------------------------------------------------------------- # Packages for other developement tasks diff --git a/requirements_setup.txt b/requirements_setup.txt index e610aeeb..e81c01e4 100644 --- a/requirements_setup.txt +++ b/requirements_setup.txt @@ -1,10 +1,10 @@ -aiohttp == 3.9.3 -pydantic == 2.6.1 +aiohttp == 3.9.4 +pydantic == 2.7.0 msgspec == 0.18.6 -bidict == 0.22.1 +bidict == 0.23.1 watchdog == 4.0.0 ujson == 5.9.0 -aiomqtt == 2.0.0 +aiomqtt == 2.0.1 immutables == 0.20 eascheduler == 0.1.11 @@ -12,10 +12,12 @@ easyconfig == 0.3.2 pendulum == 2.1.2 stack_data == 0.6.3 colorama == 0.4.6 +fastnumbers == 5.1.0 + voluptuous == 0.14.2 -typing-extensions == 4.9.0 +typing-extensions == 4.11.0 aiohttp-sse-client == 0.2.1 diff --git a/requirements_tests.txt b/requirements_tests.txt index 3d25d953..d4c5c693 100644 --- a/requirements_tests.txt +++ b/requirements_tests.txt @@ -6,6 +6,6 @@ # ----------------------------------------------------------------------------- # Packages to run source tests # ----------------------------------------------------------------------------- -packaging == 23.2 +packaging == 24.0 pytest == 7.4.4 -pytest-asyncio == 0.23.4 +pytest-asyncio == 0.23.6 diff --git a/src/HABApp/__check_dependency_packages__.py b/src/HABApp/__check_dependency_packages__.py index dad27f87..72b0fbd5 100644 --- a/src/HABApp/__check_dependency_packages__.py +++ b/src/HABApp/__check_dependency_packages__.py @@ -15,6 +15,7 @@ def get_dependencies() -> list[str]: 'colorama', 'eascheduler', 'easyconfig', + 'fastnumbers', 'pydantic', 'stack_data', 'voluptuous', diff --git a/src/HABApp/__version__.py b/src/HABApp/__version__.py index cf9d3fe4..5860aae4 100644 --- a/src/HABApp/__version__.py +++ b/src/HABApp/__version__.py @@ -10,4 +10,4 @@ # Development versions contain the DEV-COUNTER postfix: # - 24.01.0.DEV-1 -__version__ = '24.02.1.DEV-1' +__version__ = '24.04.1.DEV-1' diff --git a/src/HABApp/openhab/definitions/helpers/persistence_data.py b/src/HABApp/openhab/definitions/helpers/persistence_data.py index 5cbc5613..187472fc 100644 --- a/src/HABApp/openhab/definitions/helpers/persistence_data.py +++ b/src/HABApp/openhab/definitions/helpers/persistence_data.py @@ -1,6 +1,8 @@ from datetime import datetime from typing import Optional, Union +from fastnumbers import try_real + from HABApp.openhab.definitions.rest import ItemHistoryResp @@ -18,19 +20,7 @@ def from_resp(cls, data: ItemHistoryResp) -> 'OpenhabPersistenceData': for entry in data.data: # calc as timestamp time = entry.time / 1000 - state = entry.state - if '.' in state: - try: - state = float(state) - except ValueError: - pass - else: - try: - state = int(state) - except ValueError: - pass - - c.data[time] = state + c.data[time] = try_real(entry.state) return c def get_data(self, start_date: OPTIONAL_DT = None, end_date: OPTIONAL_DT = None): diff --git a/src/HABApp/openhab/definitions/values.py b/src/HABApp/openhab/definitions/values.py index 72b34b0d..d6ca22d3 100644 --- a/src/HABApp/openhab/definitions/values.py +++ b/src/HABApp/openhab/definitions/values.py @@ -1,5 +1,7 @@ from base64 import b64decode -from typing import Tuple, Union +from typing import Tuple + +from fastnumbers import real from HABApp.core.events import ComplexEventValue @@ -92,10 +94,7 @@ def split_unit(value: str) -> Tuple[str, str]: def __init__(self, value: str): value, unit = QuantityValue.split_unit(value) - try: - val: Union[int, float] = int(value) - except ValueError: - val = float(value) + val = real(value) super().__init__(val) self.unit = unit diff --git a/src/HABApp/openhab/items/dimmer_item.py b/src/HABApp/openhab/items/dimmer_item.py index 8e419a11..2fa0e2dc 100644 --- a/src/HABApp/openhab/items/dimmer_item.py +++ b/src/HABApp/openhab/items/dimmer_item.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING, FrozenSet, Mapping, Optional, Union +from fastnumbers import real + from HABApp.openhab.items.base_item import MetaData, OpenhabItem from HABApp.openhab.items.commands import OnOffCommand, PercentCommand @@ -29,10 +31,7 @@ class DimmerItem(OpenhabItem, OnOffCommand, PercentCommand): @staticmethod def _state_from_oh_str(state: str): - try: - return int(state) - except ValueError: - return float(state) + return real(state) def set_value(self, new_value) -> bool: diff --git a/src/HABApp/openhab/items/number_item.py b/src/HABApp/openhab/items/number_item.py index 3ec37fe3..9926d315 100644 --- a/src/HABApp/openhab/items/number_item.py +++ b/src/HABApp/openhab/items/number_item.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING, FrozenSet, Mapping, Optional, Union +from fastnumbers import real + from HABApp.core.errors import InvalidItemValue, ItemValueIsNoneError from HABApp.openhab.definitions import QuantityValue from HABApp.openhab.items.base_item import MetaData, OpenhabItem @@ -33,10 +35,7 @@ def unit(self) -> Optional[str]: @staticmethod def _state_from_oh_str(state: str): - try: - return int(state) - except ValueError: - return float(state) + return real(state) def set_value(self, new_value) -> bool: diff --git a/src/HABApp/openhab/items/rollershutter_item.py b/src/HABApp/openhab/items/rollershutter_item.py index 71717881..a411b346 100644 --- a/src/HABApp/openhab/items/rollershutter_item.py +++ b/src/HABApp/openhab/items/rollershutter_item.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING, FrozenSet, Mapping, Optional, Union +from fastnumbers import real + from HABApp.core.errors import InvalidItemValue from HABApp.openhab.definitions import PercentValue, UpDownValue from HABApp.openhab.items.base_item import MetaData, OpenhabItem @@ -28,10 +30,7 @@ class RollershutterItem(OpenhabItem, UpDownCommand, PercentCommand): @staticmethod def _state_from_oh_str(state: str): - try: - return int(state) - except ValueError: - return float(state) + return real(state) def set_value(self, new_value) -> bool: diff --git a/src/HABApp/openhab/map_values.py b/src/HABApp/openhab/map_values.py index 19cdcf84..2f97c2a9 100644 --- a/src/HABApp/openhab/map_values.py +++ b/src/HABApp/openhab/map_values.py @@ -1,8 +1,17 @@ from datetime import datetime +from fastnumbers import real + from HABApp.core.const.const import PYTHON_311 -from HABApp.openhab.definitions import HSBValue, OnOffValue, OpenClosedValue, PercentValue, QuantityValue, RawValue, \ - UpDownValue +from HABApp.openhab.definitions import ( + HSBValue, + OnOffValue, + OpenClosedValue, + PercentValue, + QuantityValue, + RawValue, + UpDownValue, +) from HABApp.openhab.definitions.values import PointValue @@ -17,10 +26,7 @@ def map_openhab_values(openhab_type: str, openhab_value: str): return int(openhab_value) if openhab_type == "Decimal": - try: - return int(openhab_value) - except ValueError: - return float(openhab_value) + return real(openhab_value) if openhab_type == "String": return openhab_value diff --git a/tests/helpers/log/log_collector.py b/tests/helpers/log/log_collector.py index 224cb5e4..bd4f5c0e 100644 --- a/tests/helpers/log/log_collector.py +++ b/tests/helpers/log/log_collector.py @@ -132,7 +132,7 @@ def assert_ok(self): msg += '\n - ' + '\n - '.join(map(str, missing)) msg += '\nLog:\n' + '\n'.join(self.get_messages()) - pytest.fail(msg=msg) + pytest.fail(reason=msg) for rec in self.res_records: if not self.is_expected_record(rec): diff --git a/tests/test_openhab/test_items/test_commands.py b/tests/test_openhab/test_items/test_commands.py index c4668f54..f49a1122 100644 --- a/tests/test_openhab/test_items/test_commands.py +++ b/tests/test_openhab/test_items/test_commands.py @@ -13,7 +13,7 @@ def test_OnOff(cls): c = cls('item_name') assert not c.is_on() - if not __version__.startswith('24.02.0'): + if not __version__.startswith('24.04.0'): assert not c.is_off() c.set_value(OnOffValue('ON')) diff --git a/tests/test_openhab/test_openhab_datatypes.py b/tests/test_openhab/test_openhab_datatypes.py index 7ec4b4e0..45504ff5 100644 --- a/tests/test_openhab/test_openhab_datatypes.py +++ b/tests/test_openhab/test_openhab_datatypes.py @@ -23,7 +23,7 @@ def test_type_number(value: str, target: int): @pytest.mark.parametrize( - 'value, target', (('0.0', 0.0), ('-99.99', -99.99), ('99.99', 99.99), ('0', 0), ('-15', -15), ('55', 55), ) + 'value, target', (('0.0', 0), ('-99.99', -99.99), ('99.99', 99.99), ('0', 0), ('-15', -15), ('55', 55), ) ) def test_type_decimal(value: str, target: int): ret = NumberItem._state_from_oh_str(value)