Skip to content

Commit

Permalink
Zone - Hass configuration name is optional (#14449)
Browse files Browse the repository at this point in the history
* Hass configuration name is optional

* Check explicitly if name is none

* Reverted back to old logic for zones configured in configuration.yaml, where many zones can have the same name

* New test to verify use case of allowing multiple zones having the same name

* Fix too long line
  • Loading branch information
Kane610 authored and balloob committed Jun 8, 2018
1 parent 1cfd770 commit 9a659a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
18 changes: 8 additions & 10 deletions homeassistant/components/zone/__init__.py
Expand Up @@ -45,27 +45,25 @@

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] = {}
hass.data[DOMAIN] = {}
entities = set()
zone_entries = configured_zones(hass)
for _, entry in config_per_platform(config, DOMAIN):
name = slugify(entry[CONF_NAME])
if name not in zone_entries:
if slugify(entry[CONF_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)
ENTITY_ID_FORMAT, entry[CONF_NAME], entities)
hass.async_add_job(zone.async_update_ha_state())
hass.data[DOMAIN][name] = zone
entities.add(zone.entity_id)

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,
if ENTITY_ID_HOME not in entities and HOME_ZONE not in zone_entries:
zone = Zone(hass, hass.config.location_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

Expand Down
19 changes: 11 additions & 8 deletions tests/components/zone/test_init.py
Expand Up @@ -59,7 +59,6 @@ def test_setup_no_zones_still_adds_home_zone(self):
assert self.hass.config.latitude == state.attributes['latitude']
assert self.hass.config.longitude == state.attributes['longitude']
assert not state.attributes.get('passive', False)
assert 'test_home' in self.hass.data[zone.DOMAIN]

def test_setup(self):
"""Test a successful setup."""
Expand All @@ -79,8 +78,6 @@ def test_setup(self):
assert info['longitude'] == state.attributes['longitude']
assert info['radius'] == state.attributes['radius']
assert info['passive'] == state.attributes['passive']
assert 'test_zone' in self.hass.data[zone.DOMAIN]
assert 'test_home' in self.hass.data[zone.DOMAIN]

def test_setup_zone_skips_home_zone(self):
"""Test that zone named Home should override hass home zone."""
Expand All @@ -94,8 +91,17 @@ def test_setup_zone_skips_home_zone(self):
assert len(self.hass.states.entity_ids('zone')) == 1
state = self.hass.states.get('zone.home')
assert info['name'] == state.name
assert 'home' in self.hass.data[zone.DOMAIN]
assert 'test_home' not in self.hass.data[zone.DOMAIN]

def test_setup_name_can_be_same_on_multiple_zones(self):
"""Test that zone named Home should override hass home zone."""
info = {
'name': 'Test Zone',
'latitude': 1.1,
'longitude': -2.2,
}
assert setup.setup_component(
self.hass, zone.DOMAIN, {'zone': [info, info]})
assert len(self.hass.states.entity_ids('zone')) == 3

def test_setup_registered_zone_skips_home_zone(self):
"""Test that config entry named home should override hass home zone."""
Expand All @@ -105,7 +111,6 @@ def test_setup_registered_zone_skips_home_zone(self):
entry.add_to_hass(self.hass)
assert setup.setup_component(self.hass, zone.DOMAIN, {'zone': None})
assert len(self.hass.states.entity_ids('zone')) == 0
assert not self.hass.data[zone.DOMAIN]

def test_setup_registered_zone_skips_configured_zone(self):
"""Test if config entry will override configured zone."""
Expand All @@ -123,8 +128,6 @@ def test_setup_registered_zone_skips_configured_zone(self):
assert len(self.hass.states.entity_ids('zone')) == 1
state = self.hass.states.get('zone.test_zone')
assert not state
assert 'test_zone' not in self.hass.data[zone.DOMAIN]
assert 'test_home' in self.hass.data[zone.DOMAIN]

def test_active_zone_skips_passive_zones(self):
"""Test active and passive zones."""
Expand Down

0 comments on commit 9a659a5

Please sign in to comment.