Improve MQTT test coverage and remove dead code (#33584)

* Improve MQTT tests and remove dead code

* Remove useless test.

* Add more test
This commit is contained in:
Erik Montnemery authored and GitHub committed 2020-04-03 09:05:58 -07:00
1 parent a813847f6e
commit 4e6fd19624
12 files changed
+761 -125

No files matched your search

+159 -42
View File
@@ -1,6 +1,11 @@
"""The tests for the MQTT cover platform."""
from homeassistant.components import cover
from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION
from homeassistant.components.cover import (
ATTR_CURRENT_POSITION,
ATTR_CURRENT_TILT_POSITION,
ATTR_POSITION,
ATTR_TILT_POSITION,
)
from homeassistant.components.mqtt.cover import MqttCover
from homeassistant.const import (
ATTR_ASSUMED_STATE,
@@ -335,6 +340,68 @@ async def test_optimistic_state_change(hass, mqtt_mock):
assert state.state == STATE_CLOSED
async def test_optimistic_state_change_with_position(hass, mqtt_mock):
"""Test changing state optimistically."""
assert await async_setup_component(
hass,
cover.DOMAIN,
{
cover.DOMAIN: {
"platform": "mqtt",
"name": "test",
"optimistic": True,
"command_topic": "command-topic",
"position_topic": "position-topic",
"qos": 0,
}
},
)
state = hass.states.get("cover.test")
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ASSUMED_STATE)
assert state.attributes.get(ATTR_CURRENT_POSITION) is None
await hass.services.async_call(
cover.DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("cover.test")
assert state.state == STATE_OPEN
assert state.attributes.get(ATTR_CURRENT_POSITION) == 100
await hass.services.async_call(
cover.DOMAIN, SERVICE_CLOSE_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("cover.test")
assert STATE_CLOSED == state.state
assert state.attributes.get(ATTR_CURRENT_POSITION) == 0
await hass.services.async_call(
cover.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "OPEN", 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("cover.test")
assert STATE_OPEN == state.state
assert state.attributes.get(ATTR_CURRENT_POSITION) == 100
await hass.services.async_call(
cover.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "cover.test"}, blocking=True
)
mqtt_mock.async_publish.assert_called_once_with("command-topic", "CLOSE", 0, False)
state = hass.states.get("cover.test")
assert state.state == STATE_CLOSED
assert state.attributes.get(ATTR_CURRENT_POSITION) == 0
async def test_send_open_cover_command(hass, mqtt_mock):
"""Test the sending of open_cover."""
assert await async_setup_component(
@@ -440,31 +507,31 @@ async def test_current_cover_position(hass, mqtt_mock):
)
state_attributes_dict = hass.states.get("cover.test").attributes
assert not ("current_position" in state_attributes_dict)
assert not ("current_tilt_position" in state_attributes_dict)
assert not (ATTR_CURRENT_POSITION in state_attributes_dict)
assert not (ATTR_CURRENT_TILT_POSITION in state_attributes_dict)
assert not (4 & hass.states.get("cover.test").attributes["supported_features"] == 4)
async_fire_mqtt_message(hass, "get-position-topic", "0")
current_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_cover_position == 0
async_fire_mqtt_message(hass, "get-position-topic", "50")
current_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_cover_position == 50
async_fire_mqtt_message(hass, "get-position-topic", "non-numeric")
current_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_cover_position == 50
async_fire_mqtt_message(hass, "get-position-topic", "101")
current_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_cover_position == 100
@@ -490,48 +557,67 @@ async def test_current_cover_position_inverted(hass, mqtt_mock):
)
state_attributes_dict = hass.states.get("cover.test").attributes
assert not ("current_position" in state_attributes_dict)
assert not ("current_tilt_position" in state_attributes_dict)
assert not (ATTR_CURRENT_POSITION in state_attributes_dict)
assert not (ATTR_CURRENT_TILT_POSITION in state_attributes_dict)
assert not (4 & hass.states.get("cover.test").attributes["supported_features"] == 4)
async_fire_mqtt_message(hass, "get-position-topic", "100")
current_percentage_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_percentage_cover_position == 0
assert hass.states.get("cover.test").state == STATE_CLOSED
async_fire_mqtt_message(hass, "get-position-topic", "0")
current_percentage_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_percentage_cover_position == 100
assert hass.states.get("cover.test").state == STATE_OPEN
async_fire_mqtt_message(hass, "get-position-topic", "50")
current_percentage_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_percentage_cover_position == 50
assert hass.states.get("cover.test").state == STATE_OPEN
async_fire_mqtt_message(hass, "get-position-topic", "non-numeric")
current_percentage_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_percentage_cover_position == 50
assert hass.states.get("cover.test").state == STATE_OPEN
async_fire_mqtt_message(hass, "get-position-topic", "101")
current_percentage_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_percentage_cover_position == 0
assert hass.states.get("cover.test").state == STATE_CLOSED
async def test_set_cover_position(hass, mqtt_mock):
"""Test setting cover position."""
async def test_optimistic_position(hass, mqtt_mock):
"""Test optimistic position is not supported."""
assert await async_setup_component(
hass,
cover.DOMAIN,
{
cover.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
"set_position_topic": "set-position-topic",
}
},
)
state = hass.states.get("cover.test")
assert state is None
async def test_position_update(hass, mqtt_mock):
"""Test cover position update from received MQTT message."""
assert await async_setup_component(
hass,
cover.DOMAIN,
@@ -552,16 +638,16 @@ async def test_set_cover_position(hass, mqtt_mock):
)
state_attributes_dict = hass.states.get("cover.test").attributes
assert not ("current_position" in state_attributes_dict)
assert not ("current_tilt_position" in state_attributes_dict)
assert not (ATTR_CURRENT_POSITION in state_attributes_dict)
assert not (ATTR_CURRENT_TILT_POSITION in state_attributes_dict)
assert 4 & hass.states.get("cover.test").attributes["supported_features"] == 4
async_fire_mqtt_message(hass, "get-position-topic", "22")
state_attributes_dict = hass.states.get("cover.test").attributes
assert "current_position" in state_attributes_dict
assert not ("current_tilt_position" in state_attributes_dict)
assert ATTR_CURRENT_POSITION in state_attributes_dict
assert not (ATTR_CURRENT_TILT_POSITION in state_attributes_dict)
current_cover_position = hass.states.get("cover.test").attributes[
"current_position"
ATTR_CURRENT_POSITION
]
assert current_cover_position == 22
@@ -629,6 +715,37 @@ async def test_set_position_untemplated(hass, mqtt_mock):
mqtt_mock.async_publish.assert_called_once_with("position-topic", 62, 0, False)
async def test_set_position_untemplated_custom_percentage_range(hass, mqtt_mock):
"""Test setting cover position via template."""
assert await async_setup_component(
hass,
cover.DOMAIN,
{
cover.DOMAIN: {
"platform": "mqtt",
"name": "test",
"position_topic": "state-topic",
"command_topic": "command-topic",
"set_position_topic": "position-topic",
"position_open": 0,
"position_closed": 100,
"payload_open": "OPEN",
"payload_close": "CLOSE",
"payload_stop": "STOP",
}
},
)
await hass.services.async_call(
cover.DOMAIN,
SERVICE_SET_COVER_POSITION,
{ATTR_ENTITY_ID: "cover.test", ATTR_POSITION: 38},
blocking=True,
)
mqtt_mock.async_publish.assert_called_once_with("position-topic", 62, 0, False)
async def test_no_command_topic(hass, mqtt_mock):
"""Test with no command topic."""
assert await async_setup_component(
@@ -717,10 +834,10 @@ async def test_tilt_defaults(hass, mqtt_mock):
)
state_attributes_dict = hass.states.get("cover.test").attributes
assert "current_tilt_position" in state_attributes_dict
assert ATTR_CURRENT_TILT_POSITION in state_attributes_dict
current_cover_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_position == STATE_UNKNOWN
@@ -770,7 +887,7 @@ async def test_tilt_via_invocation_defaults(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "0")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 0
@@ -788,7 +905,7 @@ async def test_tilt_via_invocation_defaults(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "100")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 100
@@ -849,7 +966,7 @@ async def test_tilt_given_value(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "25")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 25
@@ -867,7 +984,7 @@ async def test_tilt_given_value(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "80")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 80
@@ -913,7 +1030,7 @@ async def test_tilt_given_value_optimistic(hass, mqtt_mock):
)
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 80
@@ -928,7 +1045,7 @@ async def test_tilt_given_value_optimistic(hass, mqtt_mock):
)
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 25
@@ -969,7 +1086,7 @@ async def test_tilt_given_value_altered_range(hass, mqtt_mock):
)
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 50
@@ -984,7 +1101,7 @@ async def test_tilt_given_value_altered_range(hass, mqtt_mock):
)
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 0
@@ -999,7 +1116,7 @@ async def test_tilt_given_value_altered_range(hass, mqtt_mock):
)
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 50
@@ -1030,14 +1147,14 @@ async def test_tilt_via_topic(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "0")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 0
async_fire_mqtt_message(hass, "tilt-status-topic", "50")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 50
@@ -1069,14 +1186,14 @@ async def test_tilt_via_topic_template(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "99")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 0
async_fire_mqtt_message(hass, "tilt-status-topic", "5000")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 50
@@ -1107,21 +1224,21 @@ async def test_tilt_via_topic_altered_range(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "0")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 0
async_fire_mqtt_message(hass, "tilt-status-topic", "50")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 100
async_fire_mqtt_message(hass, "tilt-status-topic", "25")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 50
@@ -1155,21 +1272,21 @@ async def test_tilt_via_topic_template_altered_range(hass, mqtt_mock):
async_fire_mqtt_message(hass, "tilt-status-topic", "99")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 0
async_fire_mqtt_message(hass, "tilt-status-topic", "5000")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 100
async_fire_mqtt_message(hass, "tilt-status-topic", "2500")
current_cover_tilt_position = hass.states.get("cover.test").attributes[
"current_tilt_position"
ATTR_CURRENT_TILT_POSITION
]
assert current_cover_tilt_position == 50