Skip to content

Commit

Permalink
Add abbreviations for all words. Add testcase.
Browse files Browse the repository at this point in the history
Add missing docstring.
  • Loading branch information
emontnemery committed Sep 16, 2018
1 parent 13fc348 commit 1d39883
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
64 changes: 54 additions & 10 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}


Expand Down Expand Up @@ -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]
Expand Down
22 changes: 22 additions & 0 deletions tests/components/mqtt/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 1d39883

Please sign in to comment.