Skip to content

Commit

Permalink
Remove loader.get_component (#23111)
Browse files Browse the repository at this point in the history
* Remove get_component

* Lint
  • Loading branch information
balloob authored and awarecan committed Apr 15, 2019
1 parent 23cb579 commit b0d893a
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 84 deletions.
28 changes: 8 additions & 20 deletions homeassistant/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
PACKAGE_CUSTOM_COMPONENTS = 'custom_components'
PACKAGE_BUILTIN = 'homeassistant.components'
LOOKUP_PATHS = [PACKAGE_CUSTOM_COMPONENTS, PACKAGE_BUILTIN]
CUSTOM_WARNING = (
'You are using a custom integration for %s which has not '
'been tested by Home Assistant. This component might '
'cause stability problems, be sure to disable it if you '
'do experience issues with Home Assistant.'
)
_UNDEF = object()


Expand Down Expand Up @@ -159,6 +165,7 @@ async def async_get_integration(hass: 'HomeAssistant', domain: str)\
Integration.resolve_from_root, hass, custom_components, domain
)
if integration is not None:
_LOGGER.warning(CUSTOM_WARNING, domain)
cache[domain] = integration
return integration

Expand Down Expand Up @@ -210,20 +217,6 @@ def __init__(self, from_domain: str, to_domain: str) -> None:
self.to_domain = to_domain


def get_component(hass, # type: HomeAssistant
comp_or_platform: str) -> Optional[ModuleType]:
"""Try to load specified component.
Async friendly.
"""
comp = _load_file(hass, comp_or_platform, LOOKUP_PATHS)

if comp is None:
_LOGGER.error("Unable to find component %s", comp_or_platform)

return comp


def _load_file(hass, # type: HomeAssistant
comp_or_platform: str,
base_paths: List[str]) -> Optional[ModuleType]:
Expand Down Expand Up @@ -266,12 +259,7 @@ def _load_file(hass, # type: HomeAssistant
cache[comp_or_platform] = module

if module.__name__.startswith(PACKAGE_CUSTOM_COMPONENTS):
_LOGGER.warning(
'You are using a custom component for %s which has not '
'been tested by Home Assistant. This component might '
'cause stability problems, be sure to disable it if you '
'do experience issues with Home Assistant.',
comp_or_platform)
_LOGGER.warning(CUSTOM_WARNING, comp_or_platform)

return module

Expand Down
7 changes: 3 additions & 4 deletions tests/components/device_sun_light_trigger/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

from homeassistant.setup import async_setup_component
import homeassistant.loader as loader
from homeassistant.const import CONF_PLATFORM, STATE_HOME, STATE_NOT_HOME
from homeassistant.components import (
device_tracker, light, device_sun_light_trigger)
Expand All @@ -18,13 +17,13 @@
@pytest.fixture
def scanner(hass):
"""Initialize components."""
scanner = loader.get_component(
hass, 'test.device_tracker').get_scanner(None, None)
scanner = getattr(
hass.components, 'test.device_tracker').get_scanner(None, None)

scanner.reset()
scanner.come_home('DEV1')

loader.get_component(hass, 'test.light').init()
getattr(hass.components, 'test.light').init()

with patch(
'homeassistant.components.device_tracker.load_yaml_config_file',
Expand Down
11 changes: 5 additions & 6 deletions tests/components/device_tracker/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from homeassistant.core import callback, State
from homeassistant.setup import async_setup_component
from homeassistant.helpers import discovery
from homeassistant.loader import get_component
import homeassistant.util.dt as dt_util
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME, ATTR_HIDDEN,
Expand Down Expand Up @@ -191,7 +190,7 @@ async def test_discover_platform(mock_demo_setup_scanner, mock_see, hass):

async def test_update_stale(hass):
"""Test stalled update."""
scanner = get_component(hass, 'test.device_tracker').SCANNER
scanner = getattr(hass.components, 'test.device_tracker').SCANNER
scanner.reset()
scanner.come_home('DEV1')

Expand Down Expand Up @@ -255,7 +254,7 @@ async def test_device_hidden(hass, yaml_devices):
hide_if_away=True)
device_tracker.update_config(yaml_devices, dev_id, device)

scanner = get_component(hass, 'test.device_tracker').SCANNER
scanner = getattr(hass.components, 'test.device_tracker').SCANNER
scanner.reset()

with assert_setup_component(1, device_tracker.DOMAIN):
Expand All @@ -274,7 +273,7 @@ async def test_group_all_devices(hass, yaml_devices):
hide_if_away=True)
device_tracker.update_config(yaml_devices, dev_id, device)

scanner = get_component(hass, 'test.device_tracker').SCANNER
scanner = getattr(hass.components, 'test.device_tracker').SCANNER
scanner.reset()

with assert_setup_component(1, device_tracker.DOMAIN):
Expand Down Expand Up @@ -440,7 +439,7 @@ async def test_see_passive_zone_state(hass):
'zone': zone_info
})

scanner = get_component(hass, 'test.device_tracker').SCANNER
scanner = getattr(hass.components, 'test.device_tracker').SCANNER
scanner.reset()
scanner.come_home('dev1')

Expand Down Expand Up @@ -556,7 +555,7 @@ def test_bad_platform(hass):

async def test_adding_unknown_device_to_config(mock_device_tracker_conf, hass):
"""Test the adding of unknown devices to configuration file."""
scanner = get_component(hass, 'test.device_tracker').SCANNER
scanner = getattr(hass.components, 'test.device_tracker').SCANNER
scanner.reset()
scanner.come_home('DEV1')

