From 325c59d22d176b0d7021b717b72b03c383a63ca0 Mon Sep 17 00:00:00 2001 From: marq24 Date: Sun, 23 Jun 2024 09:40:09 +0200 Subject: [PATCH] minor tweaks --- custom_components/tibber_local/__init__.py | 20 ++++++++++++------- custom_components/tibber_local/config_flow.py | 15 +++++++++----- custom_components/tibber_local/manifest.json | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/custom_components/tibber_local/__init__.py b/custom_components/tibber_local/__init__.py index ec2889a..19401ef 100644 --- a/custom_components/tibber_local/__init__.py +++ b/custom_components/tibber_local/__init__.py @@ -1,15 +1,9 @@ import asyncio import logging import re - -import voluptuous as vol - from datetime import timedelta -from smllib import SmlStreamReader -from smllib.errors import CrcError -from smllib.sml import SmlListEntry, ObisCode -from smllib.const import UNITS +import voluptuous as vol from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.const import CONF_ID, CONF_HOST, CONF_SCAN_INTERVAL, CONF_PASSWORD, CONF_MODE from homeassistant.core import HomeAssistant @@ -17,6 +11,10 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.entity import EntityDescription, Entity from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from smllib import SmlStreamReader +from smllib.const import UNITS +from smllib.errors import CrcError +from smllib.sml import SmlListEntry, ObisCode from .const import ( DOMAIN, @@ -30,6 +28,8 @@ MODE_0_AutoScanMode, MODE_3_SML_1_04, MODE_99_PLAINTEXT, + MODE_1_IEC_62056_21, + ENUM_IMPLEMENTATIONS, ) _LOGGER = logging.getLogger(__name__) @@ -255,6 +255,10 @@ async def detect_com_mode(self): elif self._com_mode == MODE_0_AutoScanMode: await self._check_modes_internal(MODE_3_SML_1_04, MODE_99_PLAINTEXT) + # finally raise value error if not implemented yet! + if self._com_mode not in ENUM_IMPLEMENTATIONS: + raise ValueError(f"NOT IMPLEMENTED yet! - Mode: {self._com_mode}") + async def _check_modes_internal(self, mode_1:int, mode_2:int): _LOGGER.debug(f"detect_com_mode is {self._com_mode}: will try to read {mode_1}") await self.read_tibber_local(mode_1, False, log_payload=True) @@ -304,6 +308,8 @@ async def read_tibber_local(self, mode: int, retry: bool, log_payload: bool = Fa if res.status == 200: if mode == MODE_3_SML_1_04: await self.read_sml(await res.read(), retry, log_payload) + #elif mode == MODE_1_IEC_62056_21: + # await self.read_ice62056(await res.read(), retry, log_payload) elif mode == MODE_99_PLAINTEXT: await self.read_plaintext(await res.text(), retry, log_payload) else: diff --git a/custom_components/tibber_local/config_flow.py b/custom_components/tibber_local/config_flow.py index 63c586c..5aa4014 100644 --- a/custom_components/tibber_local/config_flow.py +++ b/custom_components/tibber_local/config_flow.py @@ -55,12 +55,17 @@ async def _test_connection_tibber_local(self, host, pwd, node_num): try: bridge = TibberLocalBridge(host=host, pwd=pwd, websession=async_get_clientsession(self.hass), node_num=node_num) - await bridge.detect_com_mode() - if bridge._com_mode in ENUM_IMPLEMENTATIONS: - self._con_mode = bridge._com_mode - return await self._test_data_available(bridge, host) - else: + try: + await bridge.detect_com_mode() + if bridge._com_mode in ENUM_IMPLEMENTATIONS: + self._con_mode = bridge._com_mode + return await self._test_data_available(bridge, host) + else: + self._errors[CONF_HOST] = "unknown_mode" + + except ValueError as val_err: self._errors[CONF_HOST] = "unknown_mode" + _LOGGER.warning(f"ValueError: {val_err}") except (OSError, HTTPError, Timeout, ClientResponseError): self._errors[CONF_HOST] = "cannot_connect" diff --git a/custom_components/tibber_local/manifest.json b/custom_components/tibber_local/manifest.json index c81e842..41cce91 100644 --- a/custom_components/tibber_local/manifest.json +++ b/custom_components/tibber_local/manifest.json @@ -10,5 +10,5 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/marq24/ha-tibber-pulse-local/issues", "requirements": ["smllib==1.4"], - "version": "2024.6.1" + "version": "2024.6.2" }