Skip to content

Commit

Permalink
Improve MQTT tests (#23296)
Browse files Browse the repository at this point in the history
* Improve MQTT tests

* Tweak
  • Loading branch information
emontnemery authored and cgarwood committed Apr 22, 2019
1 parent 357631d commit a89c7f8
Show file tree
Hide file tree
Showing 13 changed files with 625 additions and 677 deletions.
42 changes: 20 additions & 22 deletions tests/components/mqtt/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ async def test_update_state_via_state_topic(hass, mqtt_mock):

entity_id = 'alarm_control_panel.test'

assert STATE_UNKNOWN == \
hass.states.get(entity_id).state
assert hass.states.get(entity_id).state == STATE_UNKNOWN

for state in (STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_NIGHT,
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED):
async_fire_mqtt_message(hass, 'alarm/state', state)
assert state == hass.states.get(entity_id).state
assert hass.states.get(entity_id).state == state


async def test_ignore_update_state_if_unknown_via_state_topic(hass, mqtt_mock):
Expand All @@ -76,11 +75,10 @@ async def test_ignore_update_state_if_unknown_via_state_topic(hass, mqtt_mock):

entity_id = 'alarm_control_panel.test'

assert STATE_UNKNOWN == \
hass.states.get(entity_id).state
assert hass.states.get(entity_id).state == STATE_UNKNOWN

async_fire_mqtt_message(hass, 'alarm/state', 'unsupported state')
assert STATE_UNKNOWN == hass.states.get(entity_id).state
assert hass.states.get(entity_id).state == STATE_UNKNOWN


async def test_arm_home_publishes_mqtt(hass, mqtt_mock):
Expand Down Expand Up @@ -120,7 +118,7 @@ async def test_arm_home_not_publishes_mqtt_with_invalid_code_when_req(
call_count = mqtt_mock.async_publish.call_count
common.async_alarm_arm_home(hass, 'abcd')
await hass.async_block_till_done()
assert call_count == mqtt_mock.async_publish.call_count
assert mqtt_mock.async_publish.call_count == call_count


async def test_arm_home_publishes_mqtt_when_code_not_req(hass, mqtt_mock):
Expand Down Expand Up @@ -182,7 +180,7 @@ async def test_arm_away_not_publishes_mqtt_with_invalid_code_when_req(
call_count = mqtt_mock.async_publish.call_count
common.async_alarm_arm_away(hass, 'abcd')
await hass.async_block_till_done()
assert call_count == mqtt_mock.async_publish.call_count
assert mqtt_mock.async_publish.call_count == call_count


async def test_arm_away_publishes_mqtt_when_code_not_req(hass, mqtt_mock):
Expand Down Expand Up @@ -244,7 +242,7 @@ async def test_arm_night_not_publishes_mqtt_with_invalid_code_when_req(
call_count = mqtt_mock.async_publish.call_count
common.async_alarm_arm_night(hass, 'abcd')
await hass.async_block_till_done()
assert call_count == mqtt_mock.async_publish.call_count
assert mqtt_mock.async_publish.call_count == call_count


async def test_arm_night_publishes_mqtt_when_code_not_req(hass, mqtt_mock):
Expand Down Expand Up @@ -353,7 +351,7 @@ async def test_disarm_not_publishes_mqtt_with_invalid_code_when_req(
call_count = mqtt_mock.async_publish.call_count
common.async_alarm_disarm(hass, 'abcd')
await hass.async_block_till_done()
assert call_count == mqtt_mock.async_publish.call_count
assert mqtt_mock.async_publish.call_count == call_count


async def test_default_availability_payload(hass, mqtt_mock):
Expand All @@ -370,17 +368,17 @@ async def test_default_availability_payload(hass, mqtt_mock):
})

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'online')

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNAVAILABLE != state.state
assert state.state != STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'offline')

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE


async def test_custom_availability_payload(hass, mqtt_mock):
Expand All @@ -399,17 +397,17 @@ async def test_custom_availability_payload(hass, mqtt_mock):
})

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'good')

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNAVAILABLE != state.state
assert state.state != STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'nogood')

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE


async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
Expand All @@ -427,7 +425,7 @@ async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'attr-topic', '{ "val": "100" }')
state = hass.states.get('alarm_control_panel.test')

assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'


async def test_update_state_via_state_topic_template(hass, mqtt_mock):
Expand All @@ -448,12 +446,12 @@ async def test_update_state_via_state_topic_template(hass, mqtt_mock):
})

state = hass.states.get('alarm_control_panel.test')
assert STATE_UNKNOWN == state.state
assert state.state == STATE_UNKNOWN

async_fire_mqtt_message(hass, 'test-topic', '100')

state = hass.states.get('alarm_control_panel.test')
assert STATE_ALARM_ARMED_AWAY == state.state
assert state.state == STATE_ALARM_ARMED_AWAY


async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
Expand Down Expand Up @@ -513,7 +511,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
await hass.async_block_till_done()
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "100" }')
state = hass.states.get('alarm_control_panel.beer')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'

