Skip to content

Commit

Permalink
Improve unit compatibility and enable statistics (#4)
Browse files Browse the repository at this point in the history
* Native units for current weather

Switch to native unit values for temp, dew point, pressure, precip

* Fix precipitation units

* Switch to native apparent temperature

* Change to native_ units in forecasts

* Correct speed unit to METERS_PER_SECOND

Fixes incorrect wind speed calculations

* Add stateclass to battery

* Add stateclass to rewards
  • Loading branch information
phidauex authored Oct 22, 2024
1 parent bf10474 commit 48944a0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
10 changes: 9 additions & 1 deletion custom_components/weatherxm/battery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.components.sensor import SensorEntity

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)

BATTERY_LEVEL_MAP = {
"ok": 100.0,
Expand All @@ -16,6 +22,8 @@ def __init__(self, coordinator, device_id, alias, bat_state, is_active):
self._is_active = is_active
self._attr_name = f"{alias} Battery"
self._attr_unique_id = f"{alias}_battery"
self._attr_state_class = SensorDeviceClass.BATTERY
self._attr_state_class = SensorStateClass.MEASUREMENT

@property
def state(self):
Expand Down
10 changes: 9 additions & 1 deletion custom_components/weatherxm/rewards.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.components.sensor import SensorEntity

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)

class WeatherXMRewardsSensor(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator, device_id, alias, actual_reward, total_rewards):
Expand All @@ -10,6 +16,7 @@ def __init__(self, coordinator, device_id, alias, actual_reward, total_rewards):
self._total_rewards = total_rewards
self._attr_name = f"{alias} Rewards"
self._attr_unique_id = f"{alias}_rewards"
self._attr_state_class = SensorStateClass.MEASUREMENT

@property
def state(self):
Expand Down Expand Up @@ -37,6 +44,7 @@ def __init__(self, coordinator, device_id, alias, total_rewards):
self._total_rewards = total_rewards
self._attr_name = f"{alias} Total Rewards"
self._attr_unique_id = f"{alias}_total_rewards"
self._attr_state_class = SensorStateClass.TOTAL_INCREASING

@property
def state(self):
Expand Down
48 changes: 24 additions & 24 deletions custom_components/weatherxm/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class WeatherXMWeather(CoordinatorEntity, WeatherEntity):

_attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
_attr_native_pressure_unit = UnitOfPressure.HPA
_attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
_attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
_attr_supported_features = (
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
)
Expand All @@ -111,7 +111,7 @@ def __init__(self, coordinator, entity_id, device_id, alias, address, current_we
self._attr_unique_id = alias

@property
def apparent_temperature(self):
def native_apparent_temperature(self):
return self._current_weather.get("feels_like")

@property
Expand All @@ -126,31 +126,31 @@ def datetime(self):
return self._current_weather.get("timestamp")

@property
def dew_point(self):
def native_dew_point(self):
return self._current_weather.get("dew_point")

@property
def humidity(self):
return self._current_weather.get("humidity")

@property
def precipitation(self):
def native_precipitation(self):
return self._current_weather.get("precipitation")

@property
def precipitation_accumulated(self):
def native_precipitation_accumulated(self):
return self._current_weather.get("precipitation_accumulated")

@property
def pressure(self):
def native_pressure(self):
return self._current_weather.get("pressure")

@property
def solar_irradiance(self):
return self._current_weather.get("solar_irradiance")

@property
def temperature(self):
def native_temperature(self):
return self._current_weather.get("temperature")

@property
Expand All @@ -162,11 +162,11 @@ def wind_bearing(self):
return self._current_weather.get("wind_direction")

@property
def wind_gust_speed(self):
def native_wind_gust_speed(self):
return self._current_weather.get("wind_gust")

@property
def wind_speed(self):
def native_wind_speed(self):
return self._current_weather.get("wind_speed")

async def async_update(self):
Expand All @@ -189,20 +189,20 @@ def state_attributes(self):
"""Return the state attributes."""
data = super().state_attributes
data.update({
"apparent_temperature": self.apparent_temperature,
"native_apparent_temperature": self.native_apparent_temperature,
"condition": self.condition,
"datetime": self.datetime,
"dew_point": self.dew_point,
"native_dew_point": self.native_dew_point,
"humidity": self.humidity,
"precipitation": self.precipitation,
"precipitation_accumulated": self.precipitation_accumulated,
"pressure": self.pressure,
"native_precipitation": self.native_precipitation,
"native_precipitation_accumulated": self.native_precipitation_accumulated,
"native_pressure": self.native_pressure,
"solar_irradiance": self.solar_irradiance,
"temperature": self.temperature,
"native_temperature": self.native_temperature,
"uv_index": self.uv_index,
"wind_bearing": self.wind_bearing,
"wind_speed": self.wind_speed,
"wind_gust_speed": self.wind_gust_speed,
"native_wind_speed": self.native_wind_speed,
"native_wind_gust_speed": self.native_wind_gust_speed,
})
return data

Expand All @@ -220,10 +220,10 @@ def forecast_hourly(self):
for hourly in daily.get("hourly", []):
hourly_forecast = {
"datetime": hourly.get("timestamp"),
"temperature": hourly.get("temperature"),
"precipitation": hourly.get("precipitation"),
"native_temperature": hourly.get("temperature"),
"native_precipitation": hourly.get("precipitation"),
"precipitation_probability": hourly.get("precipitation_probability"),
"wind_speed": hourly.get("wind_speed"),
"native_wind_speed": hourly.get("wind_speed"),
"wind_bearing": hourly.get("wind_direction"),
"condition": ICON_TO_CONDITION_MAP.get(hourly.get("icon"), "unknown"),
}
Expand All @@ -237,11 +237,11 @@ def forecast_daily(self):
day_data = daily.get("daily", {})
daily_forecast = {
"datetime": day_data.get("timestamp"),
"temperature": day_data.get("temperature_max"),
"templow": day_data.get("temperature_min"),
"precipitation": day_data.get("precipitation_intensity"),
"native_temperature": day_data.get("temperature_max"),
"native_templow": day_data.get("temperature_min"),
"native_precipitation": day_data.get("precipitation_intensity"),
"precipitation_probability": day_data.get("precipitation_probability"),
"wind_speed": day_data.get("wind_speed"),
"native_wind_speed": day_data.get("wind_speed"),
"wind_bearing": day_data.get("wind_direction"),
"condition": ICON_TO_CONDITION_MAP.get(day_data.get("icon"), "unknown"),
}
Expand Down

0 comments on commit 48944a0

Please sign in to comment.