From ac4e54c6ff7d2a6cab537639984aa5cd7645cf84 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 24 Sep 2016 00:03:44 -0700 Subject: [PATCH 1/2] Filter out falsey platform configs --- homeassistant/helpers/__init__.py | 5 ++++- tests/helpers/test_init.py | 1 - tests/test_bootstrap.py | 23 +++++++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/homeassistant/helpers/__init__.py b/homeassistant/helpers/__init__.py index 2dc3af1dff9..3e45d3ecb83 100644 --- a/homeassistant/helpers/__init__.py +++ b/homeassistant/helpers/__init__.py @@ -22,7 +22,10 @@ def config_per_platform(config: ConfigType, """ for config_key in extract_domain_configs(config, domain): platform_config = config[config_key] - if not isinstance(platform_config, list): + + if not platform_config: + continue + elif not isinstance(platform_config, list): platform_config = [platform_config] for item in platform_config: diff --git a/tests/helpers/test_init.py b/tests/helpers/test_init.py index 541247c5420..1791e5622a9 100644 --- a/tests/helpers/test_init.py +++ b/tests/helpers/test_init.py @@ -45,5 +45,4 @@ class TestHelpers(unittest.TestCase): ('hello', config['zone']), (None, 1), ('hello 2', config['zone Hallo'][1]), - (None, None) ] == list(helpers.config_per_platform(config, 'zone')) diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index b2a3136455a..b74a1de0d35 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -110,14 +110,6 @@ class TestBootstrap: loader.set_component( 'platform_conf.whatever', MockPlatform('whatever')) - assert not bootstrap._setup_component(self.hass, 'platform_conf', { - 'platform_conf': None - }) - - assert not bootstrap._setup_component(self.hass, 'platform_conf', { - 'platform_conf': {} - }) - assert not bootstrap._setup_component(self.hass, 'platform_conf', { 'platform_conf': { 'hello': 'world', @@ -150,6 +142,8 @@ class TestBootstrap: } }) + self.hass.config.components.remove('platform_conf') + assert bootstrap._setup_component(self.hass, 'platform_conf', { 'platform_conf': [{ 'platform': 'whatever', @@ -157,6 +151,19 @@ class TestBootstrap: }] }) + self.hass.config.components.remove('platform_conf') + + # Any falsey paltform config will be ignored (None, {}, etc) + assert bootstrap._setup_component(self.hass, 'platform_conf', { + 'platform_conf': None + }) + + self.hass.config.components.remove('platform_conf') + + assert bootstrap._setup_component(self.hass, 'platform_conf', { + 'platform_conf': {} + }) + def test_component_not_found(self): """setup_component should not crash if component doesn't exist.""" assert not bootstrap.setup_component(self.hass, 'non_existing') From a1488b46f6e61499abbdff712fbcc9e5f82fc939 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 24 Sep 2016 00:04:03 -0700 Subject: [PATCH 2/2] Fix zone being setup twice --- .../components/device_tracker/owntracks.py | 13 ++++- homeassistant/components/zone.py | 53 ++++--------------- tests/components/test_zone.py | 12 +++++ 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/device_tracker/owntracks.py b/homeassistant/components/device_tracker/owntracks.py index 4f6e6647f1c..1395f2940fe 100644 --- a/homeassistant/components/device_tracker/owntracks.py +++ b/homeassistant/components/device_tracker/owntracks.py @@ -218,9 +218,18 @@ def setup_scanner(hass, config, see): lat = wayp[WAYPOINT_LAT_KEY] lon = wayp[WAYPOINT_LON_KEY] rad = wayp['rad'] + + # check zone exists + entity_id = zone_comp.ENTITY_ID_FORMAT.format(slugify(pretty_name)) + + # Check if state already exists + if hass.states.get(entity_id) is not None: + continue + zone = zone_comp.Zone(hass, pretty_name, lat, lon, rad, - zone_comp.ICON_IMPORT, False, True) - zone_comp.add_zone(hass, pretty_name, zone) + zone_comp.ICON_IMPORT, False) + zone.entity_id = entity_id + zone.update_ha_state() def see_beacons(dev_id, kwargs_param): """Set active beacons to the current location.""" diff --git a/homeassistant/components/zone.py b/homeassistant/components/zone.py index 23bc6fd409c..ba5e1d6c9fc 100644 --- a/homeassistant/components/zone.py +++ b/homeassistant/components/zone.py @@ -11,7 +11,7 @@ import voluptuous as vol from homeassistant.const import ( 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 config_per_platform from homeassistant.helpers.entity import Entity, generate_entity_id from homeassistant.util.location import distance import homeassistant.helpers.config_validation as cv @@ -90,60 +90,30 @@ def in_zone(zone, latitude, longitude, radius=0): def setup(hass, config): """Setup zone.""" entities = set() - for key in extract_domain_configs(config, DOMAIN): - entries = config[key] - if not isinstance(entries, list): - entries = entries, - - for entry in entries: - 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) + for _, entry in config_per_platform(config, DOMAIN): + name = entry.get(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 = generate_entity_id(ENTITY_ID_FORMAT, name, entities) + zone.update_ha_state() + entities.add(zone.entity_id) if ENTITY_ID_HOME not in entities: zone = Zone(hass, hass.config.location_name, hass.config.latitude, hass.config.longitude, - DEFAULT_RADIUS, ICON_HOME, False, False) - add_zone(hass, hass.config.location_name, zone, entities) + DEFAULT_RADIUS, ICON_HOME, False) zone.entity_id = ENTITY_ID_HOME zone.update_ha_state() return True -# Add a zone to the existing set -def add_zone(hass, name, zone, entities=None): - """Add a zone from other components.""" - _LOGGER.info("Adding new zone %s", name) - if entities is None: - _entities = set() - else: - _entities = 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() - _entities.add(zone.entity_id) - return zone - else: - _LOGGER.info("Zone already exists") - return zone_exists - - class Zone(Entity): """Representation of a Zone.""" # pylint: disable=too-many-arguments, too-many-instance-attributes - def __init__(self, hass, name, latitude, longitude, radius, icon, passive, - imported): + def __init__(self, hass, name, latitude, longitude, radius, icon, passive): """Initialize the zone.""" self.hass = hass self._name = name @@ -152,7 +122,6 @@ class Zone(Entity): self._radius = radius self._icon = icon self._passive = passive - self._imported = imported @property def name(self): diff --git a/tests/components/test_zone.py b/tests/components/test_zone.py index abc465bf9dd..4eefe8c0031 100644 --- a/tests/components/test_zone.py +++ b/tests/components/test_zone.py @@ -18,6 +18,18 @@ class TestComponentZone(unittest.TestCase): """Stop down everything that was started.""" self.hass.stop() + def test_setup_no_zones_still_adds_home_zone(self): + """Test if no config is passed in we still get the home zone.""" + assert bootstrap.setup_component(self.hass, zone.DOMAIN, + {'zone': None}) + + assert len(self.hass.states.entity_ids('zone')) == 1 + state = self.hass.states.get('zone.home') + assert self.hass.config.location_name == state.name + assert self.hass.config.latitude == state.attributes['latitude'] + assert self.hass.config.longitude == state.attributes['longitude'] + assert not state.attributes.get('passive', False) + def test_setup(self): """Test setup.""" info = {