mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Merge pull request #3503 from home-assistant/fix-platform-component-no-config
Allow platform components without config
This commit is contained in:
commit
b628fb088b
@ -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."""
|
||||
|
@ -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):
|
||||
|
@ -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:
|
||||
|
@ -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 = {
|
||||
|
@ -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'))
|
||||
|
@ -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')
|
||||
|
Loading…
x
Reference in New Issue
Block a user