From bf39c58c76d72ccf51151c5364f46ee33b39f90e Mon Sep 17 00:00:00 2001 From: SukramJ Date: Sat, 14 Dec 2024 21:23:14 +0100 Subject: [PATCH] Partially revert #1916 (#1917) * Partially revert #1916 * Partially revert #1916 * Update changelog.md --- changelog.md | 2 +- hahomematic/caches/persistent.py | 4 ++-- hahomematic/caches/visibility.py | 4 ++-- hahomematic/client/__init__.py | 4 ++-- hahomematic/client/json_rpc.py | 7 ++++--- hahomematic/const.py | 4 +++- hahomematic/hmcli.py | 4 ++-- hahomematic/rega_scripts/fetch_all_device_data.fn | 4 ++-- hahomematic/rega_scripts/get_program_descriptions.fn | 4 ++-- hahomematic/rega_scripts/get_serial.fn | 6 +++--- .../rega_scripts/get_system_variable_descriptions.fn | 4 ++-- hahomematic/rega_scripts/set_system_variable.fn | 2 +- hahomematic/support.py | 2 +- hahomematic_support/client_local.py | 4 ++-- tests/helper.py | 2 +- tests/test_support.py | 10 ++++++---- 16 files changed, 36 insertions(+), 31 deletions(-) diff --git a/changelog.md b/changelog.md index f2d80335..b305db2a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,7 @@ # Version 2024.12.4 (2024-12-14) - Add missing encoding to unquote -- Ensure default encoding is ISO-8859-1 +- Ensure default encoding is ISO-8859-1 where needed # Version 2024.12.3 (2024-12-14) diff --git a/hahomematic/caches/persistent.py b/hahomematic/caches/persistent.py index c80598d1..21a2beb2 100644 --- a/hahomematic/caches/persistent.py +++ b/hahomematic/caches/persistent.py @@ -19,7 +19,7 @@ FILE_DEVICES, FILE_PARAMSETS, INIT_DATETIME, - ISO_8859_1, + UTF_8, DataOperationResult, DeviceDescription, ParameterData, @@ -106,7 +106,7 @@ async def load(self) -> DataOperationResult: def _load() -> DataOperationResult: with open( file=os.path.join(self._cache_dir, self._filename), - encoding=ISO_8859_1, + encoding=UTF_8, ) as fptr: data = orjson.loads(fptr.read()) if (converted_hash := hash_sha256(value=data)) == self.last_hash_saved: diff --git a/hahomematic/caches/visibility.py b/hahomematic/caches/visibility.py index d89d7669..45e9d9f0 100644 --- a/hahomematic/caches/visibility.py +++ b/hahomematic/caches/visibility.py @@ -9,7 +9,7 @@ from typing import Any, Final from hahomematic import central as hmcu, support as hms -from hahomematic.const import CLICK_EVENTS, ISO_8859_1, UN_IGNORE_WILDCARD, Parameter, ParamsetKey +from hahomematic.const import CLICK_EVENTS, UN_IGNORE_WILDCARD, UTF_8, Parameter, ParamsetKey from hahomematic.model.custom import get_required_parameters from hahomematic.support import element_matches_key, reduce_args @@ -706,7 +706,7 @@ def _load() -> None: self._storage_folder, _FILE_CUSTOM_UN_IGNORE_PARAMETERS, ), - encoding=ISO_8859_1, + encoding=UTF_8, ) as fptr: for file_line in fptr.readlines(): if "#" not in file_line: diff --git a/hahomematic/client/__init__.py b/hahomematic/client/__init__.py index 4bb49a5c..41eb3aa6 100644 --- a/hahomematic/client/__init__.py +++ b/hahomematic/client/__init__.py @@ -44,7 +44,7 @@ from hahomematic.model.device import Device from hahomematic.model.support import convert_value from hahomematic.support import ( - build_headers, + build_xml_rpc_headers, build_xml_rpc_uri, get_device_address, is_channel_address, @@ -1600,7 +1600,7 @@ async def get_xml_rpc_proxy( """Return a XmlRPC proxy for backend communication.""" central_config = self.central.config xml_rpc_headers = ( - build_headers( + build_xml_rpc_headers( username=central_config.username, password=central_config.password, ) diff --git a/hahomematic/client/json_rpc.py b/hahomematic/client/json_rpc.py index 3b32d277..1167965f 100644 --- a/hahomematic/client/json_rpc.py +++ b/hahomematic/client/json_rpc.py @@ -28,6 +28,7 @@ ISO_8859_1, PATH_JSON_RPC, REGA_SCRIPT_PATH, + UTF_8, DeviceDescription, Interface, ParameterData, @@ -307,7 +308,7 @@ def _load_script(script_name: str) -> str | None: script_file = os.path.join( Path(__file__).resolve().parent, REGA_SCRIPT_PATH, script_name ) - if script := Path(script_file).read_text(encoding=ISO_8859_1): + if script := Path(script_file).read_text(encoding=UTF_8): self._script_cache[script_name] = script return script return None @@ -400,14 +401,14 @@ async def _do_post( async def _get_json_reponse(self, response: ClientResponse) -> dict[str, Any] | Any: """Return the json object from response.""" try: - return await response.json(encoding=ISO_8859_1) + return await response.json(encoding=UTF_8) except ValueError as ver: _LOGGER.debug( "DO_POST: ValueError [%s] Unable to parse JSON. Trying workaround", reduce_args(args=ver.args), ) # Workaround for bug in CCU - return orjson.loads((await response.read()).decode(ISO_8859_1)) + return orjson.loads((await response.read()).decode(encoding=UTF_8)) async def logout(self) -> None: """Logout of CCU.""" diff --git a/hahomematic/const.py b/hahomematic/const.py index 2dbca50d..19c63f39 100644 --- a/hahomematic/const.py +++ b/hahomematic/const.py @@ -32,7 +32,9 @@ DEFAULT_VERIFY_TLS: Final = False DEFAULT_WAIT_FOR_CALLBACK: Final[int | None] = None -# Default encoding for all Homematic service calls, persistent cache and script files +# Default encoding for json service calls, persistent cache +UTF_8: Final = "utf-8" +# Default encoding for xmlrpc service calls and script files ISO_8859_1: Final = "iso-8859-1" MAX_WAIT_FOR_CALLBACK: Final = 60 diff --git a/hahomematic/hmcli.py b/hahomematic/hmcli.py index 33d3a2c0..207313bf 100644 --- a/hahomematic/hmcli.py +++ b/hahomematic/hmcli.py @@ -10,7 +10,7 @@ from hahomematic import __version__ from hahomematic.const import ParamsetKey -from hahomematic.support import build_headers, build_xml_rpc_uri, get_tls_context +from hahomematic.support import build_xml_rpc_headers, build_xml_rpc_uri, get_tls_context def main() -> None: @@ -104,7 +104,7 @@ def main() -> None: path=args.path, tls=args.tls, ) - headers = build_headers(username=args.username, password=args.password) + headers = build_xml_rpc_headers(username=args.username, password=args.password) context = None if args.tls: context = get_tls_context(verify_tls=args.verify) diff --git a/hahomematic/rega_scripts/fetch_all_device_data.fn b/hahomematic/rega_scripts/fetch_all_device_data.fn index 639cd8a5..e1686ae6 100644 --- a/hahomematic/rega_scripts/fetch_all_device_data.fn +++ b/hahomematic/rega_scripts/fetch_all_device_data.fn @@ -1,4 +1,4 @@ -!# fetch_all_device_data.fn v2.2 / file is ISO-8859-1 encoded +!# fetch_all_device_data.fn v2.2 !# This script fetches all device data required to initialize the entities without affecting the duty cycle. !# !# Original script: https://github.com/ioBroker/ioBroker.hm-rega/blob/master/regascripts/datapoints.fn @@ -10,7 +10,7 @@ !# modified by: SukramJ https://github.com/SukramJ && Baxxy13 https://github.com/Baxxy13 !# v2.2 - 09/2023 !# -!# Das Interface wird durch die Integration an 'sUse_Interface' übergeben. +!# Das Interface wird durch die Integration an 'sUse_Interface' übergeben. !# Nutzbare Interfaces: BidCos-RF, BidCos-Wired, HmIP-RF, VirtualDevices !# Zum Testen direkt auf der Homematic-Zentrale muss das Interface wie folgt eingetragen werden: sUse_Interface = "HmIP-RF"; diff --git a/hahomematic/rega_scripts/get_program_descriptions.fn b/hahomematic/rega_scripts/get_program_descriptions.fn index ab4f514d..99147626 100644 --- a/hahomematic/rega_scripts/get_program_descriptions.fn +++ b/hahomematic/rega_scripts/get_program_descriptions.fn @@ -1,5 +1,5 @@ -!# get_program_descriptions.fn / file is ISO-8859-1 encoded -!# Erstellt in Ergänzung zu https://github.com/eq-3/occu/blob/45b38865f6b60f16f825b75f0bdc8a9738831ee0/WebUI/www/api/methods/sysvar/getall.tcl +!# get_program_descriptions.fn +!# Erstellt in Ergänzung zu https://github.com/eq-3/occu/blob/45b38865f6b60f16f825b75f0bdc8a9738831ee0/WebUI/www/api/methods/sysvar/getall.tcl !# Erweitert das Script um "description" !# diff --git a/hahomematic/rega_scripts/get_serial.fn b/hahomematic/rega_scripts/get_serial.fn index 6c6198d0..3b240b66 100644 --- a/hahomematic/rega_scripts/get_serial.fn +++ b/hahomematic/rega_scripts/get_serial.fn @@ -1,13 +1,13 @@ -!# get_serial / file is ISO-8859-1 encoded +!# get_serial !# !# Erstellt durch @baxxy13 2022-04-09 !# -!# Dieses Script liefert die Seriennummer des Funkmoduls in folgender Priorisierung zurück: +!# Dieses Script liefert die Seriennummer des Funkmoduls in folgender Priorisierung zurück: !# 1. /var/board_sgtin !# 2. /var/board_serial !# 3. /sys/module/plat_eq3ccu2/parameters/board_serial !# -!# Dieses Script wird als Ersatz für JsonRPC CCU.getSerial verwendet. +!# Dieses Script wird als Ersatz für JsonRPC CCU.getSerial verwendet. !# string serial; diff --git a/hahomematic/rega_scripts/get_system_variable_descriptions.fn b/hahomematic/rega_scripts/get_system_variable_descriptions.fn index 92efde66..c3f71b73 100644 --- a/hahomematic/rega_scripts/get_system_variable_descriptions.fn +++ b/hahomematic/rega_scripts/get_system_variable_descriptions.fn @@ -1,5 +1,5 @@ -!# get_system_variable_descriptions.fn / file is ISO-8859-1 encoded -!# Erstellt in Ergänzung zu https://github.com/eq-3/occu/blob/45b38865f6b60f16f825b75f0bdc8a9738831ee0/WebUI/www/api/methods/sysvar/getall.tcl +!# get_system_variable_descriptions.fn +!# Erstellt in Ergänzung zu https://github.com/eq-3/occu/blob/45b38865f6b60f16f825b75f0bdc8a9738831ee0/WebUI/www/api/methods/sysvar/getall.tcl !# Erweitert das Script um "description" !# diff --git a/hahomematic/rega_scripts/set_system_variable.fn b/hahomematic/rega_scripts/set_system_variable.fn index 4f1b312c..66cbf831 100644 --- a/hahomematic/rega_scripts/set_system_variable.fn +++ b/hahomematic/rega_scripts/set_system_variable.fn @@ -1,4 +1,4 @@ -!# set_system_variable / file is ISO-8859-1 encoded +!# set_system_variable !# !# Erstellt durch @baxxy13 2022-04-11 !# diff --git a/hahomematic/support.py b/hahomematic/support.py index ee1e1bc8..c562d85e 100644 --- a/hahomematic/support.py +++ b/hahomematic/support.py @@ -69,7 +69,7 @@ def build_xml_rpc_uri( return f"{scheme}://{host}{s_port}{path}" -def build_headers( +def build_xml_rpc_headers( username: str, password: str, ) -> list[tuple[str, str]]: diff --git a/hahomematic_support/client_local.py b/hahomematic_support/client_local.py index a3a8724c..bc0a9bd5 100644 --- a/hahomematic_support/client_local.py +++ b/hahomematic_support/client_local.py @@ -14,7 +14,7 @@ from hahomematic.config import WAIT_FOR_CALLBACK from hahomematic.const import ( DP_KEY_VALUE, - ISO_8859_1, + UTF_8, CallSource, CommandRxMode, DeviceDescription, @@ -328,7 +328,7 @@ async def _load_json_file(self, anchor: str, resource: str, filename: str) -> An def _load() -> Any | None: with open( file=os.path.join(package_path, resource, filename), - encoding=ISO_8859_1, + encoding=UTF_8, ) as fptr: return orjson.loads(fptr.read()) diff --git a/tests/helper.py b/tests/helper.py index dcf1d20a..e4ba1a02 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -197,7 +197,7 @@ def _load_json_file(anchor: str, resource: str, filename: str) -> Any | None: package_path = str(importlib.resources.files(anchor)) with open( file=os.path.join(package_path, resource, filename), - encoding=hahomematic_const.ISO_8859_1, + encoding=hahomematic_const.UTF_8, ) as fptr: return orjson.loads(fptr.read()) diff --git a/tests/test_support.py b/tests/test_support.py index d5092163..810bace7 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -33,7 +33,7 @@ get_event_name, ) from hahomematic.support import ( - build_headers, + build_xml_rpc_headers, build_xml_rpc_uri, changed_within_seconds, check_or_create_directory, @@ -107,11 +107,13 @@ def test_build_xml_rpc_uri() -> None: def test_build_headers() -> None: """Test build_xml_rpc_uri.""" - assert build_headers(username="Martin", password="") == [ + assert build_xml_rpc_headers(username="Martin", password="") == [ ("Authorization", "Basic TWFydGluOg==") ] - assert build_headers(username="", password="asdf") == [("Authorization", "Basic OmFzZGY=")] - assert build_headers(username="Martin", password="asdf") == [ + assert build_xml_rpc_headers(username="", password="asdf") == [ + ("Authorization", "Basic OmFzZGY=") + ] + assert build_xml_rpc_headers(username="Martin", password="asdf") == [ ("Authorization", "Basic TWFydGluOmFzZGY=") ]