Zone component config entry support (#14059)
* Initial commit * Add error handling to config flow Change unique identifyer to name Clean up hound comments * Ensure hass home zone is created with correct entity id Fix failing tests * Fix rest of tests * Move zone tests to zone folder Create config flow tests * Add possibility to unload entry * Use hass.data instead of globas * Don't calculate configures zones every loop iteration * No need to know about home zone during setup of entry * Only use name as title * Don't cache hass home zone * Add new tests for setup and setup entry * Break out functionality from init to zone.py * Make hass home zone be created directly * Make sure that config flow doesn't override hass home zone * A newline was missing in const * Configured zones shall not be imported Removed config flow import functionality Improved tests
This commit is contained in:
1 parent
f5de2b9e5b
commit
4b06392442
13 files changed
+351
-83
No files matched your search
@@ -0,0 +1,93 @@
|
||||
"""
|
||||
Support for the definition of zones.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/zone/
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
CONF_NAME, CONF_LATITUDE, CONF_LONGITUDE, CONF_ICON, CONF_RADIUS)
|
||||
from homeassistant.helpers import config_per_platform
|
||||
from homeassistant.helpers.entity import async_generate_entity_id
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from .config_flow import configured_zones
|
||||
from .const import CONF_PASSIVE, DOMAIN, HOME_ZONE
|
||||
from .zone import Zone
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = 'Unnamed zone'
|
||||
DEFAULT_PASSIVE = False
|
||||
DEFAULT_RADIUS = 100
|
||||
|
||||
ENTITY_ID_FORMAT = 'zone.{}'
|
||||
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format(HOME_ZONE)
|
||||
|
||||
ICON_HOME = 'mdi:home'
|
||||
ICON_IMPORT = 'mdi:import'
|
||||
|
||||
# The config that zone accepts is the same as if it has platforms.
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Required(CONF_LATITUDE): cv.latitude,
|
||||
vol.Required(CONF_LONGITUDE): cv.longitude,
|
||||
vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): vol.Coerce(float),
|
||||
vol.Optional(CONF_PASSIVE, default=DEFAULT_PASSIVE): cv.boolean,
|
||||
vol.Optional(CONF_ICON): cv.icon,
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Setup configured zones as well as home assistant zone if necessary."""
|
||||
if DOMAIN not in hass.data:
|
||||
hass.data[DOMAIN] = {}
|
||||
zone_entries = configured_zones(hass)
|
||||
for _, entry in config_per_platform(config, DOMAIN):
|
||||
name = slugify(entry[CONF_NAME])
|
||||
if name not in zone_entries:
|
||||
zone = Zone(hass, entry[CONF_NAME], entry[CONF_LATITUDE],
|
||||
entry[CONF_LONGITUDE], entry.get(CONF_RADIUS),
|
||||
entry.get(CONF_ICON), entry.get(CONF_PASSIVE))
|
||||
zone.entity_id = async_generate_entity_id(
|
||||
ENTITY_ID_FORMAT, entry[CONF_NAME], None, hass)
|
||||
hass.async_add_job(zone.async_update_ha_state())
|
||||
hass.data[DOMAIN][name] = zone
|
||||
|
||||
if HOME_ZONE not in hass.data[DOMAIN] and HOME_ZONE not in zone_entries:
|
||||
name = hass.config.location_name
|
||||
zone = Zone(hass, name, hass.config.latitude, hass.config.longitude,
|
||||
DEFAULT_RADIUS, ICON_HOME, False)
|
||||
zone.entity_id = ENTITY_ID_HOME
|
||||
hass.async_add_job(zone.async_update_ha_state())
|
||||
hass.data[DOMAIN][slugify(name)] = zone
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry):
|
||||
"""Set up zone as config entry."""
|
||||
entry = config_entry.data
|
||||
name = entry[CONF_NAME]
|
||||
zone = Zone(hass, name, entry[CONF_LATITUDE], entry[CONF_LONGITUDE],
|
||||
entry.get(CONF_RADIUS), entry.get(CONF_ICON),
|
||||
entry.get(CONF_PASSIVE))
|
||||
zone.entity_id = async_generate_entity_id(
|
||||
ENTITY_ID_FORMAT, name, None, hass)
|
||||
hass.async_add_job(zone.async_update_ha_state())
|
||||
hass.data[DOMAIN][slugify(name)] = zone
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass, config_entry):
|
||||
"""Unload a config entry."""
|
||||
zones = hass.data[DOMAIN]
|
||||
name = slugify(config_entry.data[CONF_NAME])
|
||||
zone = zones.pop(name)
|
||||
await zone.async_remove()
|
||||
return True
|
||||
Reference in new issue
Block a user