Add ability for MQTT device tracker to map non-default topic payloads to zones/states (#27143)

* add ability for MQTT device tracker to map nondefault topic payloads to zones

* update new parameter name and add abbreviation

* support for payload_home, payload_not_home, and payload_custom

* use constants STATE_NOT_HOME and STATE_HOME as defaults

* reference state constants directly

* add empty dict as default for payload_custom

* change parameter name for custom mapping of payloads to non-home zones to be more descriptive

* removed 'payload_other_zones' per ballobs review

* remove abbreviation for 'payload_other_zones'

* add tests for feature
This commit is contained in:
Raman Gupta authored and Paulus Schoutsen committed 2019-10-17 17:51:27 -07:00
1 parent 489340160b
commit 4e25807b7d
3 files changed
+86 -4

No files matched your search

+63 -1
View File
@@ -4,7 +4,7 @@ import pytest
from homeassistant.components import device_tracker
from homeassistant.components.device_tracker.const import ENTITY_ID_FORMAT
from homeassistant.const import CONF_PLATFORM
from homeassistant.const import CONF_PLATFORM, STATE_HOME, STATE_NOT_HOME
from homeassistant.setup import async_setup_component
from tests.common import async_fire_mqtt_message
@@ -156,3 +156,65 @@ async def test_multi_level_wildcard_topic_not_matching(hass, mock_device_tracker
async_fire_mqtt_message(hass, topic, location)
await hass.async_block_till_done()
assert hass.states.get(entity_id) is None
async def test_matching_custom_payload_for_home_and_not_home(
hass, mock_device_tracker_conf
):
"""Test custom payload_home sets state to home and custom payload_not_home sets state to not_home."""
dev_id = "paulus"
entity_id = ENTITY_ID_FORMAT.format(dev_id)
topic = "/location/paulus"
payload_home = "present"
payload_not_home = "not present"
hass.config.components = set(["mqtt", "zone"])
assert await async_setup_component(
hass,
device_tracker.DOMAIN,
{
device_tracker.DOMAIN: {
CONF_PLATFORM: "mqtt",
"devices": {dev_id: topic},
"payload_home": payload_home,
"payload_not_home": payload_not_home,
}
},
)
async_fire_mqtt_message(hass, topic, payload_home)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_HOME
async_fire_mqtt_message(hass, topic, payload_not_home)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_NOT_HOME
async def test_not_matching_custom_payload_for_home_and_not_home(
hass, mock_device_tracker_conf
):
"""Test not matching payload does not set state to home or not_home."""
dev_id = "paulus"
entity_id = ENTITY_ID_FORMAT.format(dev_id)
topic = "/location/paulus"
payload_home = "present"
payload_not_home = "not present"
payload_not_matching = "test"
hass.config.components = set(["mqtt", "zone"])
assert await async_setup_component(
hass,
device_tracker.DOMAIN,
{
device_tracker.DOMAIN: {
CONF_PLATFORM: "mqtt",
"devices": {dev_id: topic},
"payload_home": payload_home,
"payload_not_home": payload_not_home,
}
},
)
async_fire_mqtt_message(hass, topic, payload_not_matching)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state != STATE_HOME
assert hass.states.get(entity_id).state != STATE_NOT_HOME