Disable polling in buienradar weather entity (#98443)

This commit is contained in:
Erik Montnemery 2023-08-15 13:28:43 +02:00 committed by GitHub
parent a87878f723
commit 35b914af97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 57 deletions

View file

@ -753,9 +753,9 @@ class BrSensor(SensorEntity):
self._timeframe = None self._timeframe = None
@callback @callback
def data_updated(self, data): def data_updated(self, data: BrData):
"""Update data.""" """Update data."""
if self.hass and self._load_data(data): if self.hass and self._load_data(data.data):
self.async_write_ha_state() self.async_write_ha_state()
@callback @callback

View file

@ -75,7 +75,7 @@ class BrData:
# Update all devices # Update all devices
for dev in self.devices: for dev in self.devices:
dev.data_updated(self.data) dev.data_updated(self)
async def schedule_update(self, minute=1): async def schedule_update(self, minute=1):
"""Schedule an update after minute minutes.""" """Schedule an update after minute minutes."""

View file

@ -48,7 +48,7 @@ from homeassistant.const import (
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
# Reuse data and API logic from the sensor implementation # 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)} coordinates = {CONF_LATITUDE: float(latitude), CONF_LONGITUDE: float(longitude)}
# create weather data: # create weather entity:
data = BrData(hass, coordinates, DEFAULT_TIMEFRAME, None)
hass.data[DOMAIN][entry.entry_id][Platform.WEATHER] = data
# create weather device:
_LOGGER.debug("Initializing buienradar weather: coordinates %s", coordinates) _LOGGER.debug("Initializing buienradar weather: coordinates %s", coordinates)
entities = [BrWeather(config, coordinates)]
# create weather data:
data = BrData(hass, coordinates, DEFAULT_TIMEFRAME, entities)
hass.data[DOMAIN][entry.entry_id][Platform.WEATHER] = data
# create condition helper # create condition helper
if DATA_CONDITION not in hass.data[DOMAIN]: if DATA_CONDITION not in hass.data[DOMAIN]:
@ -113,7 +115,7 @@ async def async_setup_entry(
for condi in condlst: for condi in condlst:
hass.data[DOMAIN][DATA_CONDITION][condi] = cond 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: # schedule the first update in 1 minute from now:
await data.schedule_update(1) await data.schedule_update(1)
@ -127,75 +129,57 @@ class BrWeather(WeatherEntity):
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
_attr_native_visibility_unit = UnitOfLength.METERS _attr_native_visibility_unit = UnitOfLength.METERS
_attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND _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.""" """Initialize the platform with a data instance and station name."""
self._stationname = config.get(CONF_NAME, "Buienradar") self._stationname = config.get(CONF_NAME, "Buienradar")
self._attr_name = ( self._attr_name = self._stationname or f"BR {'(unknown station)'}"
self._stationname or f"BR {data.stationname or '(unknown station)'}"
)
self._data = data
self._attr_condition = None
self._attr_unique_id = "{:2.6f}{:2.6f}".format( self._attr_unique_id = "{:2.6f}{:2.6f}".format(
coordinates[CONF_LATITUDE], coordinates[CONF_LONGITUDE] coordinates[CONF_LATITUDE], coordinates[CONF_LONGITUDE]
) )
@property @callback
def attribution(self): def data_updated(self, data: BrData) -> None:
"""Return the attribution.""" """Update data."""
return self._data.attribution if not self.hass:
return
@property self._attr_attribution = data.attribution
def condition(self): 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.""" """Return the current condition."""
if ( if (
self._data data.condition
and self._data.condition and (ccode := data.condition.get(CONDCODE))
and (ccode := self._data.condition.get(CONDCODE))
and (conditions := self.hass.data[DOMAIN].get(DATA_CONDITION)) and (conditions := self.hass.data[DOMAIN].get(DATA_CONDITION))
): ):
return conditions.get(ccode) return conditions.get(ccode)
return None
@property def _calc_forecast(self, data: BrData):
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):
"""Return the forecast array.""" """Return the forecast array."""
fcdata_out = [] fcdata_out = []
cond = self.hass.data[DOMAIN][DATA_CONDITION] cond = self.hass.data[DOMAIN][DATA_CONDITION]
if not self._data.forecast: if not data.forecast:
return None return None
for data_in in self._data.forecast: for data_in in data.forecast:
# remap keys from external library to # remap keys from external library to
# keys understood by the weather component: # keys understood by the weather component:
condcode = data_in.get(CONDITION, []).get(CONDCODE) condcode = data_in.get(CONDITION, []).get(CONDCODE)