Skip to content

Commit

Permalink
changes to rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
siku2 committed May 16, 2021
1 parent f061af7 commit cdb5fba
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 74 deletions.
5 changes: 1 addition & 4 deletions custom_components/weatherlink/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import asyncio
import dataclasses
import logging
from datetime import timedelta
from typing import cast

from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers import aiohttp_client
Expand All @@ -11,7 +8,7 @@
DataUpdateCoordinator,
)

from .api import CurrentConditions, DeviceType, WeatherLinkSession
from .api import CurrentConditions, WeatherLinkSession
from .const import DOMAIN, PLATFORMS

logger = logging.getLogger(__name__)
Expand Down
8 changes: 4 additions & 4 deletions custom_components/weatherlink/air_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import WeatherLinkCoordinator, WeatherLinkEntity
from .api import AirQualityCondition
from .const import DOMAIN
from .const import DECIMALS_PM, DOMAIN

logger = logging.getLogger(__name__)

Expand All @@ -28,15 +28,15 @@ def name(self):

@property
def particulate_matter_2_5(self) -> float:
return round(self._aq_condition.pm_2p5_nowcast, 2)
return round(self._aq_condition.pm_2p5_nowcast, DECIMALS_PM)

@property
def particulate_matter_10(self) -> float:
return round(self._aq_condition.pm_10_nowcast, 2)
return round(self._aq_condition.pm_10_nowcast, DECIMALS_PM)

@property
def particulate_matter_0_1(self) -> float:
return round(self._aq_condition.pm_1, 2)
return round(self._aq_condition.pm_1, DECIMALS_PM)

# TODO calculate AQI
# @property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/weatherlink/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class IssCondition(ConditionRecord):
wind_dir_scalar_avg_last_2_min: float
"""scalar average wind direction over last 2 min **(°degree)**"""

wind_speed_hi_last_2_min: float
wind_speed_hi_last_2_min: Optional[float]
"""maximum wind speed over last 2 min **(km/h)**"""
wind_dir_at_hi_speed_last_2_min: float
"""gust wind direction over last 2 min **(°degree)**"""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/weatherlink/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataclasses
import logging
from typing import Any, Dict, Optional
from typing import Any, Dict

import voluptuous as vol
from homeassistant import config_entries
Expand Down
15 changes: 15 additions & 0 deletions custom_components/weatherlink/const.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
DOMAIN = "weatherlink"
PLATFORMS = ("air_quality", "sensor", "weather")


DECIMALS_DIRECTION = None
DECIMALS_HUMIDITY = None
DECIMALS_PM = 2
DECIMALS_PRESSURE = None
DECIMALS_PRESSURE_TREND = 1
DECIMALS_RADIATION = None
DECIMALS_RAIN_RATE = 1
DECIMALS_RAIN_VOLUME = 1
DECIMALS_SPEED = 1
DECIMALS_TEMPERATURE = 1
DECIMALS_UV = 1
DECIMALS_LEAF_WETNESS = None
DECIMALS_SOIL_MOISTURE = None
37 changes: 29 additions & 8 deletions custom_components/weatherlink/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from . import WeatherLinkCoordinator
from .api import LssBarCondition, LssTempHumCondition
from .const import DOMAIN
from .const import (
DECIMALS_HUMIDITY,
DECIMALS_PRESSURE,
DECIMALS_PRESSURE_TREND,
DECIMALS_TEMPERATURE,
DOMAIN,
)
from .sensor_air_quality import *
from .sensor_common import WeatherLinkSensor, round_optional
from .sensor_iss import *
Expand All @@ -26,14 +32,14 @@ def _lss_bar_condition(self) -> LssBarCondition:

@property
def state(self):
return round(self._lss_bar_condition.bar_sea_level, 1)
return round(self._lss_bar_condition.bar_sea_level, DECIMALS_PRESSURE)

@property
def device_state_attributes(self):
condition = self._lss_bar_condition
return {
"trend": round_optional(condition.bar_trend, 1),
"absolute": round(condition.bar_absolute, 1),
"trend": round_optional(condition.bar_trend, DECIMALS_PRESSURE_TREND),
"absolute": round(condition.bar_absolute, DECIMALS_PRESSURE),
}


Expand All @@ -50,13 +56,28 @@ def _lss_temp_hum_condition(self) -> LssTempHumCondition:

@property
def state(self):
return round(self._lss_temp_hum_condition.temp_in, 1)
return round(self._lss_temp_hum_condition.temp_in, DECIMALS_TEMPERATURE)

