Skip to content

Commit

Permalink
fix: weather entity
Browse files Browse the repository at this point in the history
  • Loading branch information
chilikla authored Oct 23, 2024
1 parent 918dfe7 commit 3c70c98
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion custom_components/yerushamayim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .const import DOMAIN
from .data_coordinator import YerushamayimDataCoordinator

PLATFORMS: list[Platform] = [Platform.SENSOR]
PLATFORMS: list[Platform] = [Platform.WEATHER, Platform.SENSOR]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Yerushamayim from a config entry."""
Expand Down
45 changes: 32 additions & 13 deletions custom_components/yerushamayim/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,50 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
coordinator = hass.data[DOMAIN]
"""Set up the Yerushamayim weather platform."""
coordinator = YerushamayimDataCoordinator(hass)
await coordinator.async_config_entry_first_refresh()

async_add_entities([YerushamayimWeather(coordinator)], True)

class YerushamayimWeather(CoordinatorEntity, WeatherEntity):
"""Weather entity for Yerushamayim."""

def __init__(self, coordinator: YerushamayimDataCoordinator):
"""Initialize the weather entity."""
super().__init__(coordinator)
self._attr_unique_id = f"{DOMAIN}_weather"
self._attr_name = "Yerushamayim Weather"
self._attr_native_temperature_unit = UnitOfTemperature.CELSIUS

@property
def native_temperature(self) -> float:
return float(self.coordinator.data.temperature["temperature"])

@property
def native_apparent_temperature(self) -> float:
return float(self.coordinator.data.temperature["apparent_temperature"])
def native_temperature(self) -> float | None:
"""Return the platform temperature."""
try:
return float(self.coordinator.data.temperature["temperature"])
except (ValueError, KeyError, TypeError):
return None

@property
def native_temperature_unit(self) -> str:
return UnitOfTemperature.CELSIUS
def native_apparent_temperature(self) -> float | None:
"""Return the apparent temperature."""
try:
return float(self.coordinator.data.temperature["apparent_temperature"])
except (ValueError, KeyError, TypeError):
return None

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

@property
def condition(self) -> str:
return self.coordinator.data.status["forecast"]
def condition(self) -> str | None:
"""Return the weather condition."""
try:
return self.coordinator.data.status["condition"]
except (KeyError, TypeError):
return None

0 comments on commit 3c70c98

Please sign in to comment.