Skip to content

Commit

Permalink
Replace rounding with suggested_display_precision
Browse files Browse the repository at this point in the history
  • Loading branch information
cloneofghosts committed Feb 4, 2024
1 parent 78967e6 commit d8d69dd
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 83 deletions.
3 changes: 0 additions & 3 deletions custom_components/pirateweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
CONF_UNITS,
PW_PLATFORMS,
PW_PLATFORM,
PW_ROUND,
)

# from .weather_update_coordinator import WeatherUpdateCoordinator, DarkSkyData
Expand All @@ -55,7 +54,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
forecast_days = _get_config_value(entry, CONF_FORECAST)
forecast_hours = _get_config_value(entry, CONF_HOURLY_FORECAST)
pw_entity_platform = _get_config_value(entry, PW_PLATFORM)
pw_entity_rounding = _get_config_value(entry, PW_ROUND)
pw_scan_Int = entry.data[CONF_SCAN_INTERVAL]

# Extract list of int from forecast days/ hours string if present
Expand Down Expand Up @@ -108,7 +106,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
CONF_FORECAST: forecast_days,
CONF_HOURLY_FORECAST: forecast_hours,
PW_PLATFORM: pw_entity_platform,
PW_ROUND: pw_entity_rounding,
CONF_SCAN_INTERVAL: pw_scan_Int,
}

Expand Down
11 changes: 0 additions & 11 deletions custom_components/pirateweather/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
PW_PLATFORMS,
PW_PLATFORM,
PW_PREVPLATFORM,
PW_ROUND,
)

ATTRIBUTION = "Powered by Pirate Weather"
Expand Down Expand Up @@ -84,7 +83,6 @@ async def async_step_user(self, user_input=None):
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): cv.multi_select(
ALL_CONDITIONS
),
vol.Optional(PW_ROUND, default="No"): vol.In(["Yes", "No"]),
vol.Optional(CONF_UNITS, default=DEFAULT_UNITS): vol.In(
["si", "us", "ca", "uk"]
),
Expand Down Expand Up @@ -172,8 +170,6 @@ async def async_step_import(self, import_input=None):
config[PW_PLATFORM] = None
if PW_PREVPLATFORM not in config:
config[PW_PREVPLATFORM] = None
if PW_ROUND not in config:
config[PW_ROUND] = "No"
if CONF_SCAN_INTERVAL not in config:
config[CONF_SCAN_INTERVAL] = DEFAULT_SCAN_INTERVAL
return await self.async_step_user(config)
Expand Down Expand Up @@ -282,13 +278,6 @@ async def async_step_init(self, user_input=None):
self.config_entry.data.get(CONF_UNITS, DEFAULT_UNITS),
),
): vol.In(["si", "us", "ca", "uk"]),
vol.Optional(
PW_ROUND,
default=self.config_entry.options.get(
PW_ROUND,
self.config_entry.data.get(PW_ROUND, "No"),
),
): vol.In(["Yes", "No"]),
}
),
)
Expand Down
1 change: 0 additions & 1 deletion custom_components/pirateweather/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
PW_PLATFORMS = ["Sensor", "Weather"]
PW_PLATFORM = "pw_platform"
PW_PREVPLATFORM = "pw_prevplatform"
PW_ROUND = "pw_round"

ATTR_FORECAST_CLOUD_COVERAGE = "cloud_coverage"
ATTR_FORECAST_HUMIDITY = "humidity"
Expand Down
54 changes: 26 additions & 28 deletions custom_components/pirateweather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
PW_PLATFORMS,
PW_PLATFORM,
PW_PREVPLATFORM,
PW_ROUND,
)


Expand Down Expand Up @@ -162,6 +161,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 +173,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 +186,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 +200,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 +213,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 +226,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 +238,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 +250,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 +263,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 +275,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 +289,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 +302,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 +313,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 +326,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 +338,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 +350,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 +362,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 +374,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 +386,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 +398,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 +410,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 +422,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 +433,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,13 +445,15 @@ 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",
icon="mdi:weather-night",
suggested_display_precision=2,
forecast_mode=["daily"],
),
"sunrise_time": PirateWeatherSensorEntityDescription(
Expand Down Expand Up @@ -613,9 +637,6 @@ async def async_setup_platform(
# Define as a sensor platform
config_entry[PW_PLATFORM] = [PW_PLATFORMS[0]]

# Set as no rounding for compatability
config_entry[PW_ROUND] = "No"

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config_entry
Expand All @@ -638,9 +659,6 @@ async def async_setup_entry(
forecast_days = domain_data[CONF_FORECAST]
forecast_hours = domain_data[CONF_HOURLY_FORECAST]

# Round Output
outputRound = domain_data[PW_ROUND]

sensors: list[PirateWeatherSensor] = []

for condition in conditions:
Expand All @@ -667,7 +685,6 @@ async def async_setup_entry(
forecast_hour=None,
description=sensorDescription,
requestUnits=requestUnits,
outputRound=outputRound,
)
)

Expand All @@ -686,7 +703,6 @@ async def async_setup_entry(
forecast_hour=None,
description=sensorDescription,
requestUnits=requestUnits,
outputRound=outputRound,
)
)

Expand All @@ -705,7 +721,6 @@ async def async_setup_entry(
forecast_hour=int(forecast_h),
description=sensorDescription,
requestUnits=requestUnits,
outputRound=outputRound,
)
)

