Skip to content

Commit

Permalink
Use voluptuous for Zone (#3377)
Browse files Browse the repository at this point in the history
* Migrate to voluptuous

* Zone: Remove unneeded latitude/longitude check
  • Loading branch information
fabaff authored and balloob committed Sep 14, 2016
1 parent 7724cb9 commit 782838a
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions homeassistant/components/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,47 @@
"""
import logging

import voluptuous as vol

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.entity import Entity, generate_entity_id
from homeassistant.util.location import distance
from homeassistant.util import convert

DOMAIN = "zone"
ENTITY_ID_FORMAT = 'zone.{}'
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
STATE = 'zoning'
import homeassistant.helpers.config_validation as cv

DEFAULT_NAME = 'Unnamed zone'
_LOGGER = logging.getLogger(__name__)

ATTR_PASSIVE = 'passive'
ATTR_RADIUS = 'radius'
DEFAULT_RADIUS = 100

ATTR_PASSIVE = 'passive'
CONF_PASSIVE = 'passive'
CONF_RADIUS = 'radius'

DEFAULT_NAME = 'Unnamed zone'
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_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):
Expand Down Expand Up @@ -80,20 +97,15 @@ def setup(hass, config):
entries = entries,

for entry in entries:
name = entry.get(CONF_NAME, DEFAULT_NAME)
latitude = convert(entry.get(ATTR_LATITUDE), float)
longitude = convert(entry.get(ATTR_LONGITUDE), float)
radius = convert(entry.get(ATTR_RADIUS, DEFAULT_RADIUS), float)
icon = entry.get(ATTR_ICON)
passive = entry.get(ATTR_PASSIVE, DEFAULT_PASSIVE)

if None in (latitude, longitude):
logging.getLogger(__name__).error(
'Each zone needs a latitude and longitude.')
continue

zone = Zone(hass, name, latitude, longitude, radius,
icon, passive, False)
name = entry.get(CONF_NAME)
latitude = entry.get(CONF_LATITUDE)
longitude = entry.get(CONF_LONGITUDE)
radius = entry.get(CONF_RADIUS)
icon = entry.get(CONF_ICON)
passive = entry.get(CONF_PASSIVE)

zone = Zone(
hass, name, latitude, longitude, radius, icon, passive, False)
add_zone(hass, name, zone, entities)
entities.add(zone.entity_id)

Expand All @@ -116,8 +128,7 @@ def add_zone(hass, name, zone, entities=None):
_entities = set()
else:
_entities = entities
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
_entities)
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, _entities)
zone_exists = hass.states.get(zone.entity_id)
if zone_exists is None:
zone.update_ha_state()
Expand Down

0 comments on commit 782838a

Please sign in to comment.