mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Fix zone being setup twice
This commit is contained in:
parent
ac4e54c6ff
commit
a1488b46f6
@ -218,9 +218,18 @@ def setup_scanner(hass, config, see):
|
|||||||
lat = wayp[WAYPOINT_LAT_KEY]
|
lat = wayp[WAYPOINT_LAT_KEY]
|
||||||
lon = wayp[WAYPOINT_LON_KEY]
|
lon = wayp[WAYPOINT_LON_KEY]
|
||||||
rad = wayp['rad']
|
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 = zone_comp.Zone(hass, pretty_name, lat, lon, rad,
|
||||||
zone_comp.ICON_IMPORT, False, True)
|
zone_comp.ICON_IMPORT, False)
|
||||||
zone_comp.add_zone(hass, pretty_name, zone)
|
zone.entity_id = entity_id
|
||||||
|
zone.update_ha_state()
|
||||||
|
|
||||||
def see_beacons(dev_id, kwargs_param):
|
def see_beacons(dev_id, kwargs_param):
|
||||||
"""Set active beacons to the current location."""
|
"""Set active beacons to the current location."""
|
||||||
|
@ -11,7 +11,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME, CONF_LATITUDE,
|
ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME, CONF_LATITUDE,
|
||||||
CONF_LONGITUDE, CONF_ICON)
|
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.helpers.entity import Entity, generate_entity_id
|
||||||
from homeassistant.util.location import distance
|
from homeassistant.util.location import distance
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -90,60 +90,30 @@ def in_zone(zone, latitude, longitude, radius=0):
|
|||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup zone."""
|
"""Setup zone."""
|
||||||
entities = set()
|
entities = set()
|
||||||
for key in extract_domain_configs(config, DOMAIN):
|
for _, entry in config_per_platform(config, DOMAIN):
|
||||||
entries = config[key]
|
name = entry.get(CONF_NAME)
|
||||||
if not isinstance(entries, list):
|
zone = Zone(hass, name, entry[CONF_LATITUDE], entry[CONF_LONGITUDE],
|
||||||
entries = entries,
|
entry.get(CONF_RADIUS), entry.get(CONF_ICON),
|
||||||
|
entry.get(CONF_PASSIVE))
|
||||||
for entry in entries:
|
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, entities)
|
||||||
name = entry.get(CONF_NAME)
|
zone.update_ha_state()
|
||||||
latitude = entry.get(CONF_LATITUDE)
|
entities.add(zone.entity_id)
|
||||||
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)
|
|
||||||
|
|
||||||
if ENTITY_ID_HOME not in entities:
|
if ENTITY_ID_HOME not in entities:
|
||||||
zone = Zone(hass, hass.config.location_name,
|
zone = Zone(hass, hass.config.location_name,
|
||||||
hass.config.latitude, hass.config.longitude,
|
hass.config.latitude, hass.config.longitude,
|
||||||
DEFAULT_RADIUS, ICON_HOME, False, False)
|
DEFAULT_RADIUS, ICON_HOME, False)
|
||||||
add_zone(hass, hass.config.location_name, zone, entities)
|
|
||||||
zone.entity_id = ENTITY_ID_HOME
|
zone.entity_id = ENTITY_ID_HOME
|
||||||
zone.update_ha_state()
|
zone.update_ha_state()
|
||||||
|
|
||||||
return True
|
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):
|
class Zone(Entity):
|
||||||
"""Representation of a Zone."""
|
"""Representation of a Zone."""
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
||||||
def __init__(self, hass, name, latitude, longitude, radius, icon, passive,
|
def __init__(self, hass, name, latitude, longitude, radius, icon, passive):
|
||||||
imported):
|
|
||||||
"""Initialize the zone."""
|
"""Initialize the zone."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
@ -152,7 +122,6 @@ class Zone(Entity):
|
|||||||
self._radius = radius
|
self._radius = radius
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._passive = passive
|
self._passive = passive
|
||||||
self._imported = imported
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -18,6 +18,18 @@ class TestComponentZone(unittest.TestCase):
|
|||||||
"""Stop down everything that was started."""
|
"""Stop down everything that was started."""
|
||||||
self.hass.stop()
|
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):
|
def test_setup(self):
|
||||||
"""Test setup."""
|
"""Test setup."""
|
||||||
info = {
|
info = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user