From 5a2ba5bc9b910be48ea390c3c5f483f0a589a99f Mon Sep 17 00:00:00 2001 From: Marco Gosselink Date: Sun, 1 Oct 2023 08:32:26 +0200 Subject: [PATCH] Better error handling and added Dutch translations --- .gitignore | 3 +- README.md | 6 ++-- custom_components/davis_vantage/__init__.py | 2 ++ custom_components/davis_vantage/client.py | 34 +++++++++--------- .../davis_vantage/config_flow.py | 2 +- custom_components/davis_vantage/const.py | 4 +-- .../davis_vantage/coordinator.py | 4 +-- custom_components/davis_vantage/manifest.json | 2 +- .../davis_vantage/translations/en.json | 12 +++---- .../davis_vantage/translations/nl.json | 36 +++++++++++++++++++ 10 files changed, 69 insertions(+), 36 deletions(-) create mode 100755 custom_components/davis_vantage/translations/nl.json diff --git a/.gitignore b/.gitignore index 496ee2c..7546958 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.DS_Store \ No newline at end of file +.DS_Store +.venv \ No newline at end of file diff --git a/README.md b/README.md index 3237a71..38f1756 100755 --- a/README.md +++ b/README.md @@ -15,11 +15,9 @@ Via HACS: ## Setup -During the setup of the integration the serial port of the weather station needs to be provided. +During the setup of the integration the serial port or the hostname of the weather station needs to be provided. -Examples: -- tcp:192.168.0.18:1111 -- serial:/dev/ttyUSB0:19200:8N1 +Example network host: 192.168.0.18:1111 ![Setup](/assets/setup.png) diff --git a/custom_components/davis_vantage/__init__.py b/custom_components/davis_vantage/__init__.py index a42e026..98a905f 100755 --- a/custom_components/davis_vantage/__init__.py +++ b/custom_components/davis_vantage/__init__.py @@ -34,6 +34,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: link = entry.data.get("link", "") rain_collector = entry.data.get("rain_collector", "0.01""") windrose8 = entry.data.get("windrose8", False) + + hass.data[DOMAIN]['interval'] = entry.data.get("interval", 30) client = DavisVantageClient(hass, protocol, link, rain_collector, windrose8) diff --git a/custom_components/davis_vantage/client.py b/custom_components/davis_vantage/client.py index 79257b5..42aed32 100755 --- a/custom_components/davis_vantage/client.py +++ b/custom_components/davis_vantage/client.py @@ -1,6 +1,7 @@ from typing import Any from datetime import datetime import logging +from time import sleep from pyvantagepro import VantagePro2 from pyvantagepro.parser import LoopDataParserRevB @@ -9,8 +10,6 @@ from .utils import * from .const import RAIN_COLLECTOR_METRIC, PROTOCOL_NETWORK, PROTOCOL_SERIAL -TIMEOUT = 10 - _LOGGER: logging.Logger = logging.getLogger(__package__) class DavisVantageClient: @@ -34,7 +33,6 @@ async def async_get_current_data(self) -> LoopDataParserRevB | None: data = self._last_data try: vantagepro2 = VantagePro2.from_url(self.get_link()) # type: ignore - # vantagepro2.link.open() # type: ignore data = vantagepro2.get_current_data() if data: self.add_additional_info(data) @@ -42,26 +40,28 @@ async def async_get_current_data(self) -> LoopDataParserRevB | None: data['LastError'] = "No error" self._last_data = data else: - data['LastError'] = "Couldn't acquire data" + data['LastError'] = "Couldn't acquire data, no data received" except Exception as e: - _LOGGER.warning(f"Couldn't acquire data from {self._link}") + _LOGGER.warning(f"Couldn't acquire data from {self.get_link()}") data['LastError'] = f"Couldn't acquire data: {e}" # type: ignore - # finally: - # vantagepro2.link.close() # type: ignore return data # type: ignore async def async_get_davis_time(self) -> datetime | None: """Get time from weather station.""" - data = None - try: - vantagepro2 = VantagePro2.from_url(self.get_link()) # type: ignore - # vantagepro2.link.open() # type: ignore - data = vantagepro2.gettime() - except Exception: - _LOGGER.warning(f"Couldn't acquire data from {self._link}") - # finally: - # vantagepro2.link.close() # type: ignore - return data + tries = 3 + last_error: Exception = None + while tries > 0: + try: + vantagepro2 = VantagePro2.from_url(self.get_link()) # type: ignore + data = vantagepro2.gettime() + return data + except Exception as e: + last_error = e + pass + sleep(0.2) + tries -=1 + _LOGGER.error(f"Couldn't acquire data from {self.get_link()}: {last_error}") + return None def add_additional_info(self, data: dict[str, Any]) -> None: data['HeatIndex'] = calc_heat_index(data['TempOut'], data['HumOut']) diff --git a/custom_components/davis_vantage/config_flow.py b/custom_components/davis_vantage/config_flow.py index 588dfc5..5959309 100755 --- a/custom_components/davis_vantage/config_flow.py +++ b/custom_components/davis_vantage/config_flow.py @@ -143,7 +143,7 @@ async def async_step_setup_other_info( list_of_rain_collector = [RAIN_COLLECTOR_IMPERIAL, RAIN_COLLECTOR_METRIC] STEP_USER_DATA_SCHEMA = vol.Schema( { - vol.Required("interval", default=DEFAULT_SYNC_INTERVAL): int, #type: ignore + vol.Required("interval", default=DEFAULT_SYNC_INTERVAL): vol.All(int, vol.Range(min=5)), #type: ignore vol.Required("rain_collector"): vol.In(list_of_rain_collector), vol.Required("windrose8"): bool } diff --git a/custom_components/davis_vantage/const.py b/custom_components/davis_vantage/const.py index 9457458..c0872c4 100755 --- a/custom_components/davis_vantage/const.py +++ b/custom_components/davis_vantage/const.py @@ -4,7 +4,7 @@ DOMAIN = "davis_vantage" MANUFACTURER = "Davis" MODEL = "Vantage Pro2/Vue" -VERSION = "0.0.1" +VERSION = "0.0.2" DEFAULT_SYNC_INTERVAL = 30 # seconds DEFAULT_NAME = NAME @@ -12,5 +12,5 @@ RAIN_COLLECTOR_IMPERIAL = '0.01"' RAIN_COLLECTOR_METRIC = '0.2 mm' -PROTOCOL_NETWORK = 'Netwerk' +PROTOCOL_NETWORK = 'Network' PROTOCOL_SERIAL = 'Serial' \ No newline at end of file diff --git a/custom_components/davis_vantage/coordinator.py b/custom_components/davis_vantage/coordinator.py index 5cfdf2f..7e4db9c 100755 --- a/custom_components/davis_vantage/coordinator.py +++ b/custom_components/davis_vantage/coordinator.py @@ -9,7 +9,6 @@ from .client import DavisVantageClient from .const import ( - DEFAULT_SYNC_INTERVAL, DOMAIN, ) @@ -25,12 +24,13 @@ def __init__(self, hass: HomeAssistant, client: DavisVantageClient, device_info: self.platforms: list[str] = [] self.last_updated = None self.device_info = device_info + interval = hass.data[DOMAIN].get('interval', 30) super().__init__( hass, _LOGGER, name=DOMAIN, - update_interval=timedelta(seconds=DEFAULT_SYNC_INTERVAL), + update_interval=timedelta(seconds=interval), ) async def _async_update_data(self) -> dict[str, Any]: diff --git a/custom_components/davis_vantage/manifest.json b/custom_components/davis_vantage/manifest.json index 085629e..8fc76cf 100755 --- a/custom_components/davis_vantage/manifest.json +++ b/custom_components/davis_vantage/manifest.json @@ -1,7 +1,7 @@ { "domain": "davis_vantage", "name": "Davis Vantage", - "version": "0.0.1", + "version": "0.0.2", "config_flow": true, "documentation": "https://github.com/MarcoGos/davis_vantage", "requirements": ["PyVantagePro==0.3.2"], diff --git a/custom_components/davis_vantage/translations/en.json b/custom_components/davis_vantage/translations/en.json index cde8750..fda7e28 100755 --- a/custom_components/davis_vantage/translations/en.json +++ b/custom_components/davis_vantage/translations/en.json @@ -1,21 +1,17 @@ { "config": { "abort": { - "already_configured": "Device is already configured" + "already_configured": "Device is already configured." }, "error": { - "cannot_connect": "Failed to connect", + "cannot_connect": "Failed to connect.", "invalid_auth": "Looks like the weather station isn't reacting, try again.", - "unknown": "Unexpected error" + "unknown": "Unexpected error." }, "step": { "user": { "data": { - "protocol": "Protocol", - "link": "Link", - "interval": "Interval", - "rain_collector": "Rain collector", - "windrose8": "Wind rose with 8 cardinal directions" + "protocol": "Protocol" } }, "setup_serial": { diff --git a/custom_components/davis_vantage/translations/nl.json b/custom_components/davis_vantage/translations/nl.json new file mode 100755 index 0000000..9d18388 --- /dev/null +++ b/custom_components/davis_vantage/translations/nl.json @@ -0,0 +1,36 @@ +{ + "config": { + "abort": { + "already_configured": "Apparaat is reeds geconfigureerd." + }, + "error": { + "cannot_connect": "Kan geen verbinding maken.", + "invalid_auth": "Het lijkt erop dat het weerstation niet reageert, probeer het opnieuw.", + "unknown": "Onverwachte fout." + }, + "step": { + "user": { + "data": { + "protocol": "Protocol" + } + }, + "setup_serial": { + "data": { + "link": "Apparaat" + } + }, + "setup_network": { + "data": { + "link": "Hostnaam" + } + }, + "setup_other_info": { + "data": { + "interval": "Interval", + "rain_collector": "Regenmeter", + "windrose8": "Windroos met 8 cardinale richtingen" + } + } + } + } +} \ No newline at end of file