Expand Down
33 changes: 16 additions & 17 deletions tests/components/flux/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from homeassistant.components import switch, light
from homeassistant.const import (
CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON, SUN_EVENT_SUNRISE)
import homeassistant.loader as loader
import homeassistant.util.dt as dt_util

from tests.common import (
Expand Down Expand Up @@ -74,7 +73,7 @@ def test_invalid_config_no_lights(self):

def test_flux_when_switch_is_off(self):
"""Test the flux switch when it is off."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -114,7 +113,7 @@ def event_date(hass, event, now=None):

def test_flux_before_sunrise(self):
"""Test the flux switch before sunrise."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -159,7 +158,7 @@ def event_date(hass, event, now=None):
# pylint: disable=invalid-name
def test_flux_after_sunrise_before_sunset(self):
"""Test the flux switch after sunrise and before sunset."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -205,7 +204,7 @@ def event_date(hass, event, now=None):
# pylint: disable=invalid-name
def test_flux_after_sunset_before_stop(self):
"""Test the flux switch after sunset and before stop."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -252,7 +251,7 @@ def event_date(hass, event, now=None):
# pylint: disable=invalid-name
def test_flux_after_stop_before_sunrise(self):
"""Test the flux switch after stop and before sunrise."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -297,7 +296,7 @@ def event_date(hass, event, now=None):
# pylint: disable=invalid-name
def test_flux_with_custom_start_stop_times(self):
"""Test the flux with custom start and stop times."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -347,7 +346,7 @@ def test_flux_before_sunrise_stop_next_day(self):
This test has the stop_time on the next day (after midnight).
"""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -398,7 +397,7 @@ def test_flux_after_sunrise_before_sunset_stop_next_day(self):
This test has the stop_time on the next day (after midnight).
"""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -448,7 +447,7 @@ def test_flux_after_sunset_before_midnight_stop_next_day(self):
This test has the stop_time on the next day (after midnight).
"""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -497,7 +496,7 @@ def test_flux_after_sunset_after_midnight_stop_next_day(self):
This test has the stop_time on the next day (after midnight).
"""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -547,7 +546,7 @@ def test_flux_after_stop_before_sunrise_stop_next_day(self):
This test has the stop_time on the next day (after midnight).
"""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -594,7 +593,7 @@ def event_date(hass, event, now=None):
# pylint: disable=invalid-name
def test_flux_with_custom_colortemps(self):
"""Test the flux with custom start and stop colortemps."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -643,7 +642,7 @@ def event_date(hass, event, now=None):
# pylint: disable=invalid-name
def test_flux_with_custom_brightness(self):
"""Test the flux with custom start and stop colortemps."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -690,7 +689,7 @@ def event_date(hass, event, now=None):

def test_flux_with_multiple_lights(self):
"""Test the flux switch with multiple light entities."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -758,7 +757,7 @@ def event_date(hass, event, now=None):

def test_flux_with_mired(self):
"""Test the flux switch´s mode mired."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down Expand Up @@ -802,7 +801,7 @@ def event_date(hass, event, now=None):

def test_flux_with_rgb(self):
"""Test the flux switch´s mode rgb."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
Expand Down
3 changes: 1 addition & 2 deletions tests/components/generic_thermostat/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
TEMP_FAHRENHEIT,
ATTR_TEMPERATURE
)
from homeassistant import loader
from homeassistant.util.unit_system import METRIC_SYSTEM
from homeassistant.components import input_boolean, switch
from homeassistant.components.climate.const import (
Expand Down Expand Up @@ -98,7 +97,7 @@ async def test_heater_input_boolean(hass, setup_comp_1):

async def test_heater_switch(hass, setup_comp_1):
"""Test heater switching test switch."""
platform = loader.get_component(hass, 'test.switch')
platform = getattr(hass.components, 'test.switch')
platform.init()
switch_1 = platform.DEVICES[1]
assert await async_setup_component(hass, switch.DOMAIN, {'switch': {
Expand Down
12 changes: 6 additions & 6 deletions tests/components/light/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from homeassistant import core, loader
from homeassistant import core
from homeassistant.exceptions import Unauthorized
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.const import (
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_methods(self):

def test_services(self):
"""Test the provided services."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')

platform.init()
assert setup_component(self.hass, light.DOMAIN,
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_services(self):

def test_broken_light_profiles(self):
"""Test light profiles."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()

user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
Expand All @@ -323,7 +323,7 @@ def test_broken_light_profiles(self):

def test_light_profiles(self):
"""Test light profiles."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()

user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
Expand Down Expand Up @@ -362,7 +362,7 @@ def test_light_profiles(self):

def test_default_profiles_group(self):
"""Test default turn-on light profile for all lights."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()

user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
Expand Down Expand Up @@ -400,7 +400,7 @@ def _mock_open(path, *args, **kwargs):

def test_default_profiles_light(self):
"""Test default turn-on light profile for a specific light."""
platform = loader.get_component(self.hass, 'test.light')
platform = getattr(self.hass.components, 'test.light')
platform.init()

user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
Expand Down
3 changes: 1 addition & 2 deletions tests/components/scene/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest

from homeassistant.setup import setup_component
from homeassistant import loader
from homeassistant.components import light, scene
from homeassistant.util import yaml

Expand All @@ -18,7 +17,7 @@ class TestScene(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
test_light = loader.get_component(self.hass, 'test.light')
test_light = getattr(self.hass.components, 'test.light')
test_light.init()

assert setup_component(self.hass, light.DOMAIN, {
Expand Down
Loading

0 comments on commit b0d893a

Please sign in to comment.