Use voluptuous for Zone (#3377)

* Migrate to voluptuous

* Zone: Remove unneeded latitude/longitude check
This commit is contained in:
Fabian Affolter 2016-09-14 08:13:10 +02:00 committed by Paulus Schoutsen
parent 7724cb9eb4
commit 782838af56

View File

@ -6,30 +6,47 @@ https://home-assistant.io/components/zone/
""" """
import logging import logging
import voluptuous as vol
from homeassistant.const import ( from homeassistant.const import (
ATTR_HIDDEN, ATTR_ICON, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME) ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME, CONF_LATITUDE,
CONF_LONGITUDE, CONF_ICON)
from homeassistant.helpers import extract_domain_configs from homeassistant.helpers import extract_domain_configs
from homeassistant.helpers.entity import Entity, generate_entity_id from homeassistant.helpers.entity import Entity, generate_entity_id
from homeassistant.util.location import distance from homeassistant.util.location import distance
from homeassistant.util import convert import homeassistant.helpers.config_validation as cv
DOMAIN = "zone" _LOGGER = logging.getLogger(__name__)
ENTITY_ID_FORMAT = 'zone.{}'
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
STATE = 'zoning'
DEFAULT_NAME = 'Unnamed zone'
ATTR_RADIUS = 'radius'
DEFAULT_RADIUS = 100
ATTR_PASSIVE = 'passive' ATTR_PASSIVE = 'passive'
ATTR_RADIUS = 'radius'
CONF_PASSIVE = 'passive'
CONF_RADIUS = 'radius'
DEFAULT_NAME = 'Unnamed zone'
DEFAULT_PASSIVE = False DEFAULT_PASSIVE = False
DEFAULT_RADIUS = 100
DOMAIN = 'zone'
ENTITY_ID_FORMAT = 'zone.{}'
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
ICON_HOME = 'mdi:home' ICON_HOME = 'mdi:home'
ICON_IMPORT = 'mdi:import' ICON_IMPORT = 'mdi:import'
_LOGGER = logging.getLogger(__name__) STATE = 'zoning'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: 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)
def active_zone(hass, latitude, longitude, radius=0): def active_zone(hass, latitude, longitude, radius=0):
@ -80,20 +97,15 @@ def setup(hass, config):
entries = entries, entries = entries,
for entry in entries: for entry in entries:
name = entry.get(CONF_NAME, DEFAULT_NAME) name = entry.get(CONF_NAME)
latitude = convert(entry.get(ATTR_LATITUDE), float) latitude = entry.get(CONF_LATITUDE)
longitude = convert(entry.get(ATTR_LONGITUDE), float) longitude = entry.get(CONF_LONGITUDE)
radius = convert(entry.get(ATTR_RADIUS, DEFAULT_RADIUS), float) radius = entry.get(CONF_RADIUS)
icon = entry.get(ATTR_ICON) icon = entry.get(CONF_ICON)
passive = entry.get(ATTR_PASSIVE, DEFAULT_PASSIVE) passive = entry.get(CONF_PASSIVE)
if None in (latitude, longitude): zone = Zone(
logging.getLogger(__name__).error( hass, name, latitude, longitude, radius, icon, passive, False)
'Each zone needs a latitude and longitude.')
continue
zone = Zone(hass, name, latitude, longitude, radius,
icon, passive, False)
add_zone(hass, name, zone, entities) add_zone(hass, name, zone, entities)
entities.add(zone.entity_id) entities.add(zone.entity_id)
@ -116,8 +128,7 @@ def add_zone(hass, name, zone, entities=None):
_entities = set() _entities = set()
else: else:
_entities = entities _entities = entities
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, _entities)
_entities)
zone_exists = hass.states.get(zone.entity_id) zone_exists = hass.states.get(zone.entity_id)
if zone_exists is None: if zone_exists is None:
zone.update_ha_state() zone.update_ha_state()