mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Allow MQTT discovery (#16842)
This commit is contained in:
parent
f4974f58fe
commit
e4898bb05c
@ -10,6 +10,7 @@
|
|||||||
"broker": {
|
"broker": {
|
||||||
"data": {
|
"data": {
|
||||||
"broker": "Broker",
|
"broker": "Broker",
|
||||||
|
"discovery": "Enable discovery",
|
||||||
"password": "Password",
|
"password": "Password",
|
||||||
"port": "Port",
|
"port": "Port",
|
||||||
"username": "Username"
|
"username": "Username"
|
||||||
|
@ -36,7 +36,7 @@ from homeassistant.util.async_ import (
|
|||||||
|
|
||||||
# Loading the config flow file will register the flow
|
# Loading the config flow file will register the flow
|
||||||
from . import config_flow # noqa # pylint: disable=unused-import
|
from . import config_flow # noqa # pylint: disable=unused-import
|
||||||
from .const import CONF_BROKER
|
from .const import CONF_BROKER, CONF_DISCOVERY, DEFAULT_DISCOVERY
|
||||||
from .server import HBMQTT_CONFIG_SCHEMA
|
from .server import HBMQTT_CONFIG_SCHEMA
|
||||||
|
|
||||||
REQUIREMENTS = ['paho-mqtt==1.4.0']
|
REQUIREMENTS = ['paho-mqtt==1.4.0']
|
||||||
@ -47,13 +47,13 @@ DOMAIN = 'mqtt'
|
|||||||
|
|
||||||
DATA_MQTT = 'mqtt'
|
DATA_MQTT = 'mqtt'
|
||||||
DATA_MQTT_CONFIG = 'mqtt_config'
|
DATA_MQTT_CONFIG = 'mqtt_config'
|
||||||
|
DATA_MQTT_HASS_CONFIG = 'mqtt_hass_config'
|
||||||
|
|
||||||
SERVICE_PUBLISH = 'publish'
|
SERVICE_PUBLISH = 'publish'
|
||||||
|
|
||||||
CONF_EMBEDDED = 'embedded'
|
CONF_EMBEDDED = 'embedded'
|
||||||
|
|
||||||
CONF_CLIENT_ID = 'client_id'
|
CONF_CLIENT_ID = 'client_id'
|
||||||
CONF_DISCOVERY = 'discovery'
|
|
||||||
CONF_DISCOVERY_PREFIX = 'discovery_prefix'
|
CONF_DISCOVERY_PREFIX = 'discovery_prefix'
|
||||||
CONF_KEEPALIVE = 'keepalive'
|
CONF_KEEPALIVE = 'keepalive'
|
||||||
CONF_CERTIFICATE = 'certificate'
|
CONF_CERTIFICATE = 'certificate'
|
||||||
@ -81,7 +81,6 @@ DEFAULT_KEEPALIVE = 60
|
|||||||
DEFAULT_QOS = 0
|
DEFAULT_QOS = 0
|
||||||
DEFAULT_RETAIN = False
|
DEFAULT_RETAIN = False
|
||||||
DEFAULT_PROTOCOL = PROTOCOL_311
|
DEFAULT_PROTOCOL = PROTOCOL_311
|
||||||
DEFAULT_DISCOVERY = False
|
|
||||||
DEFAULT_DISCOVERY_PREFIX = 'homeassistant'
|
DEFAULT_DISCOVERY_PREFIX = 'homeassistant'
|
||||||
DEFAULT_TLS_PROTOCOL = 'auto'
|
DEFAULT_TLS_PROTOCOL = 'auto'
|
||||||
DEFAULT_PAYLOAD_AVAILABLE = 'online'
|
DEFAULT_PAYLOAD_AVAILABLE = 'online'
|
||||||
@ -321,23 +320,21 @@ async def _async_setup_server(hass: HomeAssistantType, config: ConfigType):
|
|||||||
return broker_config
|
return broker_config
|
||||||
|
|
||||||
|
|
||||||
async def _async_setup_discovery(hass: HomeAssistantType,
|
async def _async_setup_discovery(hass: HomeAssistantType, conf: ConfigType,
|
||||||
config: ConfigType) -> bool:
|
hass_config: ConfigType) -> bool:
|
||||||
"""Try to start the discovery of MQTT devices.
|
"""Try to start the discovery of MQTT devices.
|
||||||
|
|
||||||
This method is a coroutine.
|
This method is a coroutine.
|
||||||
"""
|
"""
|
||||||
conf = config.get(DOMAIN, {}) # type: ConfigType
|
|
||||||
|
|
||||||
discovery = await async_prepare_setup_platform(
|
discovery = await async_prepare_setup_platform(
|
||||||
hass, config, DOMAIN, 'discovery')
|
hass, hass_config, DOMAIN, 'discovery')
|
||||||
|
|
||||||
if discovery is None:
|
if discovery is None:
|
||||||
_LOGGER.error("Unable to load MQTT discovery")
|
_LOGGER.error("Unable to load MQTT discovery")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
success = await discovery.async_start(
|
success = await discovery.async_start(
|
||||||
hass, conf[CONF_DISCOVERY_PREFIX], config) # type: bool
|
hass, conf[CONF_DISCOVERY_PREFIX], hass_config) # type: bool
|
||||||
|
|
||||||
return success
|
return success
|
||||||
|
|
||||||
@ -346,6 +343,11 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
|||||||
"""Start the MQTT protocol service."""
|
"""Start the MQTT protocol service."""
|
||||||
conf = config.get(DOMAIN) # type: Optional[ConfigType]
|
conf = config.get(DOMAIN) # type: Optional[ConfigType]
|
||||||
|
|
||||||
|
# We need this because discovery can cause components to be set up and
|
||||||
|
# otherwise it will not load the users config.
|
||||||
|
# This needs a better solution.
|
||||||
|
hass.data[DATA_MQTT_HASS_CONFIG] = config
|
||||||
|
|
||||||
if conf is None:
|
if conf is None:
|
||||||
# If we have a config entry, setup is done by that config entry.
|
# If we have a config entry, setup is done by that config entry.
|
||||||
# If there is no config entry, this should fail.
|
# If there is no config entry, this should fail.
|
||||||
@ -390,13 +392,6 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
|||||||
data={}
|
data={}
|
||||||
))
|
))
|
||||||
|
|
||||||
if conf.get(CONF_DISCOVERY):
|
|
||||||
async def async_setup_discovery(event):
|
|
||||||
await _async_setup_discovery(hass, config)
|
|
||||||
|
|
||||||
hass.bus.async_listen_once(
|
|
||||||
EVENT_HOMEASSISTANT_START, async_setup_discovery)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -528,6 +523,14 @@ async def async_setup_entry(hass, entry):
|
|||||||
DOMAIN, SERVICE_PUBLISH, async_publish_service,
|
DOMAIN, SERVICE_PUBLISH, async_publish_service,
|
||||||
schema=MQTT_PUBLISH_SCHEMA)
|
schema=MQTT_PUBLISH_SCHEMA)
|
||||||
|
|
||||||
|
if conf.get(CONF_DISCOVERY):
|
||||||
|
async def async_setup_discovery(event):
|
||||||
|
await _async_setup_discovery(
|
||||||
|
hass, conf, hass.data[DATA_MQTT_HASS_CONFIG])
|
||||||
|
|
||||||
|
hass.bus.async_listen_once(
|
||||||
|
EVENT_HOMEASSISTANT_START, async_setup_discovery)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import voluptuous as vol
|
|||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
||||||
|
|
||||||
from .const import CONF_BROKER
|
from .const import CONF_BROKER, CONF_DISCOVERY, DEFAULT_DISCOVERY
|
||||||
|
|
||||||
|
|
||||||
@config_entries.HANDLERS.register('mqtt')
|
@config_entries.HANDLERS.register('mqtt')
|
||||||
@ -44,6 +44,7 @@ class FlowHandler(config_entries.ConfigFlow):
|
|||||||
fields[vol.Required(CONF_PORT, default=1883)] = vol.Coerce(int)
|
fields[vol.Required(CONF_PORT, default=1883)] = vol.Coerce(int)
|
||||||
fields[vol.Optional(CONF_USERNAME)] = str
|
fields[vol.Optional(CONF_USERNAME)] = str
|
||||||
fields[vol.Optional(CONF_PASSWORD)] = str
|
fields[vol.Optional(CONF_PASSWORD)] = str
|
||||||
|
fields[vol.Optional(CONF_DISCOVERY, default=DEFAULT_DISCOVERY)] = bool
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id='broker', data_schema=vol.Schema(fields), errors=errors)
|
step_id='broker', data_schema=vol.Schema(fields), errors=errors)
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
"""Constants used by multiple MQTT modules."""
|
"""Constants used by multiple MQTT modules."""
|
||||||
CONF_BROKER = 'broker'
|
CONF_BROKER = 'broker'
|
||||||
|
CONF_DISCOVERY = 'discovery'
|
||||||
|
DEFAULT_DISCOVERY = False
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
"broker": "Broker",
|
"broker": "Broker",
|
||||||
"port": "Port",
|
"port": "Port",
|
||||||
"username": "Username",
|
"username": "Username",
|
||||||
"password": "Password"
|
"password": "Password",
|
||||||
|
"discovery": "Enable discovery"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -41,6 +41,11 @@ async def test_user_connection_works(hass, mock_try_connection,
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result['type'] == 'create_entry'
|
assert result['type'] == 'create_entry'
|
||||||
|
assert result['result'].data == {
|
||||||
|
'broker': '127.0.0.1',
|
||||||
|
'port': 1883,
|
||||||
|
'discovery': False,
|
||||||
|
}
|
||||||
# Check we tried the connection
|
# Check we tried the connection
|
||||||
assert len(mock_try_connection.mock_calls) == 1
|
assert len(mock_try_connection.mock_calls) == 1
|
||||||
# Check config entry got setup
|
# Check config entry got setup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user