Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggested_display_precision to sensor values #187

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

target-version = "py310"

select = [
lint.select = [
"B007", # Loop control variable {name} not used within loop body
"B014", # Exception handler with duplicate exception
"C", # complexity
Expand All @@ -26,7 +26,7 @@ select = [
"W", # pycodestyle
]

ignore = [
lint.ignore = [
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
Expand All @@ -36,7 +36,7 @@ ignore = [
"D411", # Missing blank line before section
"E501", # line too long
"E731", # do not assign a lambda expression, use a def
# May conflict with the formatter, https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191",
"E111",
Expand All @@ -57,11 +57,11 @@ ignore = [
"PLE0605",
]

[flake8-pytest-style]
[lint.flake8-pytest-style]
fixture-parentheses = false

[pyupgrade]
[lint.pyupgrade]
keep-runtime-typing = true

[mccabe]
[lint.mccabe]
max-complexity = 25
34 changes: 29 additions & 5 deletions custom_components/pirateweather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfLength.KILOMETERS,
uk_unit=UnitOfLength.KILOMETERS,
uk2_unit=UnitOfLength.MILES,
suggested_display_precision=2,
icon="mdi:weather-lightning",
forecast_mode=["currently"],
),
Expand All @@ -145,6 +146,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=DEGREE,
uk_unit=DEGREE,
uk2_unit=DEGREE,
suggested_display_precision=0,
icon="mdi:weather-lightning",
forecast_mode=["currently"],
),
Expand All @@ -162,6 +164,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
uk_unit=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
uk2_unit=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
suggested_display_precision=4,
icon="mdi:weather-rainy",
forecast_mode=["currently", "minutely", "hourly", "daily"],
),
Expand All @@ -173,6 +176,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=PERCENTAGE,
uk_unit=PERCENTAGE,
uk2_unit=PERCENTAGE,
suggested_display_precision=0,
icon="mdi:water-percent",
forecast_mode=["currently", "minutely", "hourly", "daily"],
),
Expand All @@ -185,6 +189,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfLength.CENTIMETERS,
uk_unit=UnitOfLength.CENTIMETERS,
uk2_unit=UnitOfLength.CENTIMETERS,
suggested_display_precision=4,
icon="mdi:weather-snowy",
forecast_mode=["hourly", "daily"],
),
Expand All @@ -198,6 +203,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["currently", "hourly"],
),
"apparent_temperature": PirateWeatherSensorEntityDescription(
Expand All @@ -210,6 +216,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["currently", "hourly"],
),
"dew_point": PirateWeatherSensorEntityDescription(
Expand All @@ -222,6 +229,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["currently", "hourly", "daily"],
),
"wind_speed": PirateWeatherSensorEntityDescription(
Expand All @@ -233,6 +241,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfSpeed.KILOMETERS_PER_HOUR,
uk_unit=UnitOfSpeed.MILES_PER_HOUR,
uk2_unit=UnitOfSpeed.MILES_PER_HOUR,
suggested_display_precision=2,
icon="mdi:weather-windy",
forecast_mode=["currently", "hourly", "daily"],
),
Expand All @@ -244,6 +253,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=DEGREE,
uk_unit=DEGREE,
uk2_unit=DEGREE,
suggested_display_precision=0,
icon="mdi:compass",
forecast_mode=["currently", "hourly", "daily"],
),
Expand All @@ -256,6 +266,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfSpeed.KILOMETERS_PER_HOUR,
uk_unit=UnitOfSpeed.MILES_PER_HOUR,
uk2_unit=UnitOfSpeed.MILES_PER_HOUR,
suggested_display_precision=2,
icon="mdi:weather-windy-variant",
forecast_mode=["currently", "hourly", "daily"],
),
Expand All @@ -267,6 +278,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=PERCENTAGE,
uk_unit=PERCENTAGE,
uk2_unit=PERCENTAGE,
suggested_display_precision=0,
icon="mdi:weather-partly-cloudy",
forecast_mode=["currently", "hourly", "daily"],
),
Expand All @@ -280,6 +292,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=PERCENTAGE,
uk_unit=PERCENTAGE,
uk2_unit=PERCENTAGE,
suggested_display_precision=0,
forecast_mode=["currently", "hourly", "daily"],
),
"pressure": PirateWeatherSensorEntityDescription(
Expand All @@ -292,6 +305,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfPressure.MBAR,
uk_unit=UnitOfPressure.MBAR,
uk2_unit=UnitOfPressure.MBAR,
suggested_display_precision=2,
forecast_mode=["currently", "hourly", "daily"],
),
"visibility": PirateWeatherSensorEntityDescription(
Expand All @@ -302,6 +316,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfLength.KILOMETERS,
uk_unit=UnitOfLength.KILOMETERS,
uk2_unit=UnitOfLength.MILES,
suggested_display_precision=2,
icon="mdi:eye",
forecast_mode=["currently", "hourly", "daily"],
),
Expand All @@ -314,6 +329,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit="DU",
uk_unit="DU",
uk2_unit="DU",
suggested_display_precision=2,
forecast_mode=["currently", "hourly", "daily"],
),
"apparent_temperature_max": PirateWeatherSensorEntityDescription(
Expand All @@ -325,6 +341,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"apparent_temperature_high": PirateWeatherSensorEntityDescription(
Expand All @@ -336,6 +353,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"apparent_temperature_min": PirateWeatherSensorEntityDescription(
Expand All @@ -347,6 +365,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"apparent_temperature_low": PirateWeatherSensorEntityDescription(
Expand All @@ -358,6 +377,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"temperature_max": PirateWeatherSensorEntityDescription(
Expand All @@ -369,6 +389,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"temperature_high": PirateWeatherSensorEntityDescription(
Expand All @@ -380,6 +401,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"temperature_min": PirateWeatherSensorEntityDescription(
Expand All @@ -391,6 +413,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"temperature_low": PirateWeatherSensorEntityDescription(
Expand All @@ -402,6 +425,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfTemperature.CELSIUS,
uk_unit=UnitOfTemperature.CELSIUS,
uk2_unit=UnitOfTemperature.CELSIUS,
suggested_display_precision=2,
forecast_mode=["daily"],
),
"precip_intensity_max": PirateWeatherSensorEntityDescription(
Expand All @@ -412,6 +436,7 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
uk_unit=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
uk2_unit=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
suggested_display_precision=2,
icon="mdi:thermometer",
forecast_mode=["daily"],
),
Expand All @@ -423,12 +448,14 @@ class PirateWeatherSensorEntityDescription(SensorEntityDescription):
ca_unit=UV_INDEX,
uk_unit=UV_INDEX,
uk2_unit=UV_INDEX,
suggested_display_precision=2,
icon="mdi:weather-sunny",
forecast_mode=["currently", "hourly", "daily"],
),
"moon_phase": PirateWeatherSensorEntityDescription(
key="moon_phase",
name="Moon Phase",
suggested_display_precision=2,
icon="mdi:weather-night",
forecast_mode=["daily"],
),
Expand Down Expand Up @@ -922,10 +949,7 @@ def get_state(self, data):
# Some state data needs to be rounded to whole values or converted to
# percentages
if self.type in ["precip_probability", "cloud_cover", "humidity"]:
if roundingVal == 0:
state = int(round(state * 100, roundingVal))
else:
state = round(state * 100, roundingVal)
state = state * 100

# Logic to convert from SI to requsested units for compatability
# Temps in F
Expand Down Expand Up @@ -1001,7 +1025,7 @@ def get_state(self, data):
if roundingVal == 0:
outState = int(round(state, roundingVal))
else:
outState = round(state, roundingVal)
outState = state

else:
outState = state
Expand Down
30 changes: 6 additions & 24 deletions custom_components/pirateweather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,21 @@ def native_temperature(self):
"""Return the temperature."""
temperature = self._weather_coordinator.data.currently().d.get("temperature")

if self.outputRound == "Yes":
return round(temperature, 0) + 0
else:
return round(temperature, 2)
return round(temperature, 2)

@property
def humidity(self):
"""Return the humidity."""
humidity = self._weather_coordinator.data.currently().d.get("humidity") * 100.0

if self.outputRound == "Yes":
return round(humidity, 0) + 0
else:
return round(humidity, 2)
return round(humidity, 2)

@property
def native_wind_speed(self):
"""Return the wind speed."""
windspeed = self._weather_coordinator.data.currently().d.get("windSpeed")

if self.outputRound == "Yes":
return round(windspeed, 0) + 0
else:
return round(windspeed, 2)
return round(windspeed, 2)

@property
def wind_bearing(self):
Expand All @@ -296,30 +287,21 @@ def ozone(self):
"""Return the ozone level."""
ozone = self._weather_coordinator.data.currently().d.get("ozone")

if self.outputRound == "Yes":
return round(ozone, 0) + 0
else:
return round(ozone, 2)
return round(ozone, 2)

@property
def native_pressure(self):
"""Return the pressure."""
pressure = self._weather_coordinator.data.currently().d.get("pressure")

if self.outputRound == "Yes":
return round(pressure, 0) + 0
else:
return round(pressure, 2)
return round(pressure, 2)

@property
def native_visibility(self):
"""Return the visibility."""
visibility = self._weather_coordinator.data.currently().d.get("visibility")

if self.outputRound == "Yes":
return round(visibility, 0) + 0
else:
return round(visibility, 2)
return round(visibility, 2)

@property
def condition(self):
Expand Down