From 5f5970944d004057584eb0996bef97a19bb007ee Mon Sep 17 00:00:00 2001 From: Marco Gosselink Date: Wed, 12 Feb 2025 14:02:19 +0100 Subject: [PATCH] Added persistent connection --- custom_components/davis_vantage/__init__.py | 4 ++- custom_components/davis_vantage/client.py | 30 ++++++++++++------- .../davis_vantage/config_flow.py | 4 ++- custom_components/davis_vantage/const.py | 1 + .../davis_vantage/translations/de.json | 3 +- .../davis_vantage/translations/en.json | 3 +- .../davis_vantage/translations/nl.json | 3 +- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/custom_components/davis_vantage/__init__.py b/custom_components/davis_vantage/__init__.py index 9072135..98cb9e5 100755 --- a/custom_components/davis_vantage/__init__.py +++ b/custom_components/davis_vantage/__init__.py @@ -30,6 +30,7 @@ CONFIG_INTERVAL, CONFIG_PROTOCOL, CONFIG_LINK, + CONFIG_PERSISTENT_CONNECTION ) from .coordinator import DavisVantageDataUpdateCoordinator from .utils import convert_to_iso_datetime @@ -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() diff --git a/custom_components/davis_vantage/client.py b/custom_components/davis_vantage/client.py index a5dc3d4..0699c8a 100755 --- a/custom_components/davis_vantage/client.py +++ b/custom_components/davis_vantage/client.py @@ -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: @@ -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 @@ -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: @@ -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 @@ -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: @@ -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.""" @@ -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, @@ -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: @@ -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.""" @@ -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: diff --git a/custom_components/davis_vantage/config_flow.py b/custom_components/davis_vantage/config_flow.py index dae9d4f..fa79804 100755 --- a/custom_components/davis_vantage/config_flow.py +++ b/custom_components/davis_vantage/config_flow.py @@ -26,6 +26,7 @@ CONFIG_MINIMAL_INTERVAL, CONFIG_PROTOCOL, CONFIG_LINK, + CONFIG_PERSISTENT_CONNECTION, ) from .client import DavisVantageClient @@ -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 @@ -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, } ) diff --git a/custom_components/davis_vantage/const.py b/custom_components/davis_vantage/const.py index 900e888..b5ecb8d 100755 --- a/custom_components/davis_vantage/const.py +++ b/custom_components/davis_vantage/const.py @@ -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", diff --git a/custom_components/davis_vantage/translations/de.json b/custom_components/davis_vantage/translations/de.json index ee09224..e10669d 100755 --- a/custom_components/davis_vantage/translations/de.json +++ b/custom_components/davis_vantage/translations/de.json @@ -35,7 +35,8 @@ "description": "Neukonfiguration für {name}.", "data": { "link": "Hostname", - "interval": "Intervall" + "interval": "Intervall", + "persistent_connection": "Persistente Verbindung" } } } diff --git a/custom_components/davis_vantage/translations/en.json b/custom_components/davis_vantage/translations/en.json index f736e90..5e401da 100755 --- a/custom_components/davis_vantage/translations/en.json +++ b/custom_components/davis_vantage/translations/en.json @@ -28,7 +28,8 @@ "setup_other_info": { "data": { "station_model": "Davis weather station model", - "interval": "Interval" + "interval": "Interval", + "persistent_connection": "Persistent Connection" } }, "reconfigure_confirm": { diff --git a/custom_components/davis_vantage/translations/nl.json b/custom_components/davis_vantage/translations/nl.json index 92a5ba3..dc66335 100755 --- a/custom_components/davis_vantage/translations/nl.json +++ b/custom_components/davis_vantage/translations/nl.json @@ -28,7 +28,8 @@ "setup_other_info": { "data": { "station_model": "Davis weerstation model", - "interval": "Interval" + "interval": "Interval", + "persistent_connection": "Persistente verbinding" } }, "reconfigure_confirm": {