Skip to content
Permalink
Browse files

Remove loader.get_component (#23111)

* Remove get_component

* Lint
  • Loading branch information...
balloob authored and awarecan committed Apr 15, 2019
1 parent 23cb579 commit b0d893afc9bcdef0f761a3328d1c209a5f52dc19
@@ -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()


@@ -159,6 +165,7 @@ def __repr__(self) -> str:
Integration.resolve_from_root, hass, custom_components, domain
)
if integration is not None:
_LOGGER.warning(CUSTOM_WARNING, domain)
cache[domain] = integration
return integration

@@ -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]:
@@ -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

@@ -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)
@@ -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',
@@ -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,
@@ -191,7 +190,7 @@ def yaml_devices(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')

@@ -255,7 +254,7 @@ def yaml_devices(hass):
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):
@@ -274,7 +273,7 @@ def yaml_devices(hass):
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):
@@ -440,7 +439,7 @@ def listener(event):
'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')

@@ -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')

@@ -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 (
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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'}})
@@ -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 (
@@ -98,7 +97,7 @@ def setup_comp_1(hass):

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': {
@@ -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 (
@@ -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,
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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

@@ -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, {

0 comments on commit b0d893a

Please sign in to comment.
You can’t perform that action at this time.