Skip to content

Commit

Permalink
Improved getting station info
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Feb 11, 2025
1 parent 80184e7 commit b83b040
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
11 changes: 2 additions & 9 deletions custom_components/davis_vantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

client = DavisVantageClient(hass, protocol, link)
await client.connect_to_station()
static_info = await client.async_get_static_info()
firmware_version = (
static_info.get("version", None) if static_info is not None else None
)
latitude, longitude, elevation = await client.async_get_latitude_longitude_elevation()
hass.data.setdefault("latitude", latitude)
hass.data.setdefault("longitude", longitude)
hass.data.setdefault("elevation", elevation)
await client.get_station_info()

device_info = DeviceInfo(
identifiers={(DOMAIN, entry.entry_id)},
manufacturer=MANUFACTURER,
name=NAME,
model=entry.data.get(CONFIG_STATION_MODEL, "Unknown"),
sw_version=firmware_version,
sw_version=client.firmware_version,
hw_version=None,
)

Expand Down
44 changes: 39 additions & 5 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""All client function"""

from typing import Any
from functools import cached_property
from datetime import datetime, timedelta, time, date
from zoneinfo import ZoneInfo
import logging
Expand Down Expand Up @@ -40,6 +41,10 @@ class DavisVantageClient:
"""Davis Vantage Client class"""

_vantagepro2: VantagePro2 = None # type: ignore
_latitude: float = 0.0
_longitude: float = 0.0
_elevation: int = 0
_firmware_version: str|None = None

def __init__(self, hass: HomeAssistant, protocol: str, link: str) -> None:
self._hass = hass
Expand All @@ -49,6 +54,22 @@ def __init__(self, hass: HomeAssistant, protocol: str, link: str) -> None:
self._last_data: LoopDataParserRevB = {} # type: ignore
self._last_raw_data: DataParser = {} # type: ignore

@property
def latitude(self) -> float:
return self._latitude

@property
def longitude(self) -> float:
return self._longitude

@property
def elevation(self) -> int:
return self._elevation

@cached_property
def firmware_version(self) -> str|None:
return self._firmware_version

def get_vantagepro2fromurl(self, url: str) -> VantagePro2 | None:
try:
vp = VantagePro2.from_url(url)
Expand All @@ -70,6 +91,19 @@ async def async_get_vantagepro2fromurl(self, url: str) -> VantagePro2 | None:
async def connect_to_station(self):
self._vantagepro2 = await self.async_get_vantagepro2fromurl(self.get_link()) # type: ignore

async def get_station_info(self):
static_info = await self.async_get_static_info()
self._firmware_version = (
static_info.get("version", None) if static_info is not None else None
)
latitude, longitude, elevation = await self.async_get_latitude_longitude_elevation()
if latitude:
self._latitude = latitude
if longitude:
self._longitude = longitude
if elevation:
self._elevation = elevation

def get_current_data(
self,
) -> tuple[LoopDataParserRevB | None, ListDict | None, HighLowParserRevB | None]:
Expand Down Expand Up @@ -315,9 +349,9 @@ def add_additional_info(self, data: dict[str, Any]) -> None:
data["IsRaining"] = data["RainRate"] > 0
data["ArchiveInterval"] = self._vantagepro2.archive_period

data["Latitude"] = self._hass.data.get("latitude", None)
data["Longitude"] = self._hass.data.get("longitude", None)
data["Elevation"] = self._hass.data.get("elevation", None)
data["Latitude"] = self.latitude
data["Longitude"] = self.longitude
data["Elevation"] = self.elevation

def convert_values(self, data: dict[str, Any]) -> None:
del data["Datetime"]
Expand Down Expand Up @@ -478,8 +512,8 @@ async def async_set_rain_collector(self, rain_collector: str):

def get_latitude_longitude_elevation(self) -> tuple[float, float, int]:
latitude = longitude = None
data = self._vantagepro2.read_from_eeprom("0B", 6)
latitude, longitude, elevation = struct.unpack(b"hhh", data)
data = self._vantagepro2.read_from_eeprom("0B", 6) # type: ignore
latitude, longitude, elevation = struct.unpack(b"hhh", data) # type: ignore
latitude /= 10
longitude /= 10
return latitude, longitude, elevation
Expand Down

0 comments on commit b83b040

Please sign in to comment.