Move weather.ipma into a component (#20706)
* initial version * works * lint * move * hound * fix formatting * update * add extra features * houmd * docstring * fix tests * lint * update requirements_all.txt * new tests * lint * update CODEOWNERS * MockDependency pyipma * hound * bump pyipma version * add config_flow tests * lint * improve test coverage * fix test * address comments by @MartinHjelmare * remove device_info * hound * stale comment * Add deprecation warning * address comments * lint
This commit is contained in:
1 parent
1e95719436
commit
55d1d3d8ae
13 files changed
+408
-90
No files matched your search
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"config": {
|
||||
"error": {
|
||||
"name_exists": "Name already exists"
|
||||
},
|
||||
"step": {
|
||||
"user": {
|
||||
"data": {
|
||||
"latitude": "Latitude",
|
||||
"longitude": "Longitude",
|
||||
"name": "Name"
|
||||
},
|
||||
"title": "Location"
|
||||
}
|
||||
},
|
||||
"title": "Portuguese weather service (IPMA)"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
Component for the Portuguese weather service - IPMA.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/ipma/
|
||||
"""
|
||||
from homeassistant.core import Config, HomeAssistant
|
||||
from .config_flow import IpmaFlowHandler # noqa
|
||||
from .const import DOMAIN # noqa
|
||||
|
||||
DEFAULT_NAME = 'ipma'
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
|
||||
"""Set up configured IPMA."""
|
||||
# No support for component configuration
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry):
|
||||
"""Set up IPMA station as config entry."""
|
||||
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'weather'))
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass, config_entry):
|
||||
"""Unload a config entry."""
|
||||
await hass.config_entries.async_forward_entry_unload(
|
||||
config_entry, 'weather')
|
||||
return True
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Config flow to configure IPMA component."""
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import DOMAIN, HOME_LOCATION_NAME
|
||||
|
||||
|
||||
@config_entries.HANDLERS.register(DOMAIN)
|
||||
class IpmaFlowHandler(data_entry_flow.FlowHandler):
|
||||
"""Config flow for IPMA component."""
|
||||
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||
|
||||
def __init__(self):
|
||||
"""Init IpmaFlowHandler."""
|
||||
self._errors = {}
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle a flow initialized by the user."""
|
||||
self._errors = {}
|
||||
|
||||
if user_input is not None:
|
||||
if user_input[CONF_NAME] not in\
|
||||
self.hass.config_entries.async_entries(DOMAIN):
|
||||
return self.async_create_entry(
|
||||
title=user_input[CONF_NAME],
|
||||
data=user_input,
|
||||
)
|
||||
|
||||
self._errors[CONF_NAME] = 'name_exists'
|
||||
|
||||
# default location is set hass configuration
|
||||
return await self._show_config_form(
|
||||
name=HOME_LOCATION_NAME,
|
||||
latitude=self.hass.config.latitude,
|
||||
longitude=self.hass.config.longitude)
|
||||
|
||||
async def _show_config_form(self, name=None, latitude=None,
|
||||
longitude=None):
|
||||
"""Show the configuration form to edit location data."""
|
||||
return self.async_show_form(
|
||||
step_id='user',
|
||||
data_schema=vol.Schema({
|
||||
vol.Required(CONF_NAME, default=name): str,
|
||||
vol.Required(CONF_LATITUDE, default=latitude): cv.latitude,
|
||||
vol.Required(CONF_LONGITUDE, default=longitude): cv.longitude
|
||||
}),
|
||||
errors=self._errors,
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Constants in ipma component."""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
|
||||
|
||||
DOMAIN = 'ipma'
|
||||
|
||||
HOME_LOCATION_NAME = 'Home'
|
||||
|
||||
ENTITY_ID_SENSOR_FORMAT = WEATHER_DOMAIN + ".ipma_{}"
|
||||
ENTITY_ID_SENSOR_FORMAT_HOME = ENTITY_ID_SENSOR_FORMAT.format(
|
||||
HOME_LOCATION_NAME)
|
||||
|
||||
_LOGGER = logging.getLogger('homeassistant.components.ipma')
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"config": {
|
||||
"title": "Portuguese weather service (IPMA)",
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "Location",
|
||||
"description": "Instituto Português do Mar e Atmosfera",
|
||||
"data": {
|
||||
"name": "Name",
|
||||
"latitude": "Latitude",
|
||||
"longitude": "Longitude"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"name_exists": "Name already exists"
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
-3
@@ -20,7 +20,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
REQUIREMENTS = ['pyipma==1.1.6']
|
||||
REQUIREMENTS = ['pyipma==1.2.1']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -56,7 +56,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the ipma platform."""
|
||||
"""Set up the ipma platform.
|
||||
|
||||
Deprecated.
|
||||
"""
|
||||
_LOGGER.warning('Loading IPMA via platform config is deprecated')
|
||||
|
||||
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
||||
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
||||
|
||||
@@ -64,6 +69,23 @@ async def async_setup_platform(hass, config, async_add_entities,
|
||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||
return
|
||||
|
||||
station = await async_get_station(hass, latitude, longitude)
|
||||
|
||||
async_add_entities([IPMAWeather(station, config)], True)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Add a weather entity from a config_entry."""
|
||||
latitude = config_entry.data[CONF_LATITUDE]
|
||||
longitude = config_entry.data[CONF_LONGITUDE]
|
||||
|
||||
station = await async_get_station(hass, latitude, longitude)
|
||||
|
||||
async_add_entities([IPMAWeather(station, config_entry.data)], True)
|
||||
|
||||
|
||||
async def async_get_station(hass, latitude, longitude):
|
||||
"""Retrieve weather station, station name to be used as the entity name."""
|
||||
from pyipma import Station
|
||||
|
||||
websession = async_get_clientsession(hass)
|
||||
@@ -74,7 +96,7 @@ async def async_setup_platform(hass, config, async_add_entities,
|
||||
_LOGGER.debug("Initializing for coordinates %s, %s -> station %s",
|
||||
latitude, longitude, station.local)
|
||||
|
||||
async_add_entities([IPMAWeather(station, config)], True)
|
||||
return station
|
||||
|
||||
|
||||
class IPMAWeather(WeatherEntity):
|
||||
@@ -103,6 +125,11 @@ class IPMAWeather(WeatherEntity):
|
||||
self._forecast = await self._station.forecast()
|
||||
self._description = self._forecast[0].description
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id."""
|
||||
return '{}, {}'.format(self._station.latitude, self._station.longitude)
|
||||
|
||||
@property
|
||||
def attribution(self):
|
||||
"""Return the attribution."""
|
||||
@@ -125,26 +152,41 @@ class IPMAWeather(WeatherEntity):
|
||||
@property
|
||||
def temperature(self):
|
||||
"""Return the current temperature."""
|
||||
if not self._condition:
|
||||
return None
|
||||
|
||||
return self._condition.temperature
|
||||
|
||||
@property
|
||||
def pressure(self):
|
||||
"""Return the current pressure."""
|
||||
if not self._condition:
|
||||
return None
|
||||
|
||||
return self._condition.pressure
|
||||
|
||||
@property
|
||||
def humidity(self):
|
||||
"""Return the name of the sensor."""
|
||||
if not self._condition:
|
||||
return None
|
||||
|
||||
return self._condition.humidity
|
||||
|
||||
@property
|
||||
def wind_speed(self):
|
||||
"""Return the current windspeed."""
|
||||
if not self._condition:
|
||||
return None
|
||||
|
||||
return self._condition.windspeed
|
||||
|
||||
@property
|
||||
def wind_bearing(self):
|
||||
"""Return the current wind bearing (degrees)."""
|
||||
if not self._condition:
|
||||
return None
|
||||
|
||||
return self._condition.winddirection
|
||||
|
||||
@property
|
||||
Reference in new issue
Block a user