mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Async tests for mqtt switch (#18685)
This commit is contained in:
parent
6f0a3b4b22
commit
00c9ca64c8
@ -1,9 +1,9 @@
|
|||||||
"""The tests for the MQTT switch platform."""
|
"""The tests for the MQTT switch platform."""
|
||||||
import json
|
import json
|
||||||
import unittest
|
from asynctest import patch
|
||||||
from unittest.mock import patch
|
import pytest
|
||||||
|
|
||||||
from homeassistant.setup import setup_component, async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\
|
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\
|
||||||
ATTR_ASSUMED_STATE
|
ATTR_ASSUMED_STATE
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
@ -11,26 +11,20 @@ from homeassistant.components import switch, mqtt
|
|||||||
from homeassistant.components.mqtt.discovery import async_start
|
from homeassistant.components.mqtt.discovery import async_start
|
||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro,
|
mock_coro, async_mock_mqtt_component, async_fire_mqtt_message,
|
||||||
async_mock_mqtt_component, async_fire_mqtt_message, MockConfigEntry)
|
MockConfigEntry)
|
||||||
from tests.components.switch import common
|
from tests.components.switch import common
|
||||||
|
|
||||||
|
|
||||||
class TestSwitchMQTT(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Test the MQTT switch."""
|
def mock_publish(hass):
|
||||||
|
"""Initialize components."""
|
||||||
|
yield hass.loop.run_until_complete(async_mock_mqtt_component(hass))
|
||||||
|
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.mock_publish = mock_mqtt_component(self.hass)
|
|
||||||
|
|
||||||
def tearDown(self): # pylint: disable=invalid-name
|
async def test_controlling_state_via_topic(hass, mock_publish):
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
def test_controlling_state_via_topic(self):
|
|
||||||
"""Test the controlling state via topic."""
|
"""Test the controlling state via topic."""
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -41,29 +35,31 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '1')
|
async_fire_mqtt_message(hass, 'state-topic', '1')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '0')
|
async_fire_mqtt_message(hass, 'state-topic', '0')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
def test_sending_mqtt_commands_and_optimistic(self):
|
|
||||||
|
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
|
||||||
"""Test the sending MQTT commands in optimistic mode."""
|
"""Test the sending MQTT commands in optimistic mode."""
|
||||||
fake_state = ha.State('switch.test', 'on')
|
fake_state = ha.State('switch.test', 'on')
|
||||||
|
|
||||||
with patch('homeassistant.components.switch.mqtt.async_get_last_state',
|
with patch('homeassistant.components.switch.mqtt.async_get_last_state',
|
||||||
return_value=mock_coro(fake_state)):
|
return_value=mock_coro(fake_state)):
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -74,30 +70,33 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
assert state.attributes.get(ATTR_ASSUMED_STATE)
|
||||||
|
|
||||||
common.turn_on(self.hass, 'switch.test')
|
common.turn_on(hass, 'switch.test')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
self.mock_publish.async_publish.assert_called_once_with(
|
mock_publish.async_publish.assert_called_once_with(
|
||||||
'command-topic', 'beer on', 2, False)
|
'command-topic', 'beer on', 2, False)
|
||||||
self.mock_publish.async_publish.reset_mock()
|
mock_publish.async_publish.reset_mock()
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
common.turn_off(self.hass, 'switch.test')
|
common.turn_off(hass, 'switch.test')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
self.mock_publish.async_publish.assert_called_once_with(
|
mock_publish.async_publish.assert_called_once_with(
|
||||||
'command-topic', 'beer off', 2, False)
|
'command-topic', 'beer off', 2, False)
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
def test_controlling_state_via_topic_and_json_message(self):
|
|
||||||
|
async def test_controlling_state_via_topic_and_json_message(
|
||||||
|
hass, mock_publish):
|
||||||
"""Test the controlling state via topic and JSON message."""
|
"""Test the controlling state via topic and JSON message."""
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -109,24 +108,27 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer on"}')
|
async_fire_mqtt_message(hass, 'state-topic', '{"val":"beer on"}')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer off"}')
|
async_fire_mqtt_message(hass, 'state-topic', '{"val":"beer off"}')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
def test_controlling_availability(self):
|
|
||||||
|
async def test_controlling_availability(hass, mock_publish):
|
||||||
"""Test the controlling state via topic."""
|
"""Test the controlling state via topic."""
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -140,37 +142,42 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', '1')
|
async_fire_mqtt_message(hass, 'availability_topic', '1')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', '0')
|
async_fire_mqtt_message(hass, 'availability_topic', '0')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '1')
|
async_fire_mqtt_message(hass, 'state-topic', '1')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', '1')
|
async_fire_mqtt_message(hass, 'availability_topic', '1')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
def test_default_availability_payload(self):
|
|
||||||
|
async def test_default_availability_payload(hass, mock_publish):
|
||||||
"""Test the availability payload."""
|
"""Test the availability payload."""
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -182,37 +189,42 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', 'online')
|
async_fire_mqtt_message(hass, 'availability_topic', 'online')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', 'offline')
|
async_fire_mqtt_message(hass, 'availability_topic', 'offline')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '1')
|
async_fire_mqtt_message(hass, 'state-topic', '1')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', 'online')
|
async_fire_mqtt_message(hass, 'availability_topic', 'online')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
def test_custom_availability_payload(self):
|
|
||||||
|
async def test_custom_availability_payload(hass, mock_publish):
|
||||||
"""Test the availability payload."""
|
"""Test the availability payload."""
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -226,37 +238,41 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', 'good')
|
async_fire_mqtt_message(hass, 'availability_topic', 'good')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', 'nogood')
|
async_fire_mqtt_message(hass, 'availability_topic', 'nogood')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', '1')
|
async_fire_mqtt_message(hass, 'state-topic', '1')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_UNAVAILABLE == state.state
|
assert STATE_UNAVAILABLE == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'availability_topic', 'good')
|
async_fire_mqtt_message(hass, 'availability_topic', 'good')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
def test_custom_state_payload(self):
|
|
||||||
|
async def test_custom_state_payload(hass, mock_publish):
|
||||||
"""Test the state payload."""
|
"""Test the state payload."""
|
||||||
assert setup_component(self.hass, switch.DOMAIN, {
|
assert await async_setup_component(hass, switch.DOMAIN, {
|
||||||
switch.DOMAIN: {
|
switch.DOMAIN: {
|
||||||
'platform': 'mqtt',
|
'platform': 'mqtt',
|
||||||
'name': 'test',
|
'name': 'test',
|
||||||
@ -269,20 +285,22 @@ class TestSwitchMQTT(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', 'HIGH')
|
async_fire_mqtt_message(hass, 'state-topic', 'HIGH')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_ON == state.state
|
assert STATE_ON == state.state
|
||||||
|
|
||||||
fire_mqtt_message(self.hass, 'state-topic', 'LOW')
|
async_fire_mqtt_message(hass, 'state-topic', 'LOW')
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = self.hass.states.get('switch.test')
|
state = hass.states.get('switch.test')
|
||||||
assert STATE_OFF == state.state
|
assert STATE_OFF == state.state
|
||||||
|
|
||||||
|
|
||||||
@ -307,6 +325,7 @@ async def test_unique_id(hass):
|
|||||||
|
|
||||||
async_fire_mqtt_message(hass, 'test-topic', 'payload')
|
async_fire_mqtt_message(hass, 'test-topic', 'payload')
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert len(hass.states.async_entity_ids()) == 2
|
assert len(hass.states.async_entity_ids()) == 2
|
||||||
# all switches group is 1, unique id created is 1
|
# all switches group is 1, unique id created is 1
|
||||||
@ -326,6 +345,7 @@ async def test_discovery_removal_switch(hass, mqtt_mock, caplog):
|
|||||||
async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config',
|
async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config',
|
||||||
data)
|
data)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get('switch.beer')
|
state = hass.states.get('switch.beer')
|
||||||
assert state is not None
|
assert state is not None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user