Skip to content

Commit

Permalink
configurable rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
siku2 committed May 29, 2021
1 parent 69efa2f commit e1d8a7b
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 146 deletions.
9 changes: 3 additions & 6 deletions custom_components/weatherlink/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,15 @@ async def current_conditions(self) -> CurrentConditions:


def fahrenheit_to_celsius(value: float) -> float:
c = (value - 32) * 5 / 9
return round(c, 3)
return (value - 32) * 5 / 9


def mph_to_kph(value: float) -> float:
kph = 1.60934 * value
return round(kph, 3)
return 1.609344 * value


def in_hg_to_hpa(value: float) -> float:
hpa = 33.86389 * value
return round(hpa, 3)
return 33.86389 * value


def json_apply_converters(d: JsonObject, **converters: Callable[[Any], Any]) -> None:
Expand Down
17 changes: 14 additions & 3 deletions custom_components/weatherlink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async def async_step_zeroconf_confirm(self, user_input=None):
class OptionsFlow(config_entries.OptionsFlow):
config_entry: config_entries.ConfigEntry
options: dict
units_config: dict

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
super().__init__()
Expand Down Expand Up @@ -127,13 +128,23 @@ async def async_step_misc(self, user_input=None):

async def async_step_units(self, user_input=None):
if user_input is not None:
config = UnitConfig.from_unit_of_measurement(user_input)
self.units_config = user_input
return await self.async_step_rounding()

return self.async_show_form(
step_id="units",
data_schema=get_unit_config(self.hass, self.config_entry).units_schema(),
)

async def async_step_rounding(self, user_input=None):
if user_input is not None:
config = UnitConfig.from_config_flow(self.units_config, user_input)
self.options["units"] = config.as_dict()
return await self.finish()

return self.async_show_form(
step_id="units",
data_schema=get_unit_config(self.hass, self.config_entry).data_schema(),
step_id="rounding",
data_schema=get_unit_config(self.hass, self.config_entry).rounding_schema(),
)

async def finish(self):
Expand Down
6 changes: 3 additions & 3 deletions custom_components/weatherlink/sensor_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from . import WeatherLinkCoordinator, WeatherLinkEntity
from .api import ConditionRecord, CurrentConditions
from .units import Units
from .units import Measurement

logger = logging.getLogger(__name__)

Expand All @@ -17,7 +17,7 @@ def __init_subclass__(
cls,
*,
sensor_name: str,
unit_of_measurement: Union[str, Type[Units], None],
unit_of_measurement: Union[str, Type[Measurement], None],
device_class: Optional[str],
required_conditions: Iterable[Type[ConditionRecord]] = None,
**kwargs,
Expand Down Expand Up @@ -89,7 +89,7 @@ def unit_of_measurement(self):
if unit is None or isinstance(unit, str):
return unit

return self.units.by_units(unit).unit_of_measurement
return self.units.by_measurement(unit).info.unit_of_measurement

@property
def device_class(self):
Expand Down
14 changes: 14 additions & 0 deletions custom_components/weatherlink/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,26 @@
"step": {
"misc": {
"title": "Misc",
"description": "(1 / 3)",
"data": {
"update_interval": "Update interval"
}
},
"units": {
"title": "Units",
"description": "(2 / 3)",
"data": {
"temperature": "Temperature",
"pressure": "Pressure",
"wind_speed": "Wind Speed",
"pm": "Pm",
"rain_rate": "Rain rate",
"rainfall": "Rain volume"
}
},
"rounding": {
"title": "Rounding",
"description": "(3 / 3)",
"data": {
"temperature": "Temperature",
"pressure": "Pressure",
Expand Down
Loading

0 comments on commit e1d8a7b

Please sign in to comment.