Skip to content

Commit

Permalink
Further improved stability and added unit of measure to solar radiation
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Oct 14, 2023
1 parent 31f6cb7 commit f6927d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
41 changes: 31 additions & 10 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ def __init__(
self._vantagepro2 = VantagePro2.from_url(self.get_link())
self._vantagepro2.link.close()

def get_current_data(self) -> LoopDataParserRevB | None:
try:
self._vantagepro2.link.open()
data = self._vantagepro2.get_current_data()
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
return data

async def async_get_current_data(self) -> LoopDataParserRevB | None:
"""Get current date from weather station."""
data = self._last_data
try:
self._vantagepro2.link.open()
loop = asyncio.get_event_loop()
new_data = await loop.run_in_executor(None, self._vantagepro2.get_current_data)
new_data = await loop.run_in_executor(None, self.get_current_data)
if new_data:
if contains_correct_data(new_data):
self.add_additional_info(new_data)
Expand All @@ -50,34 +59,46 @@ async def async_get_current_data(self) -> LoopDataParserRevB | None:
except Exception as e:
_LOGGER.warning(f"Couldn't acquire data from {self.get_link()}")
data['LastError'] = f"Couldn't acquire data: {e}"
self._last_data = data
return data

def get_davis_time(self) -> datetime | None:
data = None
try:
self._vantagepro2.link.open()
data = self._vantagepro2.gettime()
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
self._last_data = data
return data

async def async_get_davis_time(self) -> datetime | None:
"""Get time from weather station."""
data = None
try:
self._vantagepro2.link.open()
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, self._vantagepro2.gettime)
data = await loop.run_in_executor(None, self.get_davis_time)
except Exception as e:
_LOGGER.error(f"Couldn't get davis time: {e}")
return data

def set_davis_time(self, dtime: datetime) -> None:
try:
self._vantagepro2.link.open()
self._vantagepro2.settime(dtime)
except Exception as e:
raise e
finally:
self._vantagepro2.link.close()
return data

async def async_set_davis_time(self) -> None:
"""Set time of weather station."""
try:
self._vantagepro2.link.open()
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, self._vantagepro2.settime, datetime.now())
await loop.run_in_executor(None, self.set_davis_time, datetime.now())
except Exception as e:
_LOGGER.error(f"Couldn't set davis time: {e}")
finally:
self._vantagepro2.link.close()

def add_additional_info(self, data: dict[str, Any]) -> None:
data['HeatIndex'] = calc_heat_index(data['TempOut'], data['HumOut'])
Expand Down
2 changes: 1 addition & 1 deletion 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.8"
VERSION = "1.0.9"

DEFAULT_SYNC_INTERVAL = 30 # seconds
DEFAULT_NAME = NAME
Expand Down
5 changes: 3 additions & 2 deletions custom_components/davis_vantage/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
SensorDeviceClass
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, DEGREE, UnitOfSpeed, UnitOfLength, UnitOfVolumetricFlux, UnitOfElectricPotential, UnitOfTemperature, UnitOfPressure
from homeassistant.const import PERCENTAGE, DEGREE, UnitOfSpeed, UnitOfLength, UnitOfVolumetricFlux, UnitOfElectricPotential, UnitOfTemperature, UnitOfPressure, UnitOfIrradiance
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -189,7 +189,8 @@
name="Solar Radiation",
icon="mdi:sun-wireless-outline",
state_class="measurement",
entity_registry_enabled_default=False
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfIrradiance.WATTS_PER_SQUARE_METER
),
SensorEntityDescription(
key="BatteryVolts",
Expand Down

0 comments on commit f6927d6

Please sign in to comment.