Skip to content

Commit

Permalink
Reject incorrect values
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGos committed Oct 3, 2023
1 parent f350163 commit d1b928b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
1 change: 0 additions & 1 deletion custom_components/davis_vantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
client = DavisVantageClient(hass, protocol, link, rain_collector, windrose8)

device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, entry.entry_id)},
manufacturer=MANUFACTURER,
name=NAME,
Expand Down
20 changes: 12 additions & 8 deletions custom_components/davis_vantage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ def __init__(
self._link = link
self._windrose8 = windrose8
self._rain_collector = rain_collector
self._last_data = LoopDataParserRevB
self._last_data = {}

async def async_get_current_data(self) -> LoopDataParserRevB | None:
"""Get current date from weather station."""
data = self._last_data
try:
vantagepro2 = VantagePro2.from_url(self.get_link()) # type: ignore
data = vantagepro2.get_current_data()
if data:
self.add_additional_info(data)
self.convert_values(data)
data['LastError'] = "No error"
self._last_data = data
new_data = vantagepro2.get_current_data()
if new_data:
if contains_correct_data(new_data):
self.add_additional_info(new_data)
self.convert_values(new_data)
data = new_data
data['LastError'] = "No error"
else:
data['LastError'] = "Received incorrect data"
else:
data['LastError'] = "Couldn't acquire data, no data received"
except Exception as e:
_LOGGER.warning(f"Couldn't acquire data from {self.get_link()}")
data['LastError'] = f"Couldn't acquire data: {e}" # type: ignore
self._last_data = data
return data # type: ignore

async def async_get_davis_time(self) -> datetime | None:
Expand All @@ -58,7 +62,7 @@ async def async_get_davis_time(self) -> datetime | None:
except Exception as e:
last_error = e
pass
sleep(0.2)
# sleep(0.2)
tries -=1
_LOGGER.error(f"Couldn't acquire data from {self.get_link()}: {last_error}")
return None
Expand Down
4 changes: 2 additions & 2 deletions 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": "0.0.2",
"version": "0.0.3",
"config_flow": true,
"documentation": "https://github.com/MarcoGos/davis_vantage",
"requirements": ["PyVantagePro==0.3.2"],
Expand All @@ -12,6 +12,6 @@
"codeowners": [
"@MarcoGos"
],
"iot_class": "",
"iot_class": "local_polling",
"integration_type": "hub"
}
16 changes: 9 additions & 7 deletions custom_components/davis_vantage/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
),
SensorEntityDescription(
key="TempOut",
name="Temperature (Outside)",
name="Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class="measurement",
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
Expand All @@ -35,9 +35,9 @@
key="TempIn",
name="Temperature (Inside)",
device_class=SensorDeviceClass.TEMPERATURE,
state_class="measurement",
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
suggested_display_precision=1
suggested_display_precision=1,
entity_registry_enabled_default=False
),
SensorEntityDescription(
key="HeatIndex",
Expand Down Expand Up @@ -91,13 +91,13 @@
key="HumIn",
name="Humidity (Inside)",
device_class=SensorDeviceClass.HUMIDITY,
state_class="measurement",
native_unit_of_measurement=PERCENTAGE,
suggested_display_precision=0
suggested_display_precision=0,
entity_registry_enabled_default=False
),
SensorEntityDescription(
key="HumOut",
name="Humidity (Outside)",
name="Humidity",
device_class=SensorDeviceClass.HUMIDITY,
state_class="measurement",
native_unit_of_measurement=PERCENTAGE,
Expand Down Expand Up @@ -146,14 +146,15 @@
name="Rain (Day)",
icon="mdi:water",
device_class=SensorDeviceClass.PRECIPITATION,
state_class="total_increasing",
state_class="measurement",
native_unit_of_measurement=UnitOfLength.INCHES,
suggested_display_precision=1
),
SensorEntityDescription(
key="RainMonth",
name="Rain (Month)",
icon="mdi:water",
state_class="measurement",
device_class=SensorDeviceClass.PRECIPITATION,
native_unit_of_measurement=UnitOfLength.INCHES,
suggested_display_precision=1
Expand All @@ -162,6 +163,7 @@
key="RainYear",
name="Rain (Year)",
icon="mdi:water",
state_class="measurement",
device_class=SensorDeviceClass.PRECIPITATION,
native_unit_of_measurement=UnitOfLength.INCHES,
suggested_display_precision=1
Expand Down
12 changes: 6 additions & 6 deletions custom_components/davis_vantage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ def convert_ms_to_bft(windspeed: float) -> int:
def convert_kmh_to_bft(windspeed_kmh: float) -> int:
return convert_ms_to_bft(convert_kmh_to_ms(windspeed_kmh))

def contains_correct_data(json_data: dict[str, Any]) -> None:
return json_data['OutsideTemp']['value'] < 60 \
and json_data['RainRate']['value'] < 1000 \
and json_data['WindSpeed']['value'] < 40 \
and json_data['OutsideHum']['value'] < 100 \
and json_data['WindAvgSpeed']['value'] < 250
def contains_correct_data(data: dict[str, Any]) -> None:
return data['TempOut'] * 10 != 32767 \
and data['RainRate'] * 100 != 32767 \
and data['WindSpeed'] != 255 \
and data['HumOut'] != 255 \
and data['WindSpeed10Min'] != 255

def calc_heat_index(temperature_f: float, humidity: float) -> float:
if temperature_f < 80.0 or humidity < 40.0:
Expand Down

0 comments on commit d1b928b

Please sign in to comment.