# Change json_attributes_topic
async_fire_mqtt_message(
Expand All @@ -523,12 +521,12 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
# Verify we are no longer subscribing to the old topic
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "50" }')
state = hass.states.get('alarm_control_panel.beer')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'

# Verify we are subscribing to the new topic
async_fire_mqtt_message(hass, 'attr-topic2', '{ "val": "75" }')
state = hass.states.get('alarm_control_panel.beer')
assert '75' == state.attributes.get('val')
assert state.attributes.get('val') == '75'


async def test_unique_id(hass):
Expand Down
56 changes: 28 additions & 28 deletions tests/components/mqtt/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
})

state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF

async_fire_mqtt_message(hass, 'test-topic', 'ON')
state = hass.states.get('binary_sensor.test')
assert STATE_ON == state.state
assert state.state == STATE_ON

async_fire_mqtt_message(hass, 'test-topic', 'OFF')
state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF


async def test_setting_sensor_value_via_mqtt_message_and_template(
Expand All @@ -56,15 +56,15 @@ async def test_setting_sensor_value_via_mqtt_message_and_template(
})

state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF

async_fire_mqtt_message(hass, 'test-topic', '')
state = hass.states.get('binary_sensor.test')
assert STATE_ON == state.state
assert state.state == STATE_ON

async_fire_mqtt_message(hass, 'test-topic', '')
state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF


async def test_valid_device_class(hass, mqtt_mock):
Expand All @@ -79,7 +79,7 @@ async def test_valid_device_class(hass, mqtt_mock):
})

state = hass.states.get('binary_sensor.test')
assert 'motion' == state.attributes.get('device_class')
assert state.attributes.get('device_class') == 'motion'


async def test_invalid_device_class(hass, mqtt_mock):
Expand Down Expand Up @@ -108,7 +108,7 @@ async def test_availability_without_topic(hass, mqtt_mock):
})

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE != state.state
assert state.state != STATE_UNAVAILABLE


async def test_availability_by_defaults(hass, mqtt_mock):
Expand All @@ -123,17 +123,17 @@ async def test_availability_by_defaults(hass, mqtt_mock):
})

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'online')

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE != state.state
assert state.state != STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'offline')

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE


async def test_availability_by_custom_payload(hass, mqtt_mock):
Expand All @@ -150,17 +150,17 @@ async def test_availability_by_custom_payload(hass, mqtt_mock):
})

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'good')

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE != state.state
assert state.state != STATE_UNAVAILABLE

async_fire_mqtt_message(hass, 'availability-topic', 'nogood')

state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE


async def test_force_update_disabled(hass, mqtt_mock):
Expand All @@ -186,11 +186,11 @@ def callback(event):

async_fire_mqtt_message(hass, 'test-topic', 'ON')
await hass.async_block_till_done()
assert 1 == len(events)
assert len(events) == 1

async_fire_mqtt_message(hass, 'test-topic', 'ON')
await hass.async_block_till_done()
assert 1 == len(events)
assert len(events) == 1


async def test_force_update_enabled(hass, mqtt_mock):
Expand All @@ -217,11 +217,11 @@ def callback(event):

async_fire_mqtt_message(hass, 'test-topic', 'ON')
await hass.async_block_till_done()
assert 1 == len(events)
assert len(events) == 1

async_fire_mqtt_message(hass, 'test-topic', 'ON')
await hass.async_block_till_done()
assert 2 == len(events)
assert len(events) == 2


async def test_off_delay(hass, mqtt_mock):
Expand Down Expand Up @@ -250,20 +250,20 @@ def callback(event):
async_fire_mqtt_message(hass, 'test-topic', 'ON')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert STATE_ON == state.state
assert 1 == len(events)
assert state.state == STATE_ON
assert len(events) == 1

async_fire_mqtt_message(hass, 'test-topic', 'ON')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert STATE_ON == state.state
assert 2 == len(events)
assert state.state == STATE_ON
assert len(events) == 2

async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30))
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state
assert 3 == len(events)
assert state.state == STATE_OFF
assert len(events) == 3


async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
Expand All @@ -280,7 +280,7 @@ async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'attr-topic', '{ "val": "100" }')
state = hass.states.get('binary_sensor.test')

assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'


async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
Expand Down Expand Up @@ -338,7 +338,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
await hass.async_block_till_done()
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "100" }')
state = hass.states.get('binary_sensor.beer')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'

# Change json_attributes_topic
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
Expand All @@ -348,12 +348,12 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
# Verify we are no longer subscribing to the old topic
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "50" }')
state = hass.states.get('binary_sensor.beer')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'

# Verify we are subscribing to the new topic
async_fire_mqtt_message(hass, 'attr-topic2', '{ "val": "75" }')
state = hass.states.get('binary_sensor.beer')
assert '75' == state.attributes.get('val')
assert state.attributes.get('val') == '75'


async def test_unique_id(hass):
Expand Down
Loading

0 comments on commit a89c7f8

Please sign in to comment.