From 4c13acd934e5b4ae95851cc3ecfd6d17b9187cd3 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 15 Aug 2023 12:52:56 +0200 Subject: [PATCH] Disable polling in buienradar weather entity --- homeassistant/components/buienradar/sensor.py | 4 +- homeassistant/components/buienradar/util.py | 2 +- .../components/buienradar/weather.py | 90 ++++++++----------- 3 files changed, 40 insertions(+), 56 deletions(-) diff --git a/homeassistant/components/buienradar/sensor.py b/homeassistant/components/buienradar/sensor.py index e52000edf7f168..00740eb4801c67 100644 --- a/homeassistant/components/buienradar/sensor.py +++ b/homeassistant/components/buienradar/sensor.py @@ -753,9 +753,9 @@ def __init__( self._timeframe = None @callback - def data_updated(self, data): + def data_updated(self, data: BrData): """Update data.""" - if self.hass and self._load_data(data): + if self.hass and self._load_data(data.data): self.async_write_ha_state() @callback diff --git a/homeassistant/components/buienradar/util.py b/homeassistant/components/buienradar/util.py index 8fce65c06002fc..9d0c2a575c926c 100644 --- a/homeassistant/components/buienradar/util.py +++ b/homeassistant/components/buienradar/util.py @@ -75,7 +75,7 @@ async def update_devices(self): # Update all devices for dev in self.devices: - dev.data_updated(self.data) + dev.data_updated(self) async def schedule_update(self, minute=1): """Schedule an update after minute minutes.""" diff --git a/homeassistant/components/buienradar/weather.py b/homeassistant/components/buienradar/weather.py index c2a276eed1ca25..aedfcf82aea73f 100644 --- a/homeassistant/components/buienradar/weather.py +++ b/homeassistant/components/buienradar/weather.py @@ -48,7 +48,7 @@ UnitOfSpeed, UnitOfTemperature, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback # Reuse data and API logic from the sensor implementation @@ -99,11 +99,13 @@ async def async_setup_entry( coordinates = {CONF_LATITUDE: float(latitude), CONF_LONGITUDE: float(longitude)} + # create weather entity: + _LOGGER.debug("Initializing buienradar weather: coordinates %s", coordinates) + entities = [BrWeather(config, coordinates)] + # create weather data: - data = BrData(hass, coordinates, DEFAULT_TIMEFRAME, None) + data = BrData(hass, coordinates, DEFAULT_TIMEFRAME, entities) hass.data[DOMAIN][entry.entry_id][Platform.WEATHER] = data - # create weather device: - _LOGGER.debug("Initializing buienradar weather: coordinates %s", coordinates) # create condition helper if DATA_CONDITION not in hass.data[DOMAIN]: @@ -113,7 +115,7 @@ async def async_setup_entry( for condi in condlst: hass.data[DOMAIN][DATA_CONDITION][condi] = cond - async_add_entities([BrWeather(data, config, coordinates)]) + async_add_entities(entities) # schedule the first update in 1 minute from now: await data.schedule_update(1) @@ -127,75 +129,57 @@ class BrWeather(WeatherEntity): _attr_native_temperature_unit = UnitOfTemperature.CELSIUS _attr_native_visibility_unit = UnitOfLength.METERS _attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND + _attr_should_poll = False - def __init__(self, data, config, coordinates): + def __init__(self, config, coordinates): """Initialize the platform with a data instance and station name.""" self._stationname = config.get(CONF_NAME, "Buienradar") - self._attr_name = ( - self._stationname or f"BR {data.stationname or '(unknown station)'}" - ) - self._data = data + self._attr_name = self._stationname or f"BR {'(unknown station)'}" + self._attr_condition = None self._attr_unique_id = "{:2.6f}{:2.6f}".format( coordinates[CONF_LATITUDE], coordinates[CONF_LONGITUDE] ) - @property - def attribution(self): - """Return the attribution.""" - return self._data.attribution + @callback + def data_updated(self, data: BrData) -> None: + """Update data.""" + if not self.hass: + return - @property - def condition(self): + self._attr_attribution = data.attribution + self._attr_condition = self._calc_condition(data) + self._attr_forecast = self._calc_forecast(data) + self._attr_humidity = data.humidity + self._attr_name = ( + self._stationname or f"BR {data.stationname or '(unknown station)'}" + ) + self._attr_native_pressure = data.pressure + self._attr_native_temperature = data.temperature + self._attr_native_visibility = data.visibility + self._attr_native_wind_speed = data.wind_speed + self._attr_wind_bearing = data.wind_bearing + self.async_write_ha_state() + + def _calc_condition(self, data: BrData): """Return the current condition.""" if ( - self._data - and self._data.condition - and (ccode := self._data.condition.get(CONDCODE)) + data.condition + and (ccode := data.condition.get(CONDCODE)) and (conditions := self.hass.data[DOMAIN].get(DATA_CONDITION)) ): return conditions.get(ccode) + return None - @property - def native_temperature(self): - """Return the current temperature.""" - return self._data.temperature - - @property - def native_pressure(self): - """Return the current pressure.""" - return self._data.pressure - - @property - def humidity(self): - """Return the name of the sensor.""" - return self._data.humidity - - @property - def native_visibility(self): - """Return the current visibility in m.""" - return self._data.visibility - - @property - def native_wind_speed(self): - """Return the current windspeed in m/s.""" - return self._data.wind_speed - - @property - def wind_bearing(self): - """Return the current wind bearing (degrees).""" - return self._data.wind_bearing - - @property - def forecast(self): + def _calc_forecast(self, data: BrData): """Return the forecast array.""" fcdata_out = [] cond = self.hass.data[DOMAIN][DATA_CONDITION] - if not self._data.forecast: + if not data.forecast: return None - for data_in in self._data.forecast: + for data_in in data.forecast: # remap keys from external library to # keys understood by the weather component: condcode = data_in.get(CONDITION, []).get(CONDCODE)