Skip to content

Commit

Permalink
fix: new precipitation sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
chilikla authored Nov 29, 2024
1 parent 875c6cb commit 272ed05
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
10 changes: 5 additions & 5 deletions custom_components/yerushamayim/data_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class YerushamayimData:
humidity: Dict[str, Any]
status: Dict[str, Any]
forecast: Dict[str, Any]
rain: Dict[str, Any]
precipitation: Dict[str, Any]
wind: Dict[str, Any]

class YerushamayimDataCoordinator(DataUpdateCoordinator):
Expand Down Expand Up @@ -180,7 +180,7 @@ def _extract_data(self) -> YerushamayimData:
except Exception as err:
_LOGGER.debug("Could not parse coldmeter data: %s", err)

rain_data = {}
precipitation_data = {}
wind_data = {}
if self.rest_api is not None and self.rest_api.data:
try:
Expand All @@ -198,8 +198,8 @@ def _extract_data(self) -> YerushamayimData:

_LOGGER.debug("Processed Yerushamayim REST data: %s", rest_data)

# Rain data
rain_data = {
# Precipitation data
precipitation_data = {
"precipitation": rest_data["rainrate"],
"precipitation_probability": rest_data["rainchance"]
}
Expand Down Expand Up @@ -233,6 +233,6 @@ def _extract_data(self) -> YerushamayimData:
humidity=humidity_data,
status=status_data,
forecast=forecast_data,
rain=rain_data,
precipitation=precipitation_data,
wind=wind_data
)
55 changes: 53 additions & 2 deletions custom_components/yerushamayim/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
SensorStateClass
)
from homeassistant.const import (
PERCENTAGE,
UnitOfTemperature,
UnitOfPrecipitationDepth
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -196,4 +197,54 @@ def extra_state_attributes(self):
attrs[key] = float(attrs[key])
except (ValueError, TypeError):
pass
return attrs
return attrs

class YerushamayimPrecipitationSensor(YerushamayimBaseSensor):
"""Precipitation sensor for Yerushamayim."""

sensor_type = "precipitation"

@property
def device_class(self):
"""Return the device class."""
return SensorDeviceClass.PRECIPITATION

@property
def state_class(self):
"""Return the state class."""
return SensorStateClass.MEASUREMENT

@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return UnitOfPrecipitationDepth.MILLIMETERS

@property
def native_value(self):
"""Return the forecast text."""
return float(self.coordinator.data.rain["precipitation"])

@property
def icon(self):
"""Return the icon."""
return "mdi:weather-pouring"

# @property
# def precipitation_probability(self) -> int | None:
# """Return the precipitation probability"""
# try:
# return int(self.coordinator.data.precipitation["precipitation_probability"])
# except (ValueError, KeyError, TypeError):
# return None

# @property
# def extra_state_attributes(self):
# """Return the state attributes with numeric conversions for precipitation."""
# attrs = super().extra_state_attributes
# for key in attrs:
# if key.endswith('_temp') and attrs[key] is not None:
# try:
# attrs[key] = float(attrs[key])
# except (ValueError, TypeError):
# pass
# return attrs
20 changes: 2 additions & 18 deletions custom_components/yerushamayim/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,6 @@ def condition(self) -> str | None:
return self.coordinator.data.status["forecast"]
except (KeyError, TypeError):
return None

@property
def native_precipitation(self) -> float | None:
"""Return the precipitation"""
try:
return float(self.coordinator.data.rain["precipitation"])
except (ValueError, KeyError, TypeError):
return None

@property
def precipitation_probability(self) -> int | None:
"""Return the precipitation probability"""
try:
return int(self.coordinator.data.rain["precipitation_probability"])
except (ValueError, KeyError, TypeError):
return None

@property
def native_wind_speed(self) -> int | None:
Expand All @@ -127,8 +111,8 @@ async def async_forecast_daily(self) -> list[Forecast] | None:
datetime=datetime.now().isoformat(),
condition=self.coordinator.data.status.get("condition"),
native_temperature=float(self.coordinator.data.temperature["temperature"]),
precipitation=float(self.coordinator.data.rain["precipitation"]),
precipitation_probability=int(self.coordinator.data.rain["precipitation_probability"])
precipitation=float(self.coordinator.data.precipitation["precipitation"]),
precipitation_probability=int(self.coordinator.data.precipitation["precipitation_probability"])
)]

_LOGGER.debug("Yerushamayim forecast: %s", forecast)
Expand Down

0 comments on commit 272ed05

Please sign in to comment.