Increase coverage and cleanup BleBox cover (#35369)

* increase coverage and cleanup BleBox cover

* test entity properites via the `state` property

* properly test entity properities

* properly mock io call
pull/35460/head
gadgetmobile 2020-05-10 12:22:26 +02:00 committed by GitHub
parent b4142fc7ee
commit 9e3a8899c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 28 deletions

View File

@ -77,7 +77,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
def create_blebox_entities(product, async_add, entity_klass, entity_type):
"""Create entities from a BleBox product's features."""
entities = [entity_klass(feature) for feature in product.features[entity_type]]
entities = []
if entity_type in product.features:
for feature in product.features[entity_type]:
entities.append(entity_klass(feature))
async_add(entities, True)

View File

@ -70,8 +70,8 @@ def config_fixture():
return {DOMAIN: {CONF_HOST: "172.100.123.4", CONF_PORT: 80}}
@pytest.fixture
def wrapper(request):
@pytest.fixture(name="feature")
def feature(request):
"""Return an entity wrapper from given fixture name."""
return request.getfixturevalue(request.param)

View File

@ -50,7 +50,7 @@ def flow_feature_mock():
)
async def test_flow_works(hass, flow_feature_mock):
async def test_flow_works(hass, valid_feature_mock, flow_feature_mock):
"""Test that config flow works."""
result = await hass.config_entries.flow.async_init(

View File

@ -1,5 +1,7 @@
"""BleBox cover entities tests."""
import logging
import blebox_uniapi
import pytest
@ -30,7 +32,7 @@ from homeassistant.const import (
from .conftest import async_setup_entity, mock_feature
from tests.async_mock import ANY, AsyncMock, PropertyMock, call, patch
from tests.async_mock import AsyncMock, PropertyMock
ALL_COVER_FIXTURES = ["gatecontroller", "shutterbox", "gatebox"]
FIXTURES_SUPPORTING_STOP = ["gatecontroller", "shutterbox"]
@ -188,11 +190,11 @@ async def test_init_gatebox(gatebox, hass, config):
assert device.sw_version == "1.23"
@pytest.mark.parametrize("wrapper", ALL_COVER_FIXTURES, indirect=["wrapper"])
async def test_open(wrapper, hass, config):
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_open(feature, hass, config):
"""Test cover opening."""
feature_mock, entity_id = wrapper
feature_mock, entity_id = feature
def initial_update():
feature_mock.state = 3 # manually stopped
@ -213,11 +215,11 @@ async def test_open(wrapper, hass, config):
assert hass.states.get(entity_id).state == STATE_OPENING
@pytest.mark.parametrize("wrapper", ALL_COVER_FIXTURES, indirect=["wrapper"])
async def test_close(wrapper, hass, config):
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_close(feature, hass, config):
"""Test cover closing."""
feature_mock, entity_id = wrapper
feature_mock, entity_id = feature
def initial_update():
feature_mock.state = 4 # open
@ -251,11 +253,11 @@ def opening_to_stop_feature_mock(feature_mock):
feature_mock.async_stop = AsyncMock(side_effect=stop)
@pytest.mark.parametrize("wrapper", FIXTURES_SUPPORTING_STOP, indirect=["wrapper"])
async def test_stop(wrapper, hass, config):
@pytest.mark.parametrize("feature", FIXTURES_SUPPORTING_STOP, indirect=["feature"])
async def test_stop(feature, hass, config):
"""Test cover stopping."""
feature_mock, entity_id = wrapper
feature_mock, entity_id = feature
opening_to_stop_feature_mock(feature_mock)
await async_setup_entity(hass, config, entity_id)
@ -268,11 +270,11 @@ async def test_stop(wrapper, hass, config):
assert hass.states.get(entity_id).state == STATE_OPEN
@pytest.mark.parametrize("wrapper", ALL_COVER_FIXTURES, indirect=["wrapper"])
async def test_update(wrapper, hass, config):
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_update(feature, hass, config):
"""Test cover updating."""
feature_mock, entity_id = wrapper
feature_mock, entity_id = feature
def initial_update():
feature_mock.current = 29 # inverted
@ -288,12 +290,12 @@ async def test_update(wrapper, hass, config):
@pytest.mark.parametrize(
"wrapper", ["gatecontroller", "shutterbox"], indirect=["wrapper"]
"feature", ["gatecontroller", "shutterbox"], indirect=["feature"]
)
async def test_set_position(wrapper, hass, config):
async def test_set_position(feature, hass, config):
"""Test cover position setting."""
feature_mock, entity_id = wrapper
feature_mock, entity_id = feature
def initial_update():
feature_mock.state = 3 # closed
@ -365,17 +367,56 @@ async def test_with_no_stop(gatebox, hass, config):
assert not supported_features & SUPPORT_STOP
@pytest.mark.parametrize("wrapper", ALL_COVER_FIXTURES, indirect=["wrapper"])
async def test_update_failure(wrapper, hass, config):
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_update_failure(feature, hass, config, caplog):
"""Test that update failures are logged."""
feature_mock, entity_id = wrapper
caplog.set_level(logging.ERROR)
feature_mock, entity_id = feature
feature_mock.async_update = AsyncMock(side_effect=blebox_uniapi.error.ClientError)
name = feature_mock.full_name
await async_setup_entity(hass, config, entity_id)
with patch("homeassistant.components.blebox._LOGGER.error") as error:
await async_setup_entity(hass, config, entity_id)
assert f"Updating '{feature_mock.full_name}' failed: " in caplog.text
error.assert_has_calls([call("Updating '%s' failed: %s", name, ANY)])
assert isinstance(error.call_args[0][2], blebox_uniapi.error.ClientError)
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_opening_state(feature, hass, config):
"""Test that entity properties work."""
feature_mock, entity_id = feature
def initial_update():
feature_mock.state = 1 # opening
feature_mock.async_update = AsyncMock(side_effect=initial_update)
await async_setup_entity(hass, config, entity_id)
assert hass.states.get(entity_id).state == STATE_OPENING
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_closing_state(feature, hass, config):
"""Test that entity properties work."""
feature_mock, entity_id = feature
def initial_update():
feature_mock.state = 0 # closing
feature_mock.async_update = AsyncMock(side_effect=initial_update)
await async_setup_entity(hass, config, entity_id)
assert hass.states.get(entity_id).state == STATE_CLOSING
@pytest.mark.parametrize("feature", ALL_COVER_FIXTURES, indirect=["feature"])
async def test_closed_state(feature, hass, config):
"""Test that entity properties work."""
feature_mock, entity_id = feature
def initial_update():
feature_mock.state = 3 # closed
feature_mock.async_update = AsyncMock(side_effect=initial_update)
await async_setup_entity(hass, config, entity_id)
assert hass.states.get(entity_id).state == STATE_CLOSED