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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update met to use CoordinatorEntity #39462

Merged
merged 1 commit into from Aug 30, 2020
Merged
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
36 changes: 11 additions & 25 deletions homeassistant/components/met/weather.py
Expand Up @@ -17,6 +17,7 @@
TEMP_CELSIUS,
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.distance import convert as convert_distance
from homeassistant.util.pressure import convert as convert_pressure

Expand Down Expand Up @@ -77,37 +78,22 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
)


class MetWeather(WeatherEntity):
class MetWeather(CoordinatorEntity, WeatherEntity):
"""Implementation of a Met.no weather condition."""

def __init__(self, coordinator, config, is_metric, hourly):
"""Initialise the platform with a data instance and site."""
super().__init__(coordinator)
self._config = config
self._coordinator = coordinator
self._is_metric = is_metric
self._hourly = hourly
self._name_appendix = "-hourly" if hourly else ""

async def async_added_to_hass(self):
"""Start fetching data."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)

async def async_update(self):
"""Only used by the generic entity update service."""
await self._coordinator.async_request_refresh()

@property
def track_home(self):
"""Return if we are tracking home."""
return self._config.get(CONF_TRACK_HOME, False)

@property
def should_poll(self):
"""No polling needed."""
return False

@property
def unique_id(self):
"""Return unique ID."""
Expand All @@ -132,12 +118,12 @@ def name(self):
@property
def condition(self):
"""Return the current condition."""
return self._coordinator.data.current_weather_data.get("condition")
return self.coordinator.data.current_weather_data.get("condition")

@property
def temperature(self):
"""Return the temperature."""
return self._coordinator.data.current_weather_data.get("temperature")
return self.coordinator.data.current_weather_data.get("temperature")

@property
def temperature_unit(self):
Expand All @@ -147,7 +133,7 @@ def temperature_unit(self):
@property
def pressure(self):
"""Return the pressure."""
pressure_hpa = self._coordinator.data.current_weather_data.get("pressure")
pressure_hpa = self.coordinator.data.current_weather_data.get("pressure")
if self._is_metric or pressure_hpa is None:
return pressure_hpa

Expand All @@ -156,12 +142,12 @@ def pressure(self):
@property
def humidity(self):
"""Return the humidity."""
return self._coordinator.data.current_weather_data.get("humidity")
return self.coordinator.data.current_weather_data.get("humidity")

@property
def wind_speed(self):
"""Return the wind speed."""
speed_m_s = self._coordinator.data.current_weather_data.get("wind_speed")
speed_m_s = self.coordinator.data.current_weather_data.get("wind_speed")
if self._is_metric or speed_m_s is None:
return speed_m_s

Expand All @@ -172,7 +158,7 @@ def wind_speed(self):
@property
def wind_bearing(self):
"""Return the wind direction."""
return self._coordinator.data.current_weather_data.get("wind_bearing")
return self.coordinator.data.current_weather_data.get("wind_bearing")

@property
def attribution(self):
Expand All @@ -183,5 +169,5 @@ def attribution(self):
def forecast(self):
"""Return the forecast array."""
if self._hourly:
return self._coordinator.data.hourly_forecast
return self._coordinator.data.daily_forecast
return self.coordinator.data.hourly_forecast
return self.coordinator.data.daily_forecast