From e48ef7f44187587f8f65201117e02f8fd2382894 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 9 Apr 2019 15:42:44 -0700 Subject: [PATCH] Fix broken platform components (#22943) * Fix broken platform components * Lint --- .../components/automation/__init__.py | 8 ++----- homeassistant/components/mqtt/__init__.py | 22 ++++------------- homeassistant/components/mqtt/const.py | 3 +++ homeassistant/components/mqtt/discovery.py | 2 +- .../components/telegram_bot/__init__.py | 11 ++++----- .../components/telegram_bot/manifest.json | 2 +- homeassistant/loader.py | 24 ++++--------------- tests/components/automation/test_webhook.py | 9 +++++++ 8 files changed, 30 insertions(+), 51 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 94776deefb0..b1470582d59 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -18,7 +18,6 @@ from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.loader import bind_hass -from homeassistant.setup import async_prepare_setup_platform from homeassistant.util.dt import utcnow DOMAIN = 'automation' @@ -416,11 +415,8 @@ async def _async_process_trigger(hass, config, trigger_configs, name, action): } for conf in trigger_configs: - platform = await async_prepare_setup_platform( - hass, config, DOMAIN, conf.get(CONF_PLATFORM)) - - if platform is None: - return None + platform = importlib.import_module('.{}'.format(conf[CONF_PLATFORM]), + __name__) remove = await platform.async_trigger(hass, conf, action, info) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 9af0465c0de..4f9ad990105 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -30,15 +30,15 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import ( ConfigType, HomeAssistantType, ServiceDataType) from homeassistant.loader import bind_hass -from homeassistant.setup import async_prepare_setup_platform from homeassistant.util.async_ import ( run_callback_threadsafe, run_coroutine_threadsafe) from homeassistant.util.logging import catch_log_exception # Loading the config flow file will register the flow -from . import config_flow # noqa pylint: disable=unused-import -from .const import CONF_BROKER, CONF_DISCOVERY, DEFAULT_DISCOVERY -from .server import HBMQTT_CONFIG_SCHEMA +from . import config_flow, discovery, server # noqa pylint: disable=unused-import +from .const import ( + CONF_BROKER, CONF_DISCOVERY, DEFAULT_DISCOVERY, CONF_STATE_TOPIC, + ATTR_DISCOVERY_HASH) REQUIREMENTS = ['paho-mqtt==1.4.0'] @@ -66,7 +66,6 @@ CONF_TLS_VERSION = 'tls_version' CONF_BIRTH_MESSAGE = 'birth_message' CONF_WILL_MESSAGE = 'will_message' -CONF_STATE_TOPIC = 'state_topic' CONF_COMMAND_TOPIC = 'command_topic' CONF_AVAILABILITY_TOPIC = 'availability_topic' CONF_PAYLOAD_AVAILABLE = 'payload_available' @@ -101,7 +100,6 @@ ATTR_PAYLOAD = 'payload' ATTR_PAYLOAD_TEMPLATE = 'payload_template' ATTR_QOS = CONF_QOS ATTR_RETAIN = CONF_RETAIN -ATTR_DISCOVERY_HASH = 'discovery_hash' MAX_RECONNECT_WAIT = 300 # seconds @@ -209,7 +207,7 @@ CONFIG_SCHEMA = vol.Schema({ vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL): vol.All(cv.string, vol.In([PROTOCOL_31, PROTOCOL_311])), vol.Optional(CONF_EMBEDDED): - vol.All(HBMQTT_CONFIG_SCHEMA, embedded_broker_deprecated), + vol.All(server.HBMQTT_CONFIG_SCHEMA, embedded_broker_deprecated), vol.Optional(CONF_WILL_MESSAGE): MQTT_WILL_BIRTH_SCHEMA, vol.Optional(CONF_BIRTH_MESSAGE): MQTT_WILL_BIRTH_SCHEMA, vol.Optional(CONF_DISCOVERY, default=DEFAULT_DISCOVERY): cv.boolean, @@ -408,13 +406,6 @@ async def _async_setup_server(hass: HomeAssistantType, config: ConfigType): """ conf = config.get(DOMAIN, {}) # type: ConfigType - server = await async_prepare_setup_platform( - hass, config, DOMAIN, 'server') - - if server is None: - _LOGGER.error("Unable to load embedded server") - return None - success, broker_config = \ await server.async_start( hass, conf.get(CONF_PASSWORD), conf.get(CONF_EMBEDDED)) @@ -432,9 +423,6 @@ async def _async_setup_discovery(hass: HomeAssistantType, conf: ConfigType, This method is a coroutine. """ - discovery = await async_prepare_setup_platform( - hass, hass_config, DOMAIN, 'discovery') - if discovery is None: _LOGGER.error("Unable to load MQTT discovery") return False diff --git a/homeassistant/components/mqtt/const.py b/homeassistant/components/mqtt/const.py index 3c22001f91c..42b1a5b6755 100644 --- a/homeassistant/components/mqtt/const.py +++ b/homeassistant/components/mqtt/const.py @@ -2,3 +2,6 @@ CONF_BROKER = 'broker' CONF_DISCOVERY = 'discovery' DEFAULT_DISCOVERY = False + +ATTR_DISCOVERY_HASH = 'discovery_hash' +CONF_STATE_TOPIC = 'state_topic' diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index b10e05cbf0f..20b707eec17 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -10,7 +10,7 @@ from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.typing import HomeAssistantType -from . import ATTR_DISCOVERY_HASH, CONF_STATE_TOPIC +from .const import ATTR_DISCOVERY_HASH, CONF_STATE_TOPIC _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index e8b19ec2b2c..7d19e8212b6 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -1,6 +1,7 @@ """Support to send and receive Telegram messages.""" import io from functools import partial +import importlib import logging import requests @@ -14,7 +15,6 @@ from homeassistant.const import ( CONF_PLATFORM, CONF_TIMEOUT, HTTP_DIGEST_AUTHENTICATION) import homeassistant.helpers.config_validation as cv from homeassistant.exceptions import TemplateError -from homeassistant.setup import async_prepare_setup_platform REQUIREMENTS = ['python-telegram-bot==11.1.0'] @@ -76,7 +76,7 @@ PARSER_HTML = 'html' PARSER_MD = 'markdown' PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ - vol.Required(CONF_PLATFORM): cv.string, + vol.Required(CONF_PLATFORM): vol.In(('broadcast', 'polling', 'webhooks')), vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_ALLOWED_CHAT_IDS): vol.All(cv.ensure_list, [vol.Coerce(int)]), @@ -219,11 +219,8 @@ async def async_setup(hass, config): p_type = p_config.get(CONF_PLATFORM) - platform = await async_prepare_setup_platform( - hass, config, DOMAIN, p_type) - - if platform is None: - return + platform = importlib.import_module('.{}'.format(config[CONF_PLATFORM]), + __name__) _LOGGER.info("Setting up %s.%s", DOMAIN, p_type) try: diff --git a/homeassistant/components/telegram_bot/manifest.json b/homeassistant/components/telegram_bot/manifest.json index ba52cd4e935..f341fd587ca 100644 --- a/homeassistant/components/telegram_bot/manifest.json +++ b/homeassistant/components/telegram_bot/manifest.json @@ -5,6 +5,6 @@ "requirements": [ "python-telegram-bot==11.1.0" ], - "dependencies": [], + "dependencies": ["http"], "codeowners": [] } diff --git a/homeassistant/loader.py b/homeassistant/loader.py index a1dbad3439e..17f0130da4d 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -49,7 +49,6 @@ DATA_INTEGRATIONS = 'integrations' PACKAGE_CUSTOM_COMPONENTS = 'custom_components' PACKAGE_BUILTIN = 'homeassistant.components' LOOKUP_PATHS = [PACKAGE_CUSTOM_COMPONENTS, PACKAGE_BUILTIN] -COMPONENTS_WITH_BAD_PLATFORMS = ['automation', 'mqtt', 'telegram_bot'] _UNDEF = object() @@ -224,11 +223,7 @@ def get_platform(hass, # type: HomeAssistant """ # If the platform has a component, we will limit the platform loading path # to be the same source (custom/built-in). - if domain not in COMPONENTS_WITH_BAD_PLATFORMS: - component = _load_file(hass, platform_name, LOOKUP_PATHS) - else: - # avoid load component for legacy platform - component = None + component = _load_file(hass, platform_name, LOOKUP_PATHS) # Until we have moved all platforms under their component/own folder, it # can be that the component is None. @@ -244,14 +239,6 @@ def get_platform(hass, # type: HomeAssistant if platform is not None: return platform - # Legacy platform check for automation: components/automation/event.py - if component is None and domain in COMPONENTS_WITH_BAD_PLATFORMS: - platform = _load_file( - hass, - PLATFORM_FORMAT.format(domain=platform_name, platform=domain), - base_paths - ) - # Legacy platform check for custom: custom_components/light/hue.py # Only check if the component was also in custom components. if component is None or base_paths[0] == PACKAGE_CUSTOM_COMPONENTS: @@ -270,11 +257,10 @@ def get_platform(hass, # type: HomeAssistant _LOGGER.error("Unable to find platform %s.%s", platform_name, extra) return None - if domain not in COMPONENTS_WITH_BAD_PLATFORMS: - _LOGGER.error( - "Integrations need to be in their own folder. Change %s/%s.py to " - "%s/%s.py. This will stop working soon.", - domain, platform_name, platform_name, domain) + _LOGGER.error( + "Integrations need to be in their own folder. Change %s/%s.py to " + "%s/%s.py. This will stop working soon.", + domain, platform_name, platform_name, domain) return platform diff --git a/tests/components/automation/test_webhook.py b/tests/components/automation/test_webhook.py index a6cde395583..c42f3805662 100644 --- a/tests/components/automation/test_webhook.py +++ b/tests/components/automation/test_webhook.py @@ -1,8 +1,17 @@ """The tests for the webhook automation trigger.""" +import pytest + from homeassistant.core import callback from homeassistant.setup import async_setup_component +@pytest.fixture(autouse=True) +async def setup_http(hass): + """Set up http.""" + assert await async_setup_component(hass, 'http', {}) + assert await async_setup_component(hass, 'webhook', {}) + + async def test_webhook_json(hass, aiohttp_client): """Test triggering with a JSON webhook.""" events = []