Fix duplicate luftdaten entities (#20226)

* Use same data schema for configflow, make sensor_id a positive integer.

* Change sensor_id to int and remove duplicate Luftdaten config entries.

This fixes #18838, and also fixes the root cause
of #19981 and #19622.

* Use pure type for boolean.
This commit is contained in:
Jasper van der Neut - Stulen 2019-01-31 02:12:59 +01:00 committed by Paulus Schoutsen
parent 473bf93973
commit 5a0c707a37
2 changed files with 35 additions and 5 deletions

View File

@ -12,13 +12,14 @@ 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.core import callback
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 .config_flow import configured_sensors, duplicate_stations
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
REQUIREMENTS = ['luftdaten==0.3.4']
@ -67,6 +68,14 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
@callback
def _async_fixup_sensor_id(hass, config_entry, sensor_id):
hass.config_entries.async_update_entry(
config_entry, data={
**config_entry.data, CONF_SENSOR_ID: int(sensor_id)
})
async def async_setup(hass, config):
"""Set up the Luftdaten component."""
hass.data[DOMAIN] = {}
@ -77,7 +86,7 @@ async def async_setup(hass, config):
return True
conf = config[DOMAIN]
station_id = conf.get(CONF_SENSOR_ID)
station_id = conf[CONF_SENSOR_ID]
if station_id not in configured_sensors(hass):
hass.async_create_task(
@ -102,6 +111,18 @@ async def async_setup_entry(hass, config_entry):
from luftdaten import Luftdaten
from luftdaten.exceptions import LuftdatenError
if not isinstance(config_entry.data[CONF_SENSOR_ID], int):
_async_fixup_sensor_id(hass, config_entry,
config_entry.data[CONF_SENSOR_ID])
if (config_entry.data[CONF_SENSOR_ID] in
duplicate_stations(hass) and config_entry.source == SOURCE_IMPORT):
_LOGGER.warning("Removing duplicate sensors for station %s",
config_entry.data[CONF_SENSOR_ID])
hass.async_create_task(hass.config_entries.async_remove(
config_entry.entry_id))
return False
session = async_get_clientsession(hass)
try:

View File

@ -7,6 +7,7 @@ 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
import homeassistant.helpers.config_validation as cv
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
@ -15,10 +16,18 @@ from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
def configured_sensors(hass):
"""Return a set of configured Luftdaten sensors."""
return set(
'{0}'.format(entry.data[CONF_SENSOR_ID])
entry.data[CONF_SENSOR_ID]
for entry in hass.config_entries.async_entries(DOMAIN))
@callback
def duplicate_stations(hass):
"""Return a set of duplicate configured Luftdaten stations."""
stations = [int(entry.data[CONF_SENSOR_ID])
for entry in hass.config_entries.async_entries(DOMAIN)]
return {x for x in stations if stations.count(x) > 1}
@config_entries.HANDLERS.register(DOMAIN)
class LuftDatenFlowHandler(config_entries.ConfigFlow):
"""Handle a Luftdaten config flow."""
@ -30,7 +39,7 @@ class LuftDatenFlowHandler(config_entries.ConfigFlow):
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.Required(CONF_SENSOR_ID)] = cv.positive_int
data_schema[vol.Optional(CONF_SHOW_ON_MAP, default=False)] = bool
return self.async_show_form(
@ -72,4 +81,4 @@ class LuftDatenFlowHandler(config_entries.ConfigFlow):
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)
return self.async_create_entry(title=str(sensor_id), data=user_input)