2019-02-14 15:42:03 +00:00
|
|
|
"""Support for UK Met Office weather service."""
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
import logging
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
from homeassistant.components.weather import WeatherEntity
|
|
|
|
from homeassistant.const import LENGTH_KILOMETERS, TEMP_CELSIUS
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTRIBUTION,
|
|
|
|
CONDITION_CLASSES,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
DOMAIN,
|
|
|
|
METOFFICE_COORDINATOR,
|
|
|
|
METOFFICE_DATA,
|
|
|
|
METOFFICE_NAME,
|
|
|
|
VISIBILITY_CLASSES,
|
|
|
|
VISIBILITY_DISTANCE_CLASSES,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-03-31 20:03:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType, entry: ConfigType, async_add_entities
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Met Office weather sensor platform."""
|
|
|
|
hass_data = hass.data[DOMAIN][entry.entry_id]
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
async_add_entities(
|
2020-08-27 11:56:20 +00:00
|
|
|
[
|
|
|
|
MetOfficeWeather(
|
|
|
|
entry.data,
|
|
|
|
hass_data,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
False,
|
2020-06-15 10:02:25 +00:00
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MetOfficeWeather(WeatherEntity):
|
|
|
|
"""Implementation of a Met Office weather condition."""
|
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
def __init__(self, entry_data, hass_data):
|
|
|
|
"""Initialise the platform with a data instance."""
|
|
|
|
self._data = hass_data[METOFFICE_DATA]
|
|
|
|
self._coordinator = hass_data[METOFFICE_COORDINATOR]
|
|
|
|
|
|
|
|
self._name = f"{DEFAULT_NAME} {hass_data[METOFFICE_NAME]}"
|
|
|
|
self._unique_id = f"{self._data.latitude}_{self._data.longitude}"
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
self.metoffice_now = None
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique of the sensor."""
|
|
|
|
return self._unique_id
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def condition(self):
|
|
|
|
"""Return the current condition."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return (
|
|
|
|
[
|
|
|
|
k
|
|
|
|
for k, v in CONDITION_CLASSES.items()
|
|
|
|
if self.metoffice_now.weather.value in v
|
|
|
|
][0]
|
|
|
|
if self.metoffice_now
|
|
|
|
else None
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature(self):
|
|
|
|
"""Return the platform temperature."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return (
|
|
|
|
self.metoffice_now.temperature.value
|
|
|
|
if self.metoffice_now and self.metoffice_now.temperature
|
|
|
|
else None
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature_unit(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return TEMP_CELSIUS
|
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
@property
|
|
|
|
def visibility(self):
|
|
|
|
"""Return the platform visibility."""
|
|
|
|
_visibility = None
|
|
|
|
if hasattr(self.metoffice_now, "visibility"):
|
|
|
|
_visibility = f"{VISIBILITY_CLASSES.get(self.metoffice_now.visibility.value)} - {VISIBILITY_DISTANCE_CLASSES.get(self.metoffice_now.visibility.value)}"
|
|
|
|
return _visibility
|
|
|
|
|
|
|
|
@property
|
|
|
|
def visibility_unit(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return LENGTH_KILOMETERS
|
|
|
|
|
2017-03-31 20:03:27 +00:00
|
|
|
@property
|
|
|
|
def pressure(self):
|
|
|
|
"""Return the mean sea-level pressure."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return (
|
|
|
|
self.metoffice_now.pressure.value
|
|
|
|
if self.metoffice_now and self.metoffice_now.pressure
|
|
|
|
else None
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def humidity(self):
|
|
|
|
"""Return the relative humidity."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return (
|
|
|
|
self.metoffice_now.humidity.value
|
|
|
|
if self.metoffice_now and self.metoffice_now.humidity
|
|
|
|
else None
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_speed(self):
|
|
|
|
"""Return the wind speed."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return (
|
|
|
|
self.metoffice_now.wind_speed.value
|
|
|
|
if self.metoffice_now and self.metoffice_now.wind_speed
|
|
|
|
else None
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_bearing(self):
|
|
|
|
"""Return the wind bearing."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return (
|
|
|
|
self.metoffice_now.wind_direction.value
|
|
|
|
if self.metoffice_now and self.metoffice_now.wind_direction
|
|
|
|
else None
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
2019-02-14 21:09:22 +00:00
|
|
|
return ATTRIBUTION
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Set up a listener and load data."""
|
|
|
|
self.async_on_remove(
|
|
|
|
self._coordinator.async_add_listener(self._update_callback)
|
|
|
|
)
|
|
|
|
self._update_callback()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self) -> None:
|
|
|
|
"""Load data from integration."""
|
|
|
|
self.metoffice_now = self._data.now
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
|
|
|
"""Entities do not individually poll."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if state is available."""
|
|
|
|
return self.metoffice_now is not None
|