diff --git a/homeassistant/components/mqtt/alarm_control_panel.py b/homeassistant/components/mqtt/alarm_control_panel.py index 21234fbc5b4..4c06f6af7c3 100644 --- a/homeassistant/components/mqtt/alarm_control_panel.py +++ b/homeassistant/components/mqtt/alarm_control_panel.py @@ -1,4 +1,5 @@ """This platform enables the possibility to control a MQTT alarm.""" +import functools import logging import re @@ -29,10 +30,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -46,9 +43,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -57,6 +52,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -112,35 +108,20 @@ async def async_setup_platform( ): """Set up MQTT alarm control panel through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT alarm control panel dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add an MQTT alarm control panel.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(alarm.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, alarm.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT Alarm Control Panel platform.""" async_add_entities([MqttAlarm(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/binary_sensor.py b/homeassistant/components/mqtt/binary_sensor.py index c604181bdad..21d7740442c 100644 --- a/homeassistant/components/mqtt/binary_sensor.py +++ b/homeassistant/components/mqtt/binary_sensor.py @@ -1,5 +1,6 @@ """Support for MQTT binary sensors.""" from datetime import timedelta +import functools import logging import voluptuous as vol @@ -21,10 +22,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) import homeassistant.helpers.event as evt from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.helpers.reload import async_setup_reload_service @@ -33,9 +30,7 @@ from homeassistant.util import dt as dt_util from . import CONF_QOS, CONF_STATE_TOPIC, DOMAIN, PLATFORMS, subscription from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -44,6 +39,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -79,35 +75,20 @@ async def async_setup_platform( ): """Set up MQTT binary sensor through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT binary sensor dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT binary sensor.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(binary_sensor.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, binary_sensor.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT binary sensor.""" async_add_entities([MqttBinarySensor(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/camera.py b/homeassistant/components/mqtt/camera.py index 3888fcd9663..5600dd8a1e3 100644 --- a/homeassistant/components/mqtt/camera.py +++ b/homeassistant/components/mqtt/camera.py @@ -1,4 +1,5 @@ """Camera that loads a picture from an MQTT topic.""" +import functools import logging import voluptuous as vol @@ -8,18 +9,12 @@ from homeassistant.components.camera import Camera from homeassistant.const import CONF_DEVICE, CONF_NAME, CONF_UNIQUE_ID from homeassistant.core import callback from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType from . import CONF_QOS, DOMAIN, PLATFORMS, subscription from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -28,6 +23,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -54,35 +50,20 @@ async def async_setup_platform( ): """Set up MQTT camera through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(config, async_add_entities) + await _async_setup_entity(async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT camera dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT camera.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(camera.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, camera.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - config, async_add_entities, config_entry=None, discovery_data=None + async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT Camera.""" async_add_entities([MqttCamera(config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index 77503335ded..1c9bbd74bfb 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -1,4 +1,5 @@ """Support for MQTT climate devices.""" +import functools import logging import voluptuous as vol @@ -47,10 +48,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -63,9 +60,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -74,6 +69,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -248,7 +244,7 @@ PLATFORM_SCHEMA = ( async def async_setup_platform( - hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None + hass: HomeAssistantType, async_add_entities, config: ConfigType, discovery_info=None ): """Set up MQTT climate device through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) @@ -258,29 +254,14 @@ async def async_setup_platform( async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT climate device dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT climate device.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(climate.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, climate.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT climate devices.""" async_add_entities([MqttClimate(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index 6de560b52b1..498d6ffc8af 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -1,4 +1,5 @@ """Support for MQTT cover devices.""" +import functools import logging import voluptuous as vol @@ -33,10 +34,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -50,9 +47,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -61,6 +56,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -183,35 +179,20 @@ async def async_setup_platform( ): """Set up MQTT cover through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT cover dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add an MQTT cover.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(cover.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, cover.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT Cover.""" async_add_entities([MqttCover(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/device_automation.py b/homeassistant/components/mqtt/device_automation.py index 423690d8e69..d3e1f33421d 100644 --- a/homeassistant/components/mqtt/device_automation.py +++ b/homeassistant/components/mqtt/device_automation.py @@ -1,18 +1,14 @@ """Provides device automations for MQTT.""" +import functools import logging import voluptuous as vol from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from . import device_trigger from .. import mqtt -from .const import ATTR_DISCOVERY_HASH -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash +from .mixins import async_setup_entry_helper _LOGGER = logging.getLogger(__name__) @@ -36,24 +32,14 @@ async def async_setup_entry(hass, config_entry): return await device_trigger.async_device_removed(hass, event.data["device_id"]) - async def async_discover(discovery_payload): - """Discover and add an MQTT device automation.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - if config[CONF_AUTOMATION_TYPE] == AUTOMATION_TYPE_TRIGGER: - await device_trigger.async_setup_trigger( - hass, config, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format("device_automation", "mqtt"), async_discover - ) + setup = functools.partial(_async_setup_automation, hass, config_entry=config_entry) + await async_setup_entry_helper(hass, "device_automation", setup, PLATFORM_SCHEMA) hass.bus.async_listen(EVENT_DEVICE_REGISTRY_UPDATED, async_device_removed) + + +async def _async_setup_automation(hass, config, config_entry, discovery_data): + """Set up an MQTT device automation.""" + if config[CONF_AUTOMATION_TYPE] == AUTOMATION_TYPE_TRIGGER: + await device_trigger.async_setup_trigger( + hass, config, config_entry, discovery_data + ) diff --git a/homeassistant/components/mqtt/device_tracker/schema_discovery.py b/homeassistant/components/mqtt/device_tracker/schema_discovery.py index 9fa40c18037..8e6019eefd7 100644 --- a/homeassistant/components/mqtt/device_tracker/schema_discovery.py +++ b/homeassistant/components/mqtt/device_tracker/schema_discovery.py @@ -1,4 +1,5 @@ """Support for tracking MQTT enabled devices identified through discovery.""" +import functools import logging import voluptuous as vol @@ -20,16 +21,11 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from .. import subscription from ... import mqtt -from ..const import ATTR_DISCOVERY_HASH, CONF_QOS, CONF_STATE_TOPIC +from ..const import CONF_QOS, CONF_STATE_TOPIC from ..debug_info import log_messages -from ..discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from ..mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -38,6 +34,7 @@ from ..mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -66,29 +63,16 @@ PLATFORM_SCHEMA_DISCOVERY = ( async def async_setup_entry_from_discovery(hass, config_entry, async_add_entities): """Set up MQTT device tracker dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add an MQTT device tracker.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA_DISCOVERY(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(device_tracker.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry + ) + await async_setup_entry_helper( + hass, device_tracker.DOMAIN, setup, PLATFORM_SCHEMA_DISCOVERY ) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT Device Tracker entity.""" async_add_entities([MqttDeviceTracker(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/fan.py b/homeassistant/components/mqtt/fan.py index eb713655821..5d3abd7b793 100644 --- a/homeassistant/components/mqtt/fan.py +++ b/homeassistant/components/mqtt/fan.py @@ -1,4 +1,5 @@ """Support for MQTT fans.""" +import functools import logging import voluptuous as vol @@ -25,10 +26,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -42,9 +39,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -53,6 +48,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -124,35 +120,20 @@ async def async_setup_platform( ): """Set up MQTT fan through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT fan dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT fan.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(fan.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, fan.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT fan.""" async_add_entities([MqttFan(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/light/__init__.py b/homeassistant/components/mqtt/light/__init__.py index cfeaaa0d7c9..e780332d093 100644 --- a/homeassistant/components/mqtt/light/__init__.py +++ b/homeassistant/components/mqtt/light/__init__.py @@ -1,19 +1,15 @@ """Support for MQTT lights.""" +import functools import logging import voluptuous as vol from homeassistant.components import light -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType from .. import DOMAIN, PLATFORMS -from ..const import ATTR_DISCOVERY_HASH -from ..discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash +from ..mixins import async_setup_entry_helper from .schema import CONF_SCHEMA, MQTT_LIGHT_SCHEMA_SCHEMA from .schema_basic import PLATFORM_SCHEMA_BASIC, async_setup_entity_basic from .schema_json import PLATFORM_SCHEMA_JSON, async_setup_entity_json @@ -42,35 +38,20 @@ async def async_setup_platform( ): """Set up MQTT light through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT light dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT light.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(light.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, light.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up a MQTT Light.""" setup_entity = { diff --git a/homeassistant/components/mqtt/lock.py b/homeassistant/components/mqtt/lock.py index 72df487d22a..ef3ad6bb8eb 100644 --- a/homeassistant/components/mqtt/lock.py +++ b/homeassistant/components/mqtt/lock.py @@ -1,4 +1,5 @@ """Support for MQTT locks.""" +import functools import logging import voluptuous as vol @@ -14,10 +15,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -31,9 +28,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -42,6 +37,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -86,35 +82,20 @@ async def async_setup_platform( ): """Set up MQTT lock panel through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT lock dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add an MQTT lock.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(lock.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, lock.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT Lock platform.""" async_add_entities([MqttLock(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index c627a254791..42171ef4e34 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -30,6 +30,7 @@ from .const import ( from .debug_info import log_messages from .discovery import ( MQTT_DISCOVERY_DONE, + MQTT_DISCOVERY_NEW, MQTT_DISCOVERY_UPDATED, clear_discovery_hash, set_discovery_hash, @@ -130,6 +131,28 @@ MQTT_JSON_ATTRS_SCHEMA = vol.Schema( ) +async def async_setup_entry_helper(hass, domain, async_setup, schema): + """Set up entity, automation or tag creation dynamically through MQTT discovery.""" + + async def async_discover(discovery_payload): + """Discover and add an MQTT entity, automation or tag.""" + discovery_data = discovery_payload.discovery_data + try: + config = schema(discovery_payload) + await async_setup(config, discovery_data=discovery_data) + except Exception: + discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] + clear_discovery_hash(hass, discovery_hash) + async_dispatcher_send( + hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None + ) + raise + + async_dispatcher_connect( + hass, MQTT_DISCOVERY_NEW.format(domain, "mqtt"), async_discover + ) + + class MqttAttributes(Entity): """Mixin used for platforms that support JSON attributes.""" diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index 7196b348a6f..c844d888efe 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -1,4 +1,5 @@ """Configure number in a device through MQTT topic.""" +import functools import logging import voluptuous as vol @@ -14,10 +15,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -31,9 +28,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -42,6 +37,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -69,35 +65,20 @@ async def async_setup_platform( ): """Set up MQTT number through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(config, async_add_entities) + await _async_setup_entity(async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT number dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT number.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(number.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, number.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - config, async_add_entities, config_entry=None, discovery_data=None + async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT number.""" async_add_entities([MqttNumber(config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/scene.py b/homeassistant/components/mqtt/scene.py index 2923f512d02..908f4bafd30 100644 --- a/homeassistant/components/mqtt/scene.py +++ b/homeassistant/components/mqtt/scene.py @@ -1,4 +1,5 @@ """Support for MQTT scenes.""" +import functools import logging import voluptuous as vol @@ -7,18 +8,17 @@ from homeassistant.components import scene from homeassistant.components.scene import Scene from homeassistant.const import CONF_ICON, CONF_NAME, CONF_PAYLOAD_ON, CONF_UNIQUE_ID import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType, HomeAssistantType from . import CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN, DOMAIN, PLATFORMS from .. import mqtt -from .const import ATTR_DISCOVERY_HASH -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash -from .mixins import MQTT_AVAILABILITY_SCHEMA, MqttAvailability, MqttDiscoveryUpdate +from .mixins import ( + MQTT_AVAILABILITY_SCHEMA, + MqttAvailability, + MqttDiscoveryUpdate, + async_setup_entry_helper, +) _LOGGER = logging.getLogger(__name__) @@ -42,35 +42,20 @@ async def async_setup_platform( ): """Set up MQTT scene through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(config, async_add_entities) + await _async_setup_entity(async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT scene dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT scene.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(scene.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, scene.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - config, async_add_entities, config_entry=None, discovery_data=None + async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT scene.""" async_add_entities([MqttScene(config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/sensor.py b/homeassistant/components/mqtt/sensor.py index 5ddfc29d80f..159c4b1d118 100644 --- a/homeassistant/components/mqtt/sensor.py +++ b/homeassistant/components/mqtt/sensor.py @@ -1,5 +1,6 @@ """Support for MQTT sensors.""" from datetime import timedelta +import functools import logging from typing import Optional @@ -19,10 +20,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.entity import Entity from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.helpers.reload import async_setup_reload_service @@ -31,9 +28,7 @@ from homeassistant.util import dt as dt_util from . import CONF_QOS, CONF_STATE_TOPIC, DOMAIN, PLATFORMS, subscription from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -42,6 +37,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -73,35 +69,20 @@ async def async_setup_platform( ): """Set up MQTT sensors through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT sensors dynamically through MQTT discovery.""" - async def async_discover_sensor(discovery_payload): - """Discover and add a discovered MQTT sensor.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(sensor.DOMAIN, "mqtt"), async_discover_sensor + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, sensor.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config: ConfigType, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config: ConfigType, config_entry=None, discovery_data=None ): """Set up MQTT sensor.""" async_add_entities([MqttSensor(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/switch.py b/homeassistant/components/mqtt/switch.py index 35a436b0be8..a4e65354594 100644 --- a/homeassistant/components/mqtt/switch.py +++ b/homeassistant/components/mqtt/switch.py @@ -1,4 +1,5 @@ """Support for MQTT switches.""" +import functools import logging import voluptuous as vol @@ -18,10 +19,6 @@ from homeassistant.const import ( ) from homeassistant.core import callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import ConfigType, HomeAssistantType @@ -36,9 +33,7 @@ from . import ( subscription, ) from .. import mqtt -from .const import ATTR_DISCOVERY_HASH from .debug_info import log_messages -from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash from .mixins import ( MQTT_AVAILABILITY_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, @@ -47,6 +42,7 @@ from .mixins import ( MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, + async_setup_entry_helper, ) _LOGGER = logging.getLogger(__name__) @@ -82,35 +78,20 @@ async def async_setup_platform( ): """Set up MQTT switch through configuration.yaml.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - await _async_setup_entity(hass, config, async_add_entities) + await _async_setup_entity(hass, async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT switch dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT switch.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - hass, config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(switch.DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, hass, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, switch.DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - hass, config, async_add_entities, config_entry=None, discovery_data=None + hass, async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT switch.""" async_add_entities([MqttSwitch(hass, config, config_entry, discovery_data)]) diff --git a/homeassistant/components/mqtt/tag.py b/homeassistant/components/mqtt/tag.py index c4db7b5f4b9..b691c5cf8ce 100644 --- a/homeassistant/components/mqtt/tag.py +++ b/homeassistant/components/mqtt/tag.py @@ -1,4 +1,5 @@ """Provides tag scanning for MQTT.""" +import functools import logging import voluptuous as vol @@ -14,16 +15,12 @@ from homeassistant.helpers.dispatcher import ( from . import CONF_QOS, CONF_TOPIC, DOMAIN, subscription from .. import mqtt from .const import ATTR_DISCOVERY_HASH, ATTR_DISCOVERY_TOPIC -from .discovery import ( - MQTT_DISCOVERY_DONE, - MQTT_DISCOVERY_NEW, - MQTT_DISCOVERY_UPDATED, - clear_discovery_hash, -) +from .discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_UPDATED, clear_discovery_hash from .mixins import ( CONF_CONNECTIONS, CONF_IDENTIFIERS, MQTT_ENTITY_DEVICE_INFO_SCHEMA, + async_setup_entry_helper, cleanup_device_registry, device_info_from_config, validate_device_has_at_least_one_identifier, @@ -49,23 +46,8 @@ PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend( async def async_setup_entry(hass, config_entry): """Set up MQTT tag scan dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add MQTT tag scan.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await async_setup_tag(hass, config, config_entry, discovery_data) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format("tag", "mqtt"), async_discover - ) + setup = functools.partial(async_setup_tag, hass, config_entry=config_entry) + await async_setup_entry_helper(hass, "tag", setup, PLATFORM_SCHEMA) async def async_setup_tag(hass, config, config_entry, discovery_data): diff --git a/homeassistant/components/mqtt/vacuum/__init__.py b/homeassistant/components/mqtt/vacuum/__init__.py index 09f25eaf732..e580e874993 100644 --- a/homeassistant/components/mqtt/vacuum/__init__.py +++ b/homeassistant/components/mqtt/vacuum/__init__.py @@ -1,18 +1,14 @@ """Support for MQTT vacuums.""" +import functools import logging import voluptuous as vol from homeassistant.components.vacuum import DOMAIN -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) from homeassistant.helpers.reload import async_setup_reload_service from .. import DOMAIN as MQTT_DOMAIN, PLATFORMS -from ..const import ATTR_DISCOVERY_HASH -from ..discovery import MQTT_DISCOVERY_DONE, MQTT_DISCOVERY_NEW, clear_discovery_hash +from ..mixins import async_setup_entry_helper from .schema import CONF_SCHEMA, LEGACY, MQTT_VACUUM_SCHEMA, STATE from .schema_legacy import PLATFORM_SCHEMA_LEGACY, async_setup_entity_legacy from .schema_state import PLATFORM_SCHEMA_STATE, async_setup_entity_state @@ -34,35 +30,20 @@ PLATFORM_SCHEMA = vol.All( async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up MQTT vacuum through configuration.yaml.""" await async_setup_reload_service(hass, MQTT_DOMAIN, PLATFORMS) - await _async_setup_entity(config, async_add_entities) + await _async_setup_entity(async_add_entities, config) async def async_setup_entry(hass, config_entry, async_add_entities): """Set up MQTT vacuum dynamically through MQTT discovery.""" - async def async_discover(discovery_payload): - """Discover and add a MQTT vacuum.""" - discovery_data = discovery_payload.discovery_data - try: - config = PLATFORM_SCHEMA(discovery_payload) - await _async_setup_entity( - config, async_add_entities, config_entry, discovery_data - ) - except Exception: - discovery_hash = discovery_data[ATTR_DISCOVERY_HASH] - clear_discovery_hash(hass, discovery_hash) - async_dispatcher_send( - hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None - ) - raise - - async_dispatcher_connect( - hass, MQTT_DISCOVERY_NEW.format(DOMAIN, "mqtt"), async_discover + setup = functools.partial( + _async_setup_entity, async_add_entities, config_entry=config_entry ) + await async_setup_entry_helper(hass, DOMAIN, setup, PLATFORM_SCHEMA) async def _async_setup_entity( - config, async_add_entities, config_entry=None, discovery_data=None + async_add_entities, config, config_entry=None, discovery_data=None ): """Set up the MQTT vacuum.""" setup_entity = {LEGACY: async_setup_entity_legacy, STATE: async_setup_entity_state}