Deduplicate MQTT entity discovery code (#44970)
This commit is contained in:
parent
2d9eb25142
commit
6dd6d9b368
17 changed files with 145 additions and 413 deletions
|
@ -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)])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue