Skip to content

Commit

Permalink
Added services get and set davis time
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Oct 7, 2023
1 parent 4fa3665 commit ba6dde6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
26 changes: 22 additions & 4 deletions custom_components/davis_vantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down
18 changes: 16 additions & 2 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from time import sleep
from pyvantagepro import VantagePro2
from pyvantagepro.parser import LoopDataParserRevB
import asyncio

from homeassistant.core import HomeAssistant

Expand Down Expand Up @@ -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'])
Expand Down
7 changes: 5 additions & 2 deletions custom_components/davis_vantage/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,4 +13,7 @@
RAIN_COLLECTOR_METRIC = '0.2 mm'

PROTOCOL_NETWORK = 'Network'
PROTOCOL_SERIAL = 'Serial'
PROTOCOL_SERIAL = 'Serial'

SERVICE_SET_DAVIS_TIME = 'set_davis_time'
SERVICE_GET_DAVIS_TIME = 'get_davis_time'
2 changes: 1 addition & 1 deletion custom_components/davis_vantage/manifest.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
3 changes: 3 additions & 0 deletions custom_components/davis_vantage/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set_davis_time:

get_davis_time:

0 comments on commit ba6dde6

Please sign in to comment.