Skip to content

Commit

Permalink
Fixes blocking call to sleep inside event loop, issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Mar 6, 2024
1 parent 591141b commit da563d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion custom_components/davis_vantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
CONFIG_STATION_MODEL,
CONFIG_INTERVAL,
CONFIG_PROTOCOL,
CONFIG_LINK
CONFIG_LINK,
DATA_ARCHIVE_PERIOD
)
from .coordinator import DavisVantageDataUpdateCoordinator
from .utils import convert_to_iso_datetime
Expand Down Expand Up @@ -55,6 +56,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await client.connect_to_station()
info = await client.async_get_info()
firmware_version = info.get('version', None) if info is not None else None
hass.data.setdefault(DATA_ARCHIVE_PERIOD, info.get('archive_period', None) if info is not None else None)

device_info = DeviceInfo(
identifiers={(DOMAIN, entry.entry_id)},
Expand Down
34 changes: 18 additions & 16 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
get_uv,
get_wind_rose,
)
from .const import RAIN_COLLECTOR_IMPERIAL, PROTOCOL_NETWORK
from .const import RAIN_COLLECTOR_IMPERIAL, PROTOCOL_NETWORK, DATA_ARCHIVE_PERIOD

_LOGGER: logging.Logger = logging.getLogger(__package__)


class DavisVantageClient:
"""Davis Vantage Client class"""

_vantagepro2: VantagePro2 = None
_vantagepro2: VantagePro2 = None # type: ignore

def __init__(
self,
Expand Down Expand Up @@ -69,7 +69,7 @@ async def async_get_vantagepro2fromurl(self, url: str) -> VantagePro2 | None:
return vp

async def connect_to_station(self):
self._vantagepro2 = await self.async_get_vantagepro2fromurl(self.get_link())
self._vantagepro2 = await self.async_get_vantagepro2fromurl(self.get_link()) # type: ignore

def get_current_data(
self,
Expand Down Expand Up @@ -211,6 +211,7 @@ def get_info(self) -> dict[str, Any] | None:
firmware_version = self._vantagepro2.firmware_version # type: ignore
firmware_date = self._vantagepro2.firmware_date # type: ignore
diagnostics = self._vantagepro2.diagnostics # type: ignore
archive_period = self._vantagepro2.archive_period # type: ignore
except Exception as e:
raise e
finally:
Expand All @@ -219,6 +220,7 @@ def get_info(self) -> dict[str, Any] | None:
"version": firmware_version,
"date": firmware_date,
"diagnostics": diagnostics,
"archive_period": archive_period
}

async def async_get_info(self) -> dict[str, Any] | None:
Expand Down Expand Up @@ -249,7 +251,7 @@ def add_additional_info(self, data: dict[str, Any]) -> None:
)
if data["RainRate"] is not None:
data["IsRaining"] = data["RainRate"] > 0
data["ArchiveInterval"] = self._vantagepro2.archive_period
data["ArchiveInterval"] = self._hass.data.get(DATA_ARCHIVE_PERIOD)

def convert_values(self, data: dict[str, Any]) -> None:
del data["Datetime"]
Expand Down Expand Up @@ -297,8 +299,8 @@ def remove_incorrect_data(
for key in data.keys(): # type: ignore
info_key = re.sub(r"\d+$", "", key) # type: ignore
data_type = data_info.get(info_key, "")
raw_value = raw_data.get(info_key, 0)
if self.is_incorrect_value(raw_value, data_type):
raw_value = raw_data.get(info_key, 0) # type: ignore
if self.is_incorrect_value(raw_value, data_type): # type: ignore
data[key] = None # type: ignore

def is_incorrect_value(self, raw_value: int, data_type: str) -> bool:
Expand All @@ -320,31 +322,31 @@ def add_hilows(self, hilows: HighLowParserRevB | None, data: dict[str, Any]):
if not hilows:
return
data["TempOutHiDay"] = hilows["TempHiDay"]
data["TempOutHiTime"] = self.strtotime(hilows["TempHiTime"])
data["TempOutHiTime"] = self.strtotime(hilows["TempHiTime"]) # type: ignore
data["TempOutLowDay"] = hilows["TempLoDay"]
data["TempOutLowTime"] = self.strtotime(hilows["TempLoTime"])
data["TempOutLowTime"] = self.strtotime(hilows["TempLoTime"]) # type: ignore

data["DewPointHiDay"] = hilows["DewHiDay"]
data["DewPointHiTime"] = self.strtotime(hilows["DewHiTime"])
data["DewPointHiTime"] = self.strtotime(hilows["DewHiTime"]) # type: ignore
data["DewPointLowDay"] = hilows["DewLoDay"]
data["DewPointLowTime"] = self.strtotime(hilows["DewLoTime"])
data["DewPointLowTime"] = self.strtotime(hilows["DewLoTime"]) # type: ignore

data["RainRateDay"] = hilows["RainHiDay"]
data["RainRateTime"] = self.strtotime(hilows["RainHiTime"])
data["RainRateTime"] = self.strtotime(hilows["RainHiTime"]) # type: ignore

data["BarometerHiDay"] = hilows["BaroHiDay"]
data["BarometerHiTime"] = self.strtotime(hilows["BaroHiTime"])
data["BarometerHiTime"] = self.strtotime(hilows["BaroHiTime"]) # type: ignore
data["BarometerLowDay"] = hilows["BaroLoDay"]
data["BarometerLoTime"] = self.strtotime(hilows["BaroLoTime"])
data["BarometerLoTime"] = self.strtotime(hilows["BaroLoTime"]) # type: ignore

data["SolarRadDay"] = hilows["SolarHiDay"]
data["SolarRadTime"] = self.strtotime(hilows["SolarHiTime"])
data["SolarRadTime"] = self.strtotime(hilows["SolarHiTime"]) # type: ignore

data["UVDay"] = hilows["UVHiDay"]
data["UVTime"] = self.strtotime(hilows["UVHiTime"])
data["UVTime"] = self.strtotime(hilows["UVHiTime"]) # type: ignore

data["WindGustDay"] = hilows["WindHiDay"]
data["WindGustTime"] = self.strtotime(hilows["WindHiTime"])
data["WindGustTime"] = self.strtotime(hilows["WindHiTime"]) # type: ignore

def get_link(self) -> str | None:
"""Get device link for use with vproweather."""
Expand Down
4 changes: 3 additions & 1 deletion custom_components/davis_vantage/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
DOMAIN = "davis_vantage"
MANUFACTURER = "Davis"
MODEL = "Vantage Pro2/Vue"
VERSION = "1.1.11"
VERSION = "1.1.12"

DEFAULT_SYNC_INTERVAL = 30 # seconds
DEFAULT_NAME = NAME

DATA_ARCHIVE_PERIOD = 'archive_period'

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

Expand Down

0 comments on commit da563d7

Please sign in to comment.