From 3c70c9811ca57798727886317d8021da80dfc5d5 Mon Sep 17 00:00:00 2001 From: chilikla Date: Wed, 23 Oct 2024 03:28:37 +0300 Subject: [PATCH] fix: weather entity --- custom_components/yerushamayim/__init__.py | 2 +- custom_components/yerushamayim/weather.py | 45 +++++++++++++++------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/custom_components/yerushamayim/__init__.py b/custom_components/yerushamayim/__init__.py index b3bf6f4..15b9927 100644 --- a/custom_components/yerushamayim/__init__.py +++ b/custom_components/yerushamayim/__init__.py @@ -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.""" diff --git a/custom_components/yerushamayim/weather.py b/custom_components/yerushamayim/weather.py index 956e3c5..7930e2a 100644 --- a/custom_components/yerushamayim/weather.py +++ b/custom_components/yerushamayim/weather.py @@ -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"] \ No newline at end of file + def condition(self) -> str | None: + """Return the weather condition.""" + try: + return self.coordinator.data.status["condition"] + except (KeyError, TypeError): + return None \ No newline at end of file