mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Zone - Hass configuration name is optional (#14449)
* 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
This commit is contained in:
parent
1cfd770b95
commit
9a659a5d1d
@ -45,27 +45,25 @@ PLATFORM_SCHEMA = vol.Schema({
|
|||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Setup configured zones as well as home assistant zone if necessary."""
|
"""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)
|
zone_entries = configured_zones(hass)
|
||||||
for _, entry in config_per_platform(config, DOMAIN):
|
for _, entry in config_per_platform(config, DOMAIN):
|
||||||
name = slugify(entry[CONF_NAME])
|
if slugify(entry[CONF_NAME]) not in zone_entries:
|
||||||
if name not in zone_entries:
|
|
||||||
zone = Zone(hass, entry[CONF_NAME], entry[CONF_LATITUDE],
|
zone = Zone(hass, entry[CONF_NAME], entry[CONF_LATITUDE],
|
||||||
entry[CONF_LONGITUDE], entry.get(CONF_RADIUS),
|
entry[CONF_LONGITUDE], entry.get(CONF_RADIUS),
|
||||||
entry.get(CONF_ICON), entry.get(CONF_PASSIVE))
|
entry.get(CONF_ICON), entry.get(CONF_PASSIVE))
|
||||||
zone.entity_id = async_generate_entity_id(
|
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.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:
|
if ENTITY_ID_HOME not in entities and HOME_ZONE not in zone_entries:
|
||||||
name = hass.config.location_name
|
zone = Zone(hass, hass.config.location_name,
|
||||||
zone = Zone(hass, name, hass.config.latitude, hass.config.longitude,
|
hass.config.latitude, hass.config.longitude,
|
||||||
DEFAULT_RADIUS, ICON_HOME, False)
|
DEFAULT_RADIUS, ICON_HOME, False)
|
||||||
zone.entity_id = ENTITY_ID_HOME
|
zone.entity_id = ENTITY_ID_HOME
|
||||||
hass.async_add_job(zone.async_update_ha_state())
|
hass.async_add_job(zone.async_update_ha_state())
|
||||||
hass.data[DOMAIN][slugify(name)] = zone
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -59,7 +59,6 @@ class TestComponentZone(unittest.TestCase):
|
|||||||
assert self.hass.config.latitude == state.attributes['latitude']
|
assert self.hass.config.latitude == state.attributes['latitude']
|
||||||
assert self.hass.config.longitude == state.attributes['longitude']
|
assert self.hass.config.longitude == state.attributes['longitude']
|
||||||
assert not state.attributes.get('passive', False)
|
assert not state.attributes.get('passive', False)
|
||||||
assert 'test_home' in self.hass.data[zone.DOMAIN]
|
|
||||||
|
|
||||||
def test_setup(self):
|
def test_setup(self):
|
||||||
"""Test a successful setup."""
|
"""Test a successful setup."""
|
||||||
@ -79,8 +78,6 @@ class TestComponentZone(unittest.TestCase):
|
|||||||
assert info['longitude'] == state.attributes['longitude']
|
assert info['longitude'] == state.attributes['longitude']
|
||||||
assert info['radius'] == state.attributes['radius']
|
assert info['radius'] == state.attributes['radius']
|
||||||
assert info['passive'] == state.attributes['passive']
|
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):
|
def test_setup_zone_skips_home_zone(self):
|
||||||
"""Test that zone named Home should override hass home zone."""
|
"""Test that zone named Home should override hass home zone."""
|
||||||
@ -94,8 +91,17 @@ class TestComponentZone(unittest.TestCase):
|
|||||||
assert len(self.hass.states.entity_ids('zone')) == 1
|
assert len(self.hass.states.entity_ids('zone')) == 1
|
||||||
state = self.hass.states.get('zone.home')
|
state = self.hass.states.get('zone.home')
|
||||||
assert info['name'] == state.name
|
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):
|
def test_setup_registered_zone_skips_home_zone(self):
|
||||||
"""Test that config entry named home should override hass home zone."""
|
"""Test that config entry named home should override hass home zone."""
|
||||||
@ -105,7 +111,6 @@ class TestComponentZone(unittest.TestCase):
|
|||||||
entry.add_to_hass(self.hass)
|
entry.add_to_hass(self.hass)
|
||||||
assert setup.setup_component(self.hass, zone.DOMAIN, {'zone': None})
|
assert setup.setup_component(self.hass, zone.DOMAIN, {'zone': None})
|
||||||
assert len(self.hass.states.entity_ids('zone')) == 0
|
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):
|
def test_setup_registered_zone_skips_configured_zone(self):
|
||||||
"""Test if config entry will override configured zone."""
|
"""Test if config entry will override configured zone."""
|
||||||
@ -123,8 +128,6 @@ class TestComponentZone(unittest.TestCase):
|
|||||||
assert len(self.hass.states.entity_ids('zone')) == 1
|
assert len(self.hass.states.entity_ids('zone')) == 1
|
||||||
state = self.hass.states.get('zone.test_zone')
|
state = self.hass.states.get('zone.test_zone')
|
||||||
assert not state
|
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):
|
def test_active_zone_skips_passive_zones(self):
|
||||||
"""Test active and passive zones."""
|
"""Test active and passive zones."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user