Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable polling in buienradar weather entity #98443

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/buienradar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/buienradar/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
90 changes: 37 additions & 53 deletions homeassistant/components/buienradar/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand All @@ -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)
Expand All @@ -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)
Expand Down