diff --git a/custom_components/davis_vantage/__init__.py b/custom_components/davis_vantage/__init__.py index 9ed6f3c..52e746f 100755 --- a/custom_components/davis_vantage/__init__.py +++ b/custom_components/davis_vantage/__init__.py @@ -4,14 +4,15 @@ import logging from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceEntryType +from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse from homeassistant.helpers.entity import DeviceInfo -from homeassistant.const import Platform +from homeassistant.const import Platform, CONF_NAME, CONF_URL +from zoneinfo import ZoneInfo from .client import DavisVantageClient -from .const import DOMAIN, NAME, VERSION, MANUFACTURER +from .const import DOMAIN, NAME, VERSION, MANUFACTURER, SERVICE_SET_DAVIS_TIME, SERVICE_GET_DAVIS_TIME from .coordinator import DavisVantageDataUpdateCoordinator +from .utils import convert_to_iso_datetime PLATFORMS: list[Platform] = [ Platform.SENSOR, @@ -54,6 +55,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) entry.async_on_unload(entry.add_update_listener(async_reload_entry)) + async def set_davis_time(call: ServiceCall) -> None: + await client.async_set_davis_time() + + async def get_davis_time(call: ServiceCall) -> SupportsResponse: + davis_time = await client.async_get_davis_time() + if davis_time is not None: + return { "davis_time": convert_to_iso_datetime(davis_time, ZoneInfo(hass.config.time_zone)) } + else: + return { "error": "Couldn't get davis time, please try again later"} + + hass.services.async_register( + DOMAIN, SERVICE_SET_DAVIS_TIME, set_davis_time + ) + hass.services.async_register( + DOMAIN, SERVICE_GET_DAVIS_TIME, get_davis_time, supports_response=SupportsResponse.ONLY + ) + return True diff --git a/custom_components/davis_vantage/client.py b/custom_components/davis_vantage/client.py index 6e4c7ca..b803e54 100755 --- a/custom_components/davis_vantage/client.py +++ b/custom_components/davis_vantage/client.py @@ -4,6 +4,7 @@ from time import sleep from pyvantagepro import VantagePro2 from pyvantagepro.parser import LoopDataParserRevB +import asyncio from homeassistant.core import HomeAssistant @@ -61,12 +62,25 @@ async def async_get_davis_time(self) -> datetime | None: return data except Exception as e: last_error = e - pass - # sleep(0.2) + asyncio.sleep(1) tries -=1 _LOGGER.error(f"Couldn't acquire data from {self.get_link()}: {last_error}") return None + async def async_set_davis_time(self) -> None: + tries = 3 + last_error: Exception = None + while tries > 0: + try: + vantagepro2 = VantagePro2.from_url(self.get_link()) + vantagepro2.settime(datetime.now()) + return + except Exception as e: + last_error = e + asyncio.sleep(1) + tries -= 1 + _LOGGER.error(f"Couldn't acquire data from {self.get_link()}: {last_error}") + def add_additional_info(self, data: dict[str, Any]) -> None: data['HeatIndex'] = calc_heat_index(data['TempOut'], data['HumOut']) data['WindChill'] = calc_wind_chill(data['TempOut'], data['WindSpeed']) diff --git a/custom_components/davis_vantage/const.py b/custom_components/davis_vantage/const.py index 01aa1e6..2a69c86 100755 --- a/custom_components/davis_vantage/const.py +++ b/custom_components/davis_vantage/const.py @@ -4,7 +4,7 @@ DOMAIN = "davis_vantage" MANUFACTURER = "Davis" MODEL = "Vantage Pro2/Vue" -VERSION = "1.0.3" +VERSION = "1.0.4" DEFAULT_SYNC_INTERVAL = 30 # seconds DEFAULT_NAME = NAME @@ -13,4 +13,7 @@ RAIN_COLLECTOR_METRIC = '0.2 mm' PROTOCOL_NETWORK = 'Network' -PROTOCOL_SERIAL = 'Serial' \ No newline at end of file +PROTOCOL_SERIAL = 'Serial' + +SERVICE_SET_DAVIS_TIME = 'set_davis_time' +SERVICE_GET_DAVIS_TIME = 'get_davis_time' diff --git a/custom_components/davis_vantage/manifest.json b/custom_components/davis_vantage/manifest.json index ce4f505..e0a81aa 100755 --- a/custom_components/davis_vantage/manifest.json +++ b/custom_components/davis_vantage/manifest.json @@ -1,7 +1,7 @@ { "domain": "davis_vantage", "name": "Davis Vantage", - "version": "1.0.3", + "version": "1.0.0", "config_flow": true, "documentation": "https://github.com/MarcoGos/davis_vantage", "requirements": ["PyVantagePro==0.3.2"], diff --git a/custom_components/davis_vantage/services.yaml b/custom_components/davis_vantage/services.yaml new file mode 100644 index 0000000..34dbda9 --- /dev/null +++ b/custom_components/davis_vantage/services.yaml @@ -0,0 +1,3 @@ +set_davis_time: + +get_davis_time: \ No newline at end of file