From 1d398832640f77b37dc71f1810c1b81010820520 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 16 Sep 2018 15:39:43 +0200 Subject: [PATCH] Add abbreviations for all words. Add testcase. Add missing docstring. --- homeassistant/components/mqtt/discovery.py | 64 ++++++++++++++++++---- tests/components/mqtt/test_discovery.py | 22 ++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 2f81a2ef4ab2a1..2593a74395f221 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -40,20 +40,63 @@ ALREADY_DISCOVERED = 'mqtt_discovered_components' ABBREVIATIONS = { - 'avail': 'available', - 'cmd': 'command', - 'tmp': 'template', - 'val': 'value', - 'stat': ' state', - 'pl': 'payload', - 't': 'topic', + 'aft': 'after', 'attr': 'attributes', + 'avty': 'availability', + 'avail': 'available', 'bri': 'brightness', - 'opt': 'optimistic', + 'cla': 'class', + 'cls': 'close', + 'clsd': 'closed', + 'clr': 'color', + 'cmd': 'command', + 'dev': 'device', + 'fx': 'effect', + 'exp': 'expire', + 'frc': 'force', + 'hi': 'high', + 'ic': 'icon', + 'id': 'id', + 'inj': 'invert', + 'json': 'json', + 'list': 'list', + 'lo': 'low', + 'max': 'max', 'meas': 'measurement', + 'med': 'medium', + 'min': 'min', + 'name': 'name', + 'not': 'not', + 'of': 'of', + 'off': 'off', + 'on': 'on', + 'open': 'open', + 'opnd': 'opened', + 'opt': 'optimistic', 'osc': 'oscillation', - 'avty': 'availability', - 'fx': 'effect', + 'pl': 'payload', + 'pos': 'position', + 'qos': 'qos', + 'ret': 'retain', + 'rgb': 'rgb', + 'scl': 'scale', + 'set': 'set', + 'spd': 'speed', + 'spds': 'speeds', + 'stat': 'state', + 'status': 'status', + 'stop': 'stop', + 'temp': 'temp', + 'tmp': 'template', + 'tilt': 'tilt', + 't': 'topic', + 'type': 'type', + 'uniq': 'unique', + 'unit': 'unit', + 'upd': 'update', + 'val': 'value', + 'whit': 'white', + 'xy': 'xy', } @@ -87,6 +130,7 @@ async def async_device_message_received(topic, payload, qos): pattern = r'(?:(?<=^)|(?<=_))[^\W_]+(?=_|$)' def expand(matchobj): + """Expand a possibly abbreviated word.""" abbreviation = matchobj.group(0) if abbreviation in ABBREVIATIONS: return ABBREVIATIONS[abbreviation] diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index 9e0ef14a3faca5..37793a457cdb0a 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -181,3 +181,25 @@ def test_non_duplicate_discovery(hass, mqtt_mock, caplog): assert state_duplicate is None assert 'Component has already been discovered: ' \ 'binary_sensor bla' in caplog.text + + +@asyncio.coroutine +def test_discovery_expansion(hass, mqtt_mock, caplog): + """Test expansion of abbreviated discovery payload.""" + yield from async_start(hass, 'homeassistant', {}) + + data = ( + '{ "name": "DiscoveryExpansionTest",' + ' "stat_t": "test_topic",' + ' "cmd_t": "test_topic" }' + ) + + async_fire_mqtt_message( + hass, 'homeassistant/switch/bla/config', data) + yield from hass.async_block_till_done() + + state = hass.states.get('switch.DiscoveryExpansionTest') + + assert state is not None + assert state.name == 'DiscoveryExpansionTest' + assert ('switch', 'bla') in hass.data[ALREADY_DISCOVERED]