Expand All @@ -729,7 +744,6 @@ def __init__(
forecast_hour: int,
description: PirateWeatherSensorEntityDescription,
requestUnits: str,
outputRound: str,
) -> None:
"""Initialize the sensor."""
self.client_name = name
Expand All @@ -753,7 +767,6 @@ def __init__(
self.forecast_day = forecast_day
self.forecast_hour = forecast_hour
self.requestUnits = requestUnits
self.outputRound = outputRound
self.type = condition
self._icon = None
self._alerts = None
Expand Down Expand Up @@ -913,19 +926,10 @@ def get_state(self, data):
if "summary" in self.type:
self._icon = getattr(data, "icon", "")

# If output rounding is requested, round to nearest integer
if self.outputRound == "Yes":
roundingVal = 0
else:
roundingVal = 1

# 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 @@ -998,12 +1002,6 @@ def get_state(self, data):
"wind_speed",
"wind_gust",
]:
if roundingVal == 0:
outState = int(round(state, roundingVal))
else:
outState = round(state, roundingVal)

else:
outState = state

return outState
Expand Down
1 change: 0 additions & 1 deletion custom_components/pirateweather/translations/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"hourly_forecast": "Hodinové předpovědi senzorů ve formátu csv od 0 do 168 (např. '0,1,10'). Používá se pouze pokud jsou požadovány senzory.",
"monitored_conditions": "Monitorované podmínky pro vytváření senzorů. Používá se pouze pokud jsou požadovány senzory.",
"pw_platform": "Entita počasí a/nebo entita senzoru. Senzor vytvoří entity pro každou podmínku v každém čase. Pokud si nejste jisti, vyberte pouze Počasí!",
"pw_round": "Zaokrouhlit hodnoty na nejbližší celé číslo. Ujistěte se, že vybrané jednotky odpovídají systémovým jednotkám.",
"scan_interval": "Sekundy čekání mezi aktualizacemi. Snížení této hodnoty pod 900 sekund (15 minut) se nedoporučuje."
},
"description": "Nastavte integraci Pirate Weather. Pro generování API klíče jděte na https://pirateweather.net",
Expand Down
1 change: 0 additions & 1 deletion custom_components/pirateweather/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"hourly_forecast": "Hourly forecast sensors in csv form from 0-168 (ex. '0,1,10'). Only used if sensors are requested.",
"monitored_conditions": "Monitored conditions to create sensors for. Only used if sensors are requested.",
"pw_platform": "Weather Entity and/or Sensor Entity. Sensor will create entities for each condition at each time. If unsure, only select Weather!",
"pw_round": "Round values to the nearest integer. Ensure that the selected units match the system units.",
"scan_interval": "Seconds to wait between updates. Reducing this below 900 seconds (15 minutes) is not recomended."
},
"description": "Set up Pirate Weather integration. To generate API key go to https://pirateweather.net",
Expand Down
1 change: 0 additions & 1 deletion custom_components/pirateweather/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"hourly_forecast": "Uurlijkse prognoses sensoren in csv-vorm van 0-168 (bijv. '0,1,10'). Wordt alleen gebruikt als sensoren worden aangeroepen.",
"monitored_conditions": "Gecontroleerde omstandigheden om sensoren voor te creëren. Wordt alleen gebruikt als sensoren worden aangeroepen.",
"pw_platform": "Weer Entiteit en/of Sensor Entiteit. Sensor maakt op elk moment entiteiten voor elke voorwaarde. Als u het niet zeker weet, selecteert u alleen Weer!",
"pw_round": "Rond waarden af ​​op het dichtstbijzijnde gehele getal. Zorg ervoor dat de geselecteerde eenheden overeenkomen met de systeemeenheden.",
"scan_interval": "Seconden wachten tussen updates. Het wordt niet aanbevolen om dit onder de 900 seconden (15 minuten) te zetten."
},
"description": "Stel Pirate Weather-integratie in. Ga naar https://pirateweather.net om een ​​API-sleutel te genereren.",
Expand Down
Loading

0 comments on commit d8d69dd

Please sign in to comment.