Enable config flow for Luftdaten (#17700)

* Move file to new location

* Update requirement

* Enable config flow

* Add luftdaten

* Add tests

* Update

* Add constants

* Changes according to the review comments

* Remove wrong entry from flows

* Fix dict handling

* Add callback and use OrderedDict

* Remve leftover

* Fix

* Remove await
This commit is contained in:
Fabian Affolter authored and GitHub committed 2018-11-06 14:27:52 +01:00
1 parent 7933bd7f91
commit 2e517ab6bc
13 files changed
+508 -108

No files matched your search

@@ -0,0 +1,170 @@
"""
Support for Luftdaten stations.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/luftdaten/
"""
import logging
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import (
CONF_MONITORED_CONDITIONS, CONF_SCAN_INTERVAL, CONF_SENSORS,
CONF_SHOW_ON_MAP, TEMP_CELSIUS)
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from .config_flow import configured_sensors
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
REQUIREMENTS = ['luftdaten==0.3.4']
_LOGGER = logging.getLogger(__name__)
DATA_LUFTDATEN = 'luftdaten'
DATA_LUFTDATEN_CLIENT = 'data_luftdaten_client'
DATA_LUFTDATEN_LISTENER = 'data_luftdaten_listener'
DEFAULT_ATTRIBUTION = "Data provided by luftdaten.info"
SENSOR_HUMIDITY = 'humidity'
SENSOR_PM10 = 'P1'
SENSOR_PM2_5 = 'P2'
SENSOR_PRESSURE = 'pressure'
SENSOR_TEMPERATURE = 'temperature'
TOPIC_UPDATE = '{0}_data_update'.format(DOMAIN)
VOLUME_MICROGRAMS_PER_CUBIC_METER = 'µg/m3'
SENSORS = {
SENSOR_TEMPERATURE: ['Temperature', 'mdi:thermometer', TEMP_CELSIUS],
SENSOR_HUMIDITY: ['Humidity', 'mdi:water-percent', '%'],
SENSOR_PRESSURE: ['Pressure', 'mdi:arrow-down-bold', 'Pa'],
SENSOR_PM10: ['PM10', 'mdi:thought-bubble',
VOLUME_MICROGRAMS_PER_CUBIC_METER],
SENSOR_PM2_5: ['PM2.5', 'mdi:thought-bubble-outline',
VOLUME_MICROGRAMS_PER_CUBIC_METER]
}
SENSOR_SCHEMA = vol.Schema({
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)):
vol.All(cv.ensure_list, [vol.In(SENSORS)])
})
CONFIG_SCHEMA = vol.Schema({
DOMAIN:
vol.Schema({
vol.Required(CONF_SENSOR_ID): cv.positive_int,
vol.Optional(CONF_SENSORS, default={}): SENSOR_SCHEMA,
vol.Optional(CONF_SHOW_ON_MAP, default=False): cv.boolean,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL):
cv.time_period,
})
}, extra=vol.ALLOW_EXTRA)
async def async_setup(hass, config):
"""Set up the Luftdaten component."""
hass.data[DOMAIN] = {}
hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT] = {}
hass.data[DOMAIN][DATA_LUFTDATEN_LISTENER] = {}
if DOMAIN not in config:
return True
conf = config[DOMAIN]
station_id = conf.get(CONF_SENSOR_ID)
if station_id not in configured_sensors(hass):
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={'source': SOURCE_IMPORT},
data={
CONF_SENSORS: conf[CONF_SENSORS],
CONF_SENSOR_ID: conf[CONF_SENSOR_ID],
CONF_SHOW_ON_MAP: conf[CONF_SHOW_ON_MAP],
}
)
)
hass.data[DOMAIN][CONF_SCAN_INTERVAL] = conf[CONF_SCAN_INTERVAL]
return True
async def async_setup_entry(hass, config_entry):
"""Set up Luftdaten as config entry."""
from luftdaten import Luftdaten
from luftdaten.exceptions import LuftdatenError
session = async_get_clientsession(hass)
try:
luftdaten = LuftDatenData(
Luftdaten(
config_entry.data[CONF_SENSOR_ID], hass.loop, session),
config_entry.data.get(CONF_SENSORS, {}).get(
CONF_MONITORED_CONDITIONS, list(SENSORS)))
await luftdaten.async_update()
hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT][config_entry.entry_id] = \
luftdaten
except LuftdatenError:
raise ConfigEntryNotReady
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
config_entry, 'sensor'))
async def refresh_sensors(event_time):
"""Refresh Luftdaten data."""
await luftdaten.async_update()
async_dispatcher_send(hass, TOPIC_UPDATE)
hass.data[DOMAIN][DATA_LUFTDATEN_LISTENER][
config_entry.entry_id] = async_track_time_interval(
hass, refresh_sensors,
hass.data[DOMAIN].get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
return True
async def async_unload_entry(hass, config_entry):
"""Unload an Luftdaten config entry."""
remove_listener = hass.data[DOMAIN][DATA_LUFTDATEN_LISTENER].pop(
config_entry.entry_id)
remove_listener()
for component in ('sensor', ):
await hass.config_entries.async_forward_entry_unload(
config_entry, component)
hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT].pop(config_entry.entry_id)
return True
class LuftDatenData:
"""Define a generic Luftdaten object."""
def __init__(self, client, sensor_conditions):
"""Initialize the Luftdata object."""
self.client = client
self.data = {}
self.sensor_conditions = sensor_conditions
async def async_update(self):
"""Update sensor/binary sensor data."""
from luftdaten.exceptions import LuftdatenError
try:
await self.client.get_data()
self.data[DATA_LUFTDATEN] = self.client.values
self.data[DATA_LUFTDATEN].update(self.client.meta)
except LuftdatenError:
_LOGGER.error("Unable to retrieve data from luftdaten.info")
@@ -0,0 +1,75 @@
"""Config flow to configure the Luftdaten component."""
from collections import OrderedDict
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP
from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
@callback
def configured_sensors(hass):
"""Return a set of configured Luftdaten sensors."""
return set(
'{0}'.format(entry.data[CONF_SENSOR_ID])
for entry in hass.config_entries.async_entries(DOMAIN))
@config_entries.HANDLERS.register(DOMAIN)
class LuftDatenFlowHandler(config_entries.ConfigFlow):
"""Handle a Luftdaten config flow."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
@callback
def _show_form(self, errors=None):
"""Show the form to the user."""
data_schema = OrderedDict()
data_schema[vol.Required(CONF_SENSOR_ID)] = str
data_schema[vol.Optional(CONF_SHOW_ON_MAP, default=False)] = bool
return self.async_show_form(
step_id='user',
data_schema=vol.Schema(data_schema),
errors=errors or {}
)
async def async_step_import(self, import_config):
"""Import a config entry from configuration.yaml."""
return await self.async_step_user(import_config)
async def async_step_user(self, user_input=None):
"""Handle the start of the config flow."""
from luftdaten import Luftdaten, exceptions
if not user_input:
return self._show_form()
sensor_id = user_input[CONF_SENSOR_ID]
if sensor_id in configured_sensors(self.hass):
return self._show_form({CONF_SENSOR_ID: 'sensor_exists'})
session = aiohttp_client.async_get_clientsession(self.hass)
luftdaten = Luftdaten(
user_input[CONF_SENSOR_ID], self.hass.loop, session)
try:
await luftdaten.get_data()
valid = await luftdaten.validate_sensor()
except exceptions.LuftdatenConnectionError:
return self._show_form(
{CONF_SENSOR_ID: 'communication_error'})
if not valid:
return self._show_form({CONF_SENSOR_ID: 'invalid_sensor'})
scan_interval = user_input.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
user_input.update({CONF_SCAN_INTERVAL: scan_interval.seconds})
return self.async_create_entry(title=sensor_id, data=user_input)
@@ -0,0 +1,10 @@
"""Define constants for the Luftdaten component."""
from datetime import timedelta
ATTR_SENSOR_ID = 'sensor_id'
CONF_SENSOR_ID = 'sensor_id'
DEFAULT_SCAN_INTERVAL = timedelta(minutes=10)
DOMAIN = 'luftdaten'
@@ -0,0 +1,20 @@
{
"config": {
"title": "Luftdaten",
"step": {
"user": {
"title": "Define Luftdaten",
"data": {
"station_id": "Luftdaten Sensor ID",
"show_on_map": "Show on map"
}
}
},
"error": {
"sensor_exists": "Sensor already registered",
"invalid_sensor": "Sensor not available or invalid",
"communication_error": "Unable to communicate with the Luftdaten API"
}
}
}