Update meteo_france to use CoordinatorEntity (#39432)

* Update meteo_france to use CoordinatorEntity

* Update homeassistant/components/meteo_france/sensor.py

* Update homeassistant/components/meteo_france/weather.py

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
springstan 2020-08-30 19:52:53 +02:00 committed by GitHub
parent bd7682a694
commit 953626b2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 55 deletions

View File

@ -8,9 +8,11 @@ from meteofrance.helpers import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util import dt as dt_util
from .const import (
@ -70,13 +72,13 @@ async def async_setup_entry(
)
class MeteoFranceSensor(Entity):
class MeteoFranceSensor(CoordinatorEntity):
"""Representation of a Meteo-France sensor."""
def __init__(self, sensor_type: str, coordinator: DataUpdateCoordinator):
"""Initialize the Meteo-France sensor."""
super().__init__(coordinator)
self._type = sensor_type
self.coordinator = coordinator
city_name = self.coordinator.data.position["name"]
self._name = f"{city_name} {SENSOR_TYPES[self._type][ENTITY_NAME]}"
self._unique_id = f"{self.coordinator.data.position['lat']},{self.coordinator.data.position['lon']}_{self._type}"
@ -142,29 +144,6 @@ class MeteoFranceSensor(Entity):
"""Return the state attributes."""
return {ATTR_ATTRIBUTION: ATTRIBUTION}
@property
def available(self):
"""Return if state is available."""
return self.coordinator.last_update_success
@property
def should_poll(self) -> bool:
"""No polling needed."""
return False
async def async_update(self):
"""Only used by the generic entity update service."""
if not self.enabled:
return
await self.coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""Subscribe to updates."""
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)
class MeteoFranceRainSensor(MeteoFranceSensor):
"""Representation of a Meteo-France rain sensor."""
@ -203,8 +182,7 @@ class MeteoFranceAlertSensor(MeteoFranceSensor):
# pylint: disable=super-init-not-called
def __init__(self, sensor_type: str, coordinator: DataUpdateCoordinator):
"""Initialize the Meteo-France sensor."""
self._type = sensor_type
self.coordinator = coordinator
super().__init__(sensor_type, coordinator)
dept_code = self.coordinator.data.domain_id
self._name = f"{dept_code} {SENSOR_TYPES[self._type][ENTITY_NAME]}"
self._unique_id = self._name

View File

@ -15,7 +15,10 @@ from homeassistant.components.weather import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MODE, TEMP_CELSIUS
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util import dt as dt_util
from .const import (
@ -60,12 +63,12 @@ async def async_setup_entry(
)
class MeteoFranceWeather(WeatherEntity):
class MeteoFranceWeather(CoordinatorEntity, WeatherEntity):
"""Representation of a weather condition."""
def __init__(self, coordinator: DataUpdateCoordinator, mode: str):
"""Initialise the platform with a data instance and station name."""
self.coordinator = coordinator
super().__init__(coordinator)
self._city_name = self.coordinator.data.position["name"]
self._mode = mode
self._unique_id = f"{self.coordinator.data.position['lat']},{self.coordinator.data.position['lon']}"
@ -171,26 +174,3 @@ class MeteoFranceWeather(WeatherEntity):
def attribution(self):
"""Return the attribution."""
return ATTRIBUTION
@property
def available(self):
"""Return if state is available."""
return self.coordinator.last_update_success
@property
def should_poll(self) -> bool:
"""No polling needed."""
return False
async def async_update(self):
"""Only used by the generic entity update service."""
if not self.enabled:
return
await self.coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""Subscribe to updates."""
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)