Skip to content

Commit

Permalink
Better error handling and added Dutch translations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Oct 1, 2023
1 parent 0b018ad commit 5a2ba5b
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
.venv
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions custom_components/davis_vantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
34 changes: 17 additions & 17 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand All @@ -34,34 +33,35 @@ 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)
self.convert_values(data)
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'])
Expand Down
2 changes: 1 addition & 1 deletion custom_components/davis_vantage/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions custom_components/davis_vantage/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
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

RAIN_COLLECTOR_IMPERIAL = '0.01"'
RAIN_COLLECTOR_METRIC = '0.2 mm'

PROTOCOL_NETWORK = 'Netwerk'
PROTOCOL_NETWORK = 'Network'
PROTOCOL_SERIAL = 'Serial'
4 changes: 2 additions & 2 deletions custom_components/davis_vantage/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from .client import DavisVantageClient
from .const import (
DEFAULT_SYNC_INTERVAL,
DOMAIN,
)

Expand All @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/davis_vantage/manifest.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
12 changes: 4 additions & 8 deletions custom_components/davis_vantage/translations/en.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
36 changes: 36 additions & 0 deletions custom_components/davis_vantage/translations/nl.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}

0 comments on commit 5a2ba5b

Please sign in to comment.