Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
marq24 committed Jun 23, 2024
1 parent 7ef5718 commit 325c59d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
20 changes: 13 additions & 7 deletions custom_components/tibber_local/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
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
from homeassistant.exceptions import ConfigEntryNotReady
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,
Expand All @@ -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__)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions custom_components/tibber_local/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tibber_local/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 325c59d

Please sign in to comment.