@property
def device_state_attributes(self):
condition = self._lss_temp_hum_condition
return {
"humidity": round(condition.hum_in, 1),
"dew_point": round(condition.dew_point_in, 1),
"heat_index": round(condition.heat_index_in, 1),
"dew_point": round(condition.dew_point_in, DECIMALS_TEMPERATURE),
"heat_index": round(condition.heat_index_in, DECIMALS_TEMPERATURE),
}


class InsideHum(
WeatherLinkSensor,
sensor_name="Inside Humidity",
unit_of_measurement="%",
device_class="humidity",
required_conditions=(LssTempHumCondition,),
):
@property
def _lss_temp_hum_condition(self) -> LssTempHumCondition:
return self._conditions[LssTempHumCondition]

@property
def state(self):
return round(self._lss_temp_hum_condition.hum_in, DECIMALS_HUMIDITY)
33 changes: 17 additions & 16 deletions custom_components/weatherlink/sensor_air_quality.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime

from .api import AirQualityCondition
from .const import DECIMALS_HUMIDITY, DECIMALS_PM, DECIMALS_TEMPERATURE
from .sensor_common import WeatherLinkSensor

__all__ = [
Expand Down Expand Up @@ -74,15 +75,15 @@ class Temperature(
):
@property
def state(self):
return round(self._aq_condition.temp, 1)
return round(self._aq_condition.temp, DECIMALS_TEMPERATURE)

@property
def device_state_attributes(self):
c = self._aq_condition
return {
"dew_point": round(c.dew_point, 1),
"wet_bulb": round(c.wet_bulb, 1),
"heat_index": round(c.heat_index, 1),
"dew_point": round(c.dew_point, DECIMALS_TEMPERATURE),
"wet_bulb": round(c.wet_bulb, DECIMALS_TEMPERATURE),
"heat_index": round(c.heat_index, DECIMALS_TEMPERATURE),
}


Expand All @@ -94,7 +95,7 @@ class Humidity(
):
@property
def state(self):
return round(self._aq_condition.hum, 1)
return round(self._aq_condition.hum, DECIMALS_HUMIDITY)


class Pm1p0(
Expand All @@ -109,7 +110,7 @@ def icon(self):

@property
def state(self):
return round(self._aq_condition.pm_1, 2)
return round(self._aq_condition.pm_1, DECIMALS_PM)


class Pm2p5(
Expand All @@ -124,16 +125,16 @@ def icon(self):

@property
def state(self):
return round(self._aq_condition.pm_2p5_nowcast, 2)
return round(self._aq_condition.pm_2p5_nowcast, DECIMALS_PM)

@property
def device_state_attributes(self):
c = self._aq_condition
return {
"1_min": round(c.pm_2p5, 2),
"1_hr": round(c.pm_2p5_last_1_hour, 2),
"3_hr": round(c.pm_2p5_last_3_hours, 2),
"24_hr": round(c.pm_2p5_last_24_hours, 2),
"1_min": round(c.pm_2p5, DECIMALS_PM),
"1_hr": round(c.pm_2p5_last_1_hour, DECIMALS_PM),
"3_hr": round(c.pm_2p5_last_3_hours, DECIMALS_PM),
"24_hr": round(c.pm_2p5_last_24_hours, DECIMALS_PM),
}


Expand All @@ -149,14 +150,14 @@ def icon(self):

@property
def state(self):
return round(self._aq_condition.pm_10_nowcast, 2)
return round(self._aq_condition.pm_10_nowcast, DECIMALS_PM)

@property
def device_state_attributes(self):
c = self._aq_condition
return {
"1_min": round(c.pm_10, 2),
"1_hr": round(c.pm_10_last_1_hour, 2),
"3_hr": round(c.pm_10_last_3_hours, 2),
"24_hr": round(c.pm_10_last_24_hours, 2),
"1_min": round(c.pm_10, DECIMALS_PM),
"1_hr": round(c.pm_10_last_1_hour, DECIMALS_PM),
"3_hr": round(c.pm_10_last_3_hours, DECIMALS_PM),
"24_hr": round(c.pm_10_last_24_hours, DECIMALS_PM),
}
6 changes: 4 additions & 2 deletions custom_components/weatherlink/sensor_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import typing
from typing import Iterable, Iterator, List, Optional, Type
from typing import Iterable, Iterator, List, Optional, Type, Union

from . import WeatherLinkCoordinator, WeatherLinkEntity
from .api import ConditionRecord, CurrentConditions
Expand Down Expand Up @@ -91,7 +91,9 @@ def device_class(self):
return self._device_class


def round_optional(f: Optional[float], ndigits: int = 0) -> Optional[float]:
def round_optional(
f: Optional[Union[int, float]], ndigits: int = None
) -> Optional[Union[int, float]]:
if not f:
return f
return round(f, ndigits)
Loading

0 comments on commit cdb5fba

Please sign in to comment.