mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Support mqtt discovery topic prefix with slashes (#24840)
This commit is contained in:
parent
846575b7fb
commit
8cd138608c
@ -15,8 +15,8 @@ from .const import ATTR_DISCOVERY_HASH, CONF_STATE_TOPIC
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
TOPIC_MATCHER = re.compile(
|
TOPIC_MATCHER = re.compile(
|
||||||
r'(?P<prefix_topic>\w+)/(?P<component>\w+)/'
|
r'(?P<component>\w+)/(?:(?P<node_id>[a-zA-Z0-9_-]+)/)'
|
||||||
r'(?:(?P<node_id>[a-zA-Z0-9_-]+)/)?(?P<object_id>[a-zA-Z0-9_-]+)/config')
|
r'?(?P<object_id>[a-zA-Z0-9_-]+)/config')
|
||||||
|
|
||||||
SUPPORTED_COMPONENTS = [
|
SUPPORTED_COMPONENTS = [
|
||||||
'alarm_control_panel',
|
'alarm_control_panel',
|
||||||
@ -233,12 +233,13 @@ async def async_start(hass: HomeAssistantType, discovery_topic, hass_config,
|
|||||||
"""Process the received message."""
|
"""Process the received message."""
|
||||||
payload = msg.payload
|
payload = msg.payload
|
||||||
topic = msg.topic
|
topic = msg.topic
|
||||||
match = TOPIC_MATCHER.match(topic)
|
topic_trimmed = topic.replace('{}/'.format(discovery_topic), '', 1)
|
||||||
|
match = TOPIC_MATCHER.match(topic_trimmed)
|
||||||
|
|
||||||
if not match:
|
if not match:
|
||||||
return
|
return
|
||||||
|
|
||||||
_prefix_topic, component, node_id, object_id = match.groups()
|
component, node_id, object_id = match.groups()
|
||||||
|
|
||||||
if component not in SUPPORTED_COMPONENTS:
|
if component not in SUPPORTED_COMPONENTS:
|
||||||
_LOGGER.warning("Component %s is not supported", component)
|
_LOGGER.warning("Component %s is not supported", component)
|
||||||
|
@ -369,3 +369,21 @@ async def test_no_implicit_state_topic_switch(hass, mqtt_mock, caplog):
|
|||||||
|
|
||||||
state = hass.states.get('switch.Test1')
|
state = hass.states.get('switch.Test1')
|
||||||
assert state.state == 'off'
|
assert state.state == 'off'
|
||||||
|
|
||||||
|
|
||||||
|
async def test_complex_discovery_topic_prefix(hass, mqtt_mock, caplog):
|
||||||
|
"""Tests handling of discovery topic prefix with multiple slashes."""
|
||||||
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
|
|
||||||
|
await async_start(hass, 'my_home/homeassistant/register', {}, entry)
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, ('my_home/homeassistant/register'
|
||||||
|
'/binary_sensor/node1/object1/config'),
|
||||||
|
'{ "name": "Beer" }')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('binary_sensor.beer')
|
||||||
|
|
||||||
|
assert state is not None
|
||||||
|
assert state.name == 'Beer'
|
||||||
|
assert ('binary_sensor', 'node1 object1') in hass.data[ALREADY_DISCOVERED]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user