Files
home-assistant-core/homeassistant/components/meteo_france/sensor.py
T
Oncleben31andQuentin POLLET 6b85e23408 Refactor Météo-France to use API instead of web scraping (#37737)
* Add new python library

* Update requirements

* Remove old libs

* config flow with client.search_places

* WIP: UI config + weather OK

* WIP: sensors

* WIP: add pressure to weather + available to sensor

* WIP: coordinator next_rain + alert

* Make import step working

* migrate to meteofrance-api v0.0.3

* Create coordinator for rain only if data available in API

* Fix avoid creation of rain sensor when not available.

* Add options flow for forecast mode

* Fix import config causing bug with UI

* Add alert sensor

* Add coastal alerts when available (#5)

* Use meteofrance-api feature branch on Github

* Update unit of next_rain sensor

* Test different type of attibutes

* Typo for attribute

* Next rain sensor device class as timestamp

* Better design for rain entity attributes

* use master branch for meteofrance-api

* time displayed in the HA server timezone.

* fix bug when next_rain_date_locale is None

* Add precipitation and cloud cover sensors

* Add variable to avoid repeating computing

* Apply suggestions from code review

Co-authored-by: Quentame <polletquentin74@me.com>

* Attributes names in const.

* Cleaning

* Cleaning: use current_forecast and today_forecast

* Write state to HA after fetch

* Refactor, Log messages and bug fix. (#6)

* Add messages in log

* Refactor using 'current_forecast'.

* Use % string format with _LOGGER

* Remove inconsistent path

* Secure timestamp value and get current day forecast

* new unique_id

* Change Log message to debug

* Log messages improvement

* Don't try to create weather alert sensor if not in covered zone.

* convert wind speed in km/h

* Better list of city in config_flow

* Manage initial CONF_MODE as None

* Review correction

* Review coorections

* unique id correction

* Migrate from previous config

* Make config name detailed

* Fix weather alert sensor unload (#7)

* Unload weather alert platform

* Revert "Unload weather alert platform"

This reverts commit 95259fdee84f30a5be915eb1fbb2e19fcddc97e4.

* second try in async_unload_entry

* Make it work

* isort modification

* remove weather alert logic in sensor.py

* Refactor to avoid too long code lines

Co-authored-by: Quentin POLLET <polletquentin74@me.com>

* Update config tests to Meteo-France (#18)

* Update meteo_france exception name

* Update MeteoFranceClient name used in tests

* Update 'test_user'

* Make test_user works

* Add test test_user_list

* Make test_import works

* Quick & Dirty fix on exception managment. WIP

* allow to catch MeteoFranceClient() exceptions

* remove test_abort_if_already_setup_district

* bump meteofrance-api version

* We do not need to test Exception in flow yet

* Remove unused data

* Change client1 fixture name

* Change client2 fixture name

* Finish cities step

* Test import with multiple choice

* refactor places

* Add option flow test

Co-authored-by: Quentin POLLET <polletquentin74@me.com>

* Fix errors due to missing data in the API (#22)

* fix case where probability_forecast it not in API
* Workaround for probabilty_forecast data null value
* Fix weather alert sensor added when shouldn't
* Add a partlycloudy and cloudy value options in condition map
* Enable snow chance entity

* fix from review

* remove summary

* Other fix from PR review

* WIP: error if no results in city search

* Add test for config_flow when no result in search

* Lint fix

* generate en.json

* Update homeassistant/components/meteo_france/__init__.py

* Update homeassistant/components/meteo_france/__init__.py

* Update homeassistant/components/meteo_france/__init__.py

* Update homeassistant/components/meteo_france/sensor.py

* Update homeassistant/components/meteo_france/__init__.py

* Update homeassistant/components/meteo_france/__init__.py

* string: city input --> city field

Co-authored-by: Quentin POLLET <polletquentin74@me.com>
2020-08-01 22:56:00 +02:00

232 lines
7.3 KiB
Python

"""Support for Meteo-France raining forecast sensor."""
import logging
from meteofrance.helpers import (
get_warning_text_status_from_indice_color,
readeable_phenomenoms_dict,
)
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.util import dt as dt_util
from .const import (
ATTR_NEXT_RAIN_1_HOUR_FORECAST,
ATTRIBUTION,
COORDINATOR_ALERT,
COORDINATOR_FORECAST,
COORDINATOR_RAIN,
DOMAIN,
ENTITY_API_DATA_PATH,
ENTITY_CLASS,
ENTITY_ENABLE,
ENTITY_ICON,
ENTITY_NAME,
ENTITY_UNIT,
SENSOR_TYPES,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
) -> None:
"""Set up the Meteo-France sensor platform."""
coordinator_forecast = hass.data[DOMAIN][entry.entry_id][COORDINATOR_FORECAST]
coordinator_rain = hass.data[DOMAIN][entry.entry_id][COORDINATOR_RAIN]
coordinator_alert = hass.data[DOMAIN][entry.entry_id][COORDINATOR_ALERT]
entities = []
for sensor_type in SENSOR_TYPES:
if sensor_type == "next_rain":
if coordinator_rain:
entities.append(MeteoFranceRainSensor(sensor_type, coordinator_rain))
elif sensor_type == "weather_alert":
if coordinator_alert:
entities.append(MeteoFranceAlertSensor(sensor_type, coordinator_alert))
elif sensor_type in ["rain_chance", "freeze_chance", "snow_chance"]:
if coordinator_forecast.data.probability_forecast:
entities.append(MeteoFranceSensor(sensor_type, coordinator_forecast))
else:
_LOGGER.warning(
"Sensor %s skipped for %s as data is missing in the API",
sensor_type,
coordinator_forecast.data.position["name"],
)
else:
entities.append(MeteoFranceSensor(sensor_type, coordinator_forecast))
async_add_entities(
entities, False,
)
class MeteoFranceSensor(Entity):
"""Representation of a Meteo-France sensor."""
def __init__(self, sensor_type: str, coordinator: DataUpdateCoordinator):
"""Initialize the Meteo-France sensor."""
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}"
@property
def unique_id(self):
"""Return the unique id."""
return self._unique_id
@property
def name(self):
"""Return the name."""
return self._name
@property
def state(self):
"""Return the state."""
path = SENSOR_TYPES[self._type][ENTITY_API_DATA_PATH].split(":")
data = getattr(self.coordinator.data, path[0])
# Specific case for probability forecast
if path[0] == "probability_forecast":
if len(path) == 3:
# This is a fix compared to other entitty as first index is always null in API result for unknown reason
value = _find_first_probability_forecast_not_null(data, path)
else:
value = data[0][path[1]]
# General case
else:
if len(path) == 3:
value = data[path[1]][path[2]]
else:
value = data[path[1]]
if self._type == "wind_speed":
# convert API wind speed from m/s to km/h
value = round(value * 3.6)
return value
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return SENSOR_TYPES[self._type][ENTITY_UNIT]
@property
def icon(self):
"""Return the icon."""
return SENSOR_TYPES[self._type][ENTITY_ICON]
@property
def device_class(self):
"""Return the device class."""
return SENSOR_TYPES[self._type][ENTITY_CLASS]
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return SENSOR_TYPES[self._type][ENTITY_ENABLE]
@property
def device_state_attributes(self):
"""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."""
@property
def state(self):
"""Return the state."""
next_rain_date_locale = self.coordinator.data.next_rain_date_locale()
return (
dt_util.as_local(next_rain_date_locale) if next_rain_date_locale else None
)
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
ATTR_NEXT_RAIN_1_HOUR_FORECAST: [
{
dt_util.as_local(
self.coordinator.data.timestamp_to_locale_time(item["dt"])
).strftime("%H:%M"): item["desc"]
}
for item in self.coordinator.data.forecast
],
ATTR_ATTRIBUTION: ATTRIBUTION,
}
class MeteoFranceAlertSensor(MeteoFranceSensor):
"""Representation of a Meteo-France alert sensor."""
# 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
dept_code = self.coordinator.data.domain_id
self._name = f"{dept_code} {SENSOR_TYPES[self._type][ENTITY_NAME]}"
self._unique_id = self._name
@property
def state(self):
"""Return the state."""
return get_warning_text_status_from_indice_color(
self.coordinator.data.get_domain_max_color()
)
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
**readeable_phenomenoms_dict(self.coordinator.data.phenomenons_max_colors),
ATTR_ATTRIBUTION: ATTRIBUTION,
}
def _find_first_probability_forecast_not_null(
probability_forecast: list, path: list
) -> int:
"""Search the first not None value in the first forecast elements."""
for forecast in probability_forecast[0:3]:
if forecast[path[1]][path[2]] is not None:
return forecast[path[1]][path[2]]
# Default return value if no value founded
return None