From 0a952f388656bc1d1a4f379098afd0cb6508936e Mon Sep 17 00:00:00 2001 From: Fischelsberger <13201817+Fischelsberger@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:21:53 +0200 Subject: [PATCH 1/3] fix for 2024.4 --- custom_components/car_wash/binary_sensor.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/custom_components/car_wash/binary_sensor.py b/custom_components/car_wash/binary_sensor.py index 8129d17..4ce0c60 100644 --- a/custom_components/car_wash/binary_sensor.py +++ b/custom_components/car_wash/binary_sensor.py @@ -16,7 +16,6 @@ from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.components.weather import ( - ATTR_FORECAST, ATTR_FORECAST_CONDITION, ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_TEMP, @@ -38,6 +37,9 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import TemperatureConverter +from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN +from homeassistant.components.weather import SERVICE_GET_FORECASTS + from .const import ( BAD_CONDITIONS, CONF_DAYS, @@ -141,7 +143,9 @@ def sensor_startup(event): def _temp2c(temperature: Optional[float], temperature_unit: str) -> Optional[float]: """Convert weather temperature to Celsius degree.""" if temperature is not None and temperature_unit != UnitOfTemperature.CELSIUS: - temperature = TemperatureConverter.convert(temperature, temperature_unit, UnitOfTemperature.CELSIUS) + temperature = TemperatureConverter.convert( + temperature, temperature_unit, UnitOfTemperature.CELSIUS + ) return temperature # pylint: disable=too-many-branches,too-many-statements @@ -157,7 +161,15 @@ async def async_update(self): tmpu = self.hass.config.units.temperature_unit temp = wdata.attributes.get(ATTR_WEATHER_TEMPERATURE) cond = wdata.state - forecast = wdata.attributes.get(ATTR_FORECAST) + + daily_response = await self.hass.services.async_call( + WEATHER_DOMAIN, + SERVICE_GET_FORECASTS, + {"entity_id": self._weather_entity, "type": "daily"}, + blocking=True, + return_response=True, + ) + forecast = daily_response[self._weather_entity]["forecast"] if forecast is None: self._attr_is_on = None @@ -185,7 +197,7 @@ async def async_update(self): fc_date = fcast.get(ATTR_FORECAST_TIME) if isinstance(fc_date, int): fc_date = dt_util.as_local( - datetime.utcfromtimestamp(fc_date / 1000) + datetime.datetime.fromtimestamp(fc_date, tz=datetime.timezone.utc) ).isoformat() elif isinstance(fc_date, datetime): fc_date = dt_util.as_local(fc_date).isoformat() From 332285144e8a3e488048e4ef1597c3823a7dd95a Mon Sep 17 00:00:00 2001 From: Fischelsberger <13201817+Fischelsberger@users.noreply.github.com> Date: Thu, 18 Apr 2024 21:52:21 +0200 Subject: [PATCH 2/3] fixed minor typos, added check of presence --- custom_components/car_wash/binary_sensor.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/custom_components/car_wash/binary_sensor.py b/custom_components/car_wash/binary_sensor.py index 4ce0c60..b739ff1 100644 --- a/custom_components/car_wash/binary_sensor.py +++ b/custom_components/car_wash/binary_sensor.py @@ -22,6 +22,8 @@ ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME, ATTR_WEATHER_TEMPERATURE, + DOMAIN as WEATHER_DOMAIN, + SERVICE_GET_FORECASTS, ) from homeassistant.const import ( CONF_NAME, @@ -37,9 +39,6 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import TemperatureConverter -from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN -from homeassistant.components.weather import SERVICE_GET_FORECASTS - from .const import ( BAD_CONDITIONS, CONF_DAYS, @@ -169,7 +168,15 @@ async def async_update(self): blocking=True, return_response=True, ) - forecast = daily_response[self._weather_entity]["forecast"] + if not (isinstance(daily_response, dict)): + self._attr_is_on = None + raise HomeAssistantError("daily_response is empty.") + if (len(daily_response) == 0) or ( + "forecast" in daily_response[self._weather_entity] + ): + forecast = daily_response[self._weather_entity]["forecast"] + else: + forecast = None if forecast is None: self._attr_is_on = None @@ -197,7 +204,7 @@ async def async_update(self): fc_date = fcast.get(ATTR_FORECAST_TIME) if isinstance(fc_date, int): fc_date = dt_util.as_local( - datetime.datetime.fromtimestamp(fc_date, tz=datetime.timezone.utc) + datetime.fromtimestamp(fc_date, tz=datetime.timezone.utc) ).isoformat() elif isinstance(fc_date, datetime): fc_date = dt_util.as_local(fc_date).isoformat() From ad8df013e270ae2dbec193121b819f1c3ea55307 Mon Sep 17 00:00:00 2001 From: Fischelsberger <13201817+Fischelsberger@users.noreply.github.com> Date: Sat, 20 Apr 2024 21:54:13 +0200 Subject: [PATCH 3/3] optimized Error handeling and message --- custom_components/car_wash/binary_sensor.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/custom_components/car_wash/binary_sensor.py b/custom_components/car_wash/binary_sensor.py index b739ff1..e05c20a 100644 --- a/custom_components/car_wash/binary_sensor.py +++ b/custom_components/car_wash/binary_sensor.py @@ -168,12 +168,9 @@ async def async_update(self): blocking=True, return_response=True, ) - if not (isinstance(daily_response, dict)): - self._attr_is_on = None - raise HomeAssistantError("daily_response is empty.") - if (len(daily_response) == 0) or ( - "forecast" in daily_response[self._weather_entity] - ): + if (not (isinstance(daily_response, dict))) or (len(daily_response) == 0): + forecast = None + elif "forecast" in daily_response[self._weather_entity]: forecast = daily_response[self._weather_entity]["forecast"] else: forecast = None