Skip to content

Commit

Permalink
Added persistent connection
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Feb 12, 2025
1 parent 7ec0828 commit 5f59709
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 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 @@ -30,6 +30,7 @@
CONFIG_INTERVAL,
CONFIG_PROTOCOL,
CONFIG_LINK,
CONFIG_PERSISTENT_CONNECTION
)
from .coordinator import DavisVantageDataUpdateCoordinator
from .utils import convert_to_iso_datetime
Expand All @@ -52,10 +53,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

protocol = entry.data.get(CONFIG_PROTOCOL, "")
link = entry.data.get(CONFIG_LINK, "")
persistent_connection = entry.data.get(CONFIG_PERSISTENT_CONNECTION, False)

hass.data[DOMAIN]["interval"] = entry.data.get(CONFIG_INTERVAL, 30)

client = DavisVantageClient(hass, protocol, link)
client = DavisVantageClient(hass, protocol, link, persistent_connection)
await client.connect_to_station()
await client.get_station_info()

Expand Down
30 changes: 20 additions & 10 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ class DavisVantageClient:
_elevation: int = 0
_firmware_version: str|None = None

def __init__(self, hass: HomeAssistant, protocol: str, link: str) -> None:
def __init__(self, hass: HomeAssistant, protocol: str, link: str, persistent_connection: bool) -> None:
self._hass = hass
self._protocol = protocol
self._link = link
self._rain_collector = ""
self._last_data: LoopDataParserRevB = {} # type: ignore
self._last_raw_data: DataParser = {} # type: ignore
self._persistent_connection = persistent_connection

@property
def latitude(self) -> float:
Expand All @@ -73,7 +74,8 @@ def firmware_version(self) -> str|None:
def get_vantagepro2fromurl(self, url: str) -> VantagePro2 | None:
try:
vp = VantagePro2.from_url(url)
vp.link.close()
if not self._persistent_connection:
vp.link.close()
return vp
except Exception as e:
raise e
Expand Down Expand Up @@ -119,7 +121,8 @@ def get_current_data(
self._vantagepro2.link.open()
data = self._vantagepro2.get_current_data()
except Exception as e:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()
raise e

try:
Expand All @@ -139,7 +142,8 @@ def get_current_data(
try:
self._rain_collector = self.get_rain_collector()
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()

return data, archives, hilows

Expand Down Expand Up @@ -215,7 +219,8 @@ def get_davis_time(self) -> datetime | None:
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()
return data

async def async_get_davis_time(self) -> datetime | None:
Expand All @@ -236,7 +241,8 @@ def set_davis_time(self, dtime: datetime) -> None:
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()

async def async_set_davis_time(self) -> None:
"""Set time of weather station async."""
Expand All @@ -256,7 +262,8 @@ def get_info(self) -> dict[str, Any] | None:
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()
return {
"version": firmware_version,
"date": firmware_date,
Expand All @@ -281,7 +288,8 @@ def get_static_info(self) -> dict[str, Any] | None:
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()
return {"version": firmware_version, "archive_period": archive_period}

async def async_get_static_info(self) -> dict[str, Any] | None:
Expand All @@ -301,7 +309,8 @@ def set_yearly_rain(self, rain_clicks: int) -> None:
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()

async def async_set_yearly_rain(self, rain_clicks: int) -> None:
"""Set yearly rain of weather station async."""
Expand All @@ -319,7 +328,8 @@ def set_archive_period(self, archive_period: int) -> None:
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
if not self._persistent_connection:
self._vantagepro2.link.close()

async def async_set_archive_period(self, archive_period: int) -> None:
try:
Expand Down
4 changes: 3 additions & 1 deletion custom_components/davis_vantage/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
CONFIG_MINIMAL_INTERVAL,
CONFIG_PROTOCOL,
CONFIG_LINK,
CONFIG_PERSISTENT_CONNECTION,
)
from .client import DavisVantageClient

Expand All @@ -51,7 +52,7 @@ def __init__(self, hass: HomeAssistant) -> None:
async def authenticate(self, protocol: str, link: str) -> bool:
"""Test if we can find data for the given link."""
_LOGGER.info("authenticate called")
client = DavisVantageClient(self._hass, protocol, link)
client = DavisVantageClient(self._hass, protocol, link, False)
await client.connect_to_station()
return (await client.async_get_davis_time()) is not None

Expand Down Expand Up @@ -155,6 +156,7 @@ async def async_step_setup_other_info(
vol.Required(CONFIG_INTERVAL, default=DEFAULT_SYNC_INTERVAL): vol.All(
int, vol.Range(min=CONFIG_MINIMAL_INTERVAL) # type: ignore
),
vol.Required(CONFIG_PERSISTENT_CONNECTION, default=False): bool,
}
)

Expand Down
1 change: 1 addition & 0 deletions custom_components/davis_vantage/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
CONFIG_MINIMAL_INTERVAL = 5
CONFIG_PROTOCOL = "protocol"
CONFIG_LINK = "link"
CONFIG_PERSISTENT_CONNECTION = "persistent_connection"

KEY_TO_NAME = {
"Datetime": "Last Fetch Time",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/davis_vantage/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"description": "Neukonfiguration für {name}.",
"data": {
"link": "Hostname",
"interval": "Intervall"
"interval": "Intervall",
"persistent_connection": "Persistente Verbindung"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/davis_vantage/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"setup_other_info": {
"data": {
"station_model": "Davis weather station model",
"interval": "Interval"
"interval": "Interval",
"persistent_connection": "Persistent Connection"
}
},
"reconfigure_confirm": {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/davis_vantage/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"setup_other_info": {
"data": {
"station_model": "Davis weerstation model",
"interval": "Interval"
"interval": "Interval",
"persistent_connection": "Persistente verbinding"
}
},
"reconfigure_confirm": {
Expand Down

0 comments on commit 5f59709

Please sign in to comment.