Skip to content

Commit

Permalink
Update To What I currently have working in Home assistant.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey S. Ruby committed Feb 17, 2024
1 parent 8c5338d commit b422d21
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
12 changes: 6 additions & 6 deletions custom_components/awnet_local/const_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,16 @@
# Each sensor listed here is calculated server-side and depends on the list of
# sensors it is a key for
CALCULATED_SENSOR_TYPES = {
TYPE_LASTRAIN: [TYPE_HOURLYRAININ],
TYPE_FEELSLIKE: [TYPE_TEMPF, TYPE_WINDSPEEDMPH, TYPE_HUMIDITY],
TYPE_DEWPOINT: [TYPE_TEMPF, TYPE_HUMIDITY],
# TYPE_LASTRAIN: [TYPE_HOURLYRAININ],
# TYPE_FEELSLIKE: [TYPE_TEMPF, TYPE_WINDSPEEDMPH, TYPE_HUMIDITY],
# TYPE_DEWPOINT: [TYPE_TEMPF, TYPE_HUMIDITY],
# TYPE_FEELSLIKE_IN: [TYPE_TEMPINF, TYPE_HUMIDITYIN],
# TYPE_DEWPOINT_IN: [TYPE_TEMPINF, TYPE_HUMIDITYIN],
TYPE_SOLARRADIATION_LX: [TYPE_SOLARRADIATION],
TYPE_FEELSLIKE_IN: [TYPE_TEMPINF, TYPE_HUMIDITYIN],
TYPE_DEWPOINT_IN: [TYPE_TEMPINF, TYPE_HUMIDITYIN],
}

# Each sensor listed here is converted server-side from the native unit to the unit that HA supports
CONVERTED_SENSOR_TYPES = [TYPE_LIGHTNING_TIME]
CONVERTED_SENSOR_TYPES = [TYPE_LIGHTNING_TIME, TYPE_LASTRAIN]

# Each sensor listed here should be restored on a restart of HA since it is a timestamp type sensor
RESTORE_SENSOR_TYPES = [TYPE_LIGHTNING_TIME, TYPE_LASTRAIN]
Expand Down
41 changes: 29 additions & 12 deletions custom_components/awnet_local/helpers_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

from datetime import datetime, timezone
import time
import math

from .const_sensor import (
Expand All @@ -31,11 +32,8 @@ class AmbientSensorCalculations:
Ambient Weather stations
"""

global is_raining
is_raining = False

def __init__(self, is_raining):
self.is_raining = is_raining
last_rain_dt = None

@staticmethod
def calculate(entity_key: str, station_values: dict) -> object:
Expand Down Expand Up @@ -104,18 +102,17 @@ def last_rain(hourly_rain_in: float) -> any:
Returns:
any: timestamp if there is data to report; None if it's not raining
"""
global is_raining
# Only change the data when raining stops
# When the above api is followed, a date is returned every station update
if hourly_rain_in > 0.0 and is_raining == False:
if hourly_rain_in > 0.0 and AmbientSensorCalculations.is_raining == False:
# record the start of the rain
is_raining = True
return datetime.now(timezone.utc)
if hourly_rain_in == 0.0 and is_raining:
AmbientSensorCalculations.is_raining = True
AmbientSensorCalculations.last_rain_dt = datetime.now(timezone.utc)
if hourly_rain_in == 0.0 and AmbientSensorCalculations.is_raining:
# record the end of the rain
is_raining = False
return datetime.now(timezone.utc)
return None
AmbientSensorCalculations.is_raining = False
AmbientSensorCalculations.last_rain_dt = datetime.now(timezone.utc)
return AmbientSensorCalculations.last_rain_dt

@staticmethod
def feels_like(tempf: float, wind_mph: float, rel_humid_percent: float) -> float:
Expand Down Expand Up @@ -223,6 +220,19 @@ def dew_point(tempf: float, rel_humid_percent: float) -> float:
dew_pt_c = (const_c * gamma_t_rh) / (const_b - gamma_t_rh)
return float(round(dew_pt_c * 9 / 5 + 32, 1))

@staticmethod
def restore(entity_key: str, value: object) -> None:
"""Calls the correct restore method based on the entity_key
Args:
entity_key (str): key for the entity to restore data to.
Returns:
none:
"""
if entity_key == TYPE_LASTRAIN:
AmbientSensorCalculations.last_rain_dt = value
return None

class AmbientSensorConversions:
"""Class full of static methods for performing conversions from native units to HA units where
Expand All @@ -242,8 +252,15 @@ def convert(entity_key: str, value: object) -> object:
"""
if entity_key == TYPE_LIGHTNING_TIME:
return AmbientSensorConversions.epoch_to_datetime(int(value))
if entity_key == TYPE_LASTRAIN:
return AmbientSensorConversions.string_to_datetime(str(value))
raise NotImplementedError(f"Conversion for {entity_key} is not implemented")

@staticmethod
def epoch_to_datetime(epoch: int) -> str:
return datetime.fromtimestamp(epoch, timezone.utc)

@staticmethod
def string_to_datetime(isoTime: str) -> datetime:
epochTime = time.mktime(time.strptime(isoTime, '%Y-%m-%dT%H:%M:00.000000%z'))
return datetime.fromtimestamp(epochTime, timezone.utc)
2 changes: 2 additions & 0 deletions custom_components/awnet_local/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ async def async_added_to_hass(self) -> None:
_LOGGER.debug("State to restore for %s was %s", self.name, state)
if not state:
return
# restore any values needed in calculated sensors, ie last_rain datetime
#AmbientSensorCalculations.restore(self.entity_description.key, state.native_value)
self._attr_native_value = state.native_value
self._attr_available = True

Expand Down

0 comments on commit b422d21

Please sign in to comment.