Add cover platform to Dynalite (#32594)

* lib version

* unit-test refactoring

* added type hints

* added cover

* added test to see that consts have the same value as library consts

* Update tests/components/dynalite/test_init.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

* removed trigger template

* Update homeassistant/components/dynalite/__init__.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/dynalite/const.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

* removed CONF_TRIGGER from const
corrected type hints - not clear why mypy didn't catch it

* conversion of the config to library CONFs

* moved to use the value since it should come from the library

* taking CONF_HOST from homeassistant.const instead of module const

* use dict.get
removed leftover log

* force device_class to be from homeassistant consts

* move dict.get to inline

* removed CONF from values
changed "channelcover" to "channel_cover"

* moved some CONF values out of const.py and taking them from homeassistant.const

* verifying that device class is a valid HA device class

* moved shutter to home assistant const

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
ZivandMartin Hjelmare authored and GitHub committed 2020-04-02 20:26:36 +02:00
1 parent 1d89d22a38
commit 39408ab240
17 files changed
+587 -106

No files matched your search

+1
View File
@@ -44,6 +44,7 @@ async def create_entity_from_device(hass, device):
new_device_func = mock_dyn_dev.mock_calls[1][2]["new_device_func"]
new_device_func([device])
await hass.async_block_till_done()
return mock_dyn_dev.mock_calls[1][2]["update_device_func"]
async def run_service_tests(hass, device, platform, services):
+8
View File
@@ -61,8 +61,15 @@ async def test_add_devices_then_register(hass):
device2.name = "NAME2"
device2.unique_id = "unique2"
new_device_func([device1, device2])
device3 = Mock()
device3.category = "switch"
device3.name = "NAME3"
device3.unique_id = "unique3"
new_device_func([device3])
await hass.async_block_till_done()
assert hass.states.get("light.name")
assert hass.states.get("switch.name2")
assert hass.states.get("switch.name3")
async def test_register_then_add_devices(hass):
@@ -89,3 +96,4 @@ async def test_register_then_add_devices(hass):
new_device_func([device1, device2])
await hass.async_block_till_done()
assert hass.states.get("light.name")
assert hass.states.get("switch.name2")
+39 -27
View File
@@ -1,6 +1,7 @@
"""Test Dynalite config flow."""
from asynctest import CoroutineMock, patch
import pytest
from homeassistant import config_entries
from homeassistant.components import dynalite
@@ -8,12 +9,20 @@ from homeassistant.components import dynalite
from tests.common import MockConfigEntry
async def run_flow(hass, connection):
@pytest.mark.parametrize(
"first_con, second_con,exp_type, exp_result, exp_reason",
[
(True, True, "create_entry", "loaded", ""),
(False, False, "abort", "", "no_connection"),
(True, False, "create_entry", "setup_retry", ""),
],
)
async def test_flow(hass, first_con, second_con, exp_type, exp_result, exp_reason):
"""Run a flow with or without errors and return result."""
host = "1.2.3.4"
with patch(
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
side_effect=connection,
side_effect=[first_con, second_con],
):
result = await hass.config_entries.flow.async_init(
dynalite.DOMAIN,
@@ -21,35 +30,18 @@ async def run_flow(hass, connection):
data={dynalite.CONF_HOST: host},
)
await hass.async_block_till_done()
return result
async def test_flow_works(hass):
"""Test a successful config flow."""
result = await run_flow(hass, [True, True])
assert result["type"] == "create_entry"
assert result["result"].state == "loaded"
async def test_flow_setup_fails(hass):
"""Test a flow where async_setup fails."""
result = await run_flow(hass, [False])
assert result["type"] == "abort"
assert result["reason"] == "no_connection"
async def test_flow_setup_fails_in_setup_entry(hass):
"""Test a flow where the initial check works but inside setup_entry, the bridge setup fails."""
result = await run_flow(hass, [True, False])
assert result["type"] == "create_entry"
assert result["result"].state == "setup_retry"
assert result["type"] == exp_type
if exp_result:
assert result["result"].state == exp_result
if exp_reason:
assert result["reason"] == exp_reason
async def test_existing(hass):
"""Test when the entry exists with the same config."""
host = "1.2.3.4"
MockConfigEntry(
domain=dynalite.DOMAIN, unique_id=host, data={dynalite.CONF_HOST: host}
domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host}
).add_to_hass(hass)
with patch(
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
@@ -81,7 +73,7 @@ async def test_existing_update(hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
mock_dyn_dev().configure.assert_called_once()
assert mock_dyn_dev().configure.mock_calls[0][1][0][dynalite.CONF_PORT] == port1
assert mock_dyn_dev().configure.mock_calls[0][1][0]["port"] == port1
result = await hass.config_entries.flow.async_init(
dynalite.DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
@@ -89,6 +81,26 @@ async def test_existing_update(hass):
)
await hass.async_block_till_done()
assert mock_dyn_dev().configure.call_count == 2
assert mock_dyn_dev().configure.mock_calls[1][1][0][dynalite.CONF_PORT] == port2
assert mock_dyn_dev().configure.mock_calls[1][1][0]["port"] == port2
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
async def test_two_entries(hass):
"""Test when two different entries exist with different hosts."""
host1 = "1.2.3.4"
host2 = "5.6.7.8"
MockConfigEntry(
domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host1}
).add_to_hass(hass)
with patch(
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
return_value=True,
):
result = await hass.config_entries.flow.async_init(
dynalite.DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={dynalite.CONF_HOST: host2},
)
assert result["type"] == "create_entry"
assert result["result"].state == "loaded"
+100
View File
@@ -0,0 +1,100 @@
"""Test Dynalite cover."""
from dynalite_devices_lib.cover import DynaliteTimeCoverWithTiltDevice
import pytest
from .common import (
ATTR_ARGS,
ATTR_METHOD,
ATTR_SERVICE,
create_entity_from_device,
create_mock_device,
run_service_tests,
)
@pytest.fixture
def mock_device():
"""Mock a Dynalite device."""
mock_dev = create_mock_device("cover", DynaliteTimeCoverWithTiltDevice)
mock_dev.device_class = "blind"
return mock_dev
async def test_cover_setup(hass, mock_device):
"""Test a successful setup."""
await create_entity_from_device(hass, mock_device)
entity_state = hass.states.get("cover.name")
assert entity_state.attributes["friendly_name"] == mock_device.name
assert (
entity_state.attributes["current_position"]
== mock_device.current_cover_position
)
assert (
entity_state.attributes["current_tilt_position"]
== mock_device.current_cover_tilt_position
)
assert entity_state.attributes["device_class"] == mock_device.device_class
await run_service_tests(
hass,
mock_device,
"cover",
[
{ATTR_SERVICE: "open_cover", ATTR_METHOD: "async_open_cover"},
{ATTR_SERVICE: "close_cover", ATTR_METHOD: "async_close_cover"},
{ATTR_SERVICE: "stop_cover", ATTR_METHOD: "async_stop_cover"},
{
ATTR_SERVICE: "set_cover_position",
ATTR_METHOD: "async_set_cover_position",
ATTR_ARGS: {"position": 50},
},
{ATTR_SERVICE: "open_cover_tilt", ATTR_METHOD: "async_open_cover_tilt"},
{ATTR_SERVICE: "close_cover_tilt", ATTR_METHOD: "async_close_cover_tilt"},
{ATTR_SERVICE: "stop_cover_tilt", ATTR_METHOD: "async_stop_cover_tilt"},
{
ATTR_SERVICE: "set_cover_tilt_position",
ATTR_METHOD: "async_set_cover_tilt_position",
ATTR_ARGS: {"tilt_position": 50},
},
],
)
async def test_cover_without_tilt(hass, mock_device):
"""Test a cover with no tilt."""
mock_device.has_tilt = False
await create_entity_from_device(hass, mock_device)
await hass.services.async_call(
"cover", "open_cover_tilt", {"entity_id": "cover.name"}, blocking=True
)
await hass.async_block_till_done()
mock_device.async_open_cover_tilt.assert_not_called()
async def check_cover_position(
hass, update_func, device, closing, opening, closed, expected
):
"""Check that a given position behaves correctly."""
device.is_closing = closing
device.is_opening = opening
device.is_closed = closed
update_func(device)
await hass.async_block_till_done()
entity_state = hass.states.get("cover.name")
assert entity_state.state == expected
async def test_cover_positions(hass, mock_device):
"""Test that the state updates in the various positions."""
update_func = await create_entity_from_device(hass, mock_device)
await check_cover_position(
hass, update_func, mock_device, True, False, False, "closing"
)
await check_cover_position(
hass, update_func, mock_device, False, True, False, "opening"
)
await check_cover_position(
hass, update_func, mock_device, False, False, True, "closed"
)
await check_cover_position(
hass, update_func, mock_device, False, False, False, "open"
)
+75 -8
View File
@@ -3,7 +3,8 @@
from asynctest import call, patch
from homeassistant.components import dynalite
import homeassistant.components.dynalite.const as dynalite
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_ROOM
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@@ -17,8 +18,7 @@ async def test_empty_config(hass):
async def test_async_setup(hass):
"""Test a successful setup."""
host = "1.2.3.4"
"""Test a successful setup with all of the different options."""
with patch(
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
return_value=True,
@@ -30,8 +30,46 @@ async def test_async_setup(hass):
dynalite.DOMAIN: {
dynalite.CONF_BRIDGES: [
{
dynalite.CONF_HOST: host,
dynalite.CONF_AREA: {"1": {dynalite.CONF_NAME: "Name"}},
CONF_HOST: "1.2.3.4",
CONF_PORT: 1234,
dynalite.CONF_AUTO_DISCOVER: True,
dynalite.CONF_POLL_TIMER: 5.5,
dynalite.CONF_AREA: {
"1": {
CONF_NAME: "Name1",
dynalite.CONF_CHANNEL: {"4": {}},
dynalite.CONF_NO_DEFAULT: True,
},
"2": {CONF_NAME: "Name2"},
"3": {
CONF_NAME: "Name3",
dynalite.CONF_TEMPLATE: CONF_ROOM,
},
"4": {
CONF_NAME: "Name4",
dynalite.CONF_TEMPLATE: dynalite.CONF_TIME_COVER,
},
},
dynalite.CONF_DEFAULT: {dynalite.CONF_FADE: 2.3},
dynalite.CONF_ACTIVE: dynalite.ACTIVE_INIT,
dynalite.CONF_PRESET: {
"5": {CONF_NAME: "pres5", dynalite.CONF_FADE: 4.5}
},
dynalite.CONF_TEMPLATE: {
CONF_ROOM: {
dynalite.CONF_ROOM_ON: 6,
dynalite.CONF_ROOM_OFF: 7,
},
dynalite.CONF_TIME_COVER: {
dynalite.CONF_OPEN_PRESET: 8,
dynalite.CONF_CLOSE_PRESET: 9,
dynalite.CONF_STOP_PRESET: 10,
dynalite.CONF_CHANNEL_COVER: 3,
dynalite.CONF_DURATION: 2.2,
dynalite.CONF_TILT_TIME: 3.3,
dynalite.CONF_DEVICE_CLASS: "awning",
},
},
}
]
}
@@ -41,6 +79,35 @@ async def test_async_setup(hass):
assert len(hass.config_entries.async_entries(dynalite.DOMAIN)) == 1
async def test_async_setup_bad_config1(hass):
"""Test a successful with bad config on templates."""
with patch(
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
return_value=True,
):
assert not await async_setup_component(
hass,
dynalite.DOMAIN,
{
dynalite.DOMAIN: {
dynalite.CONF_BRIDGES: [
{
CONF_HOST: "1.2.3.4",
dynalite.CONF_AREA: {
"1": {
dynalite.CONF_TEMPLATE: dynalite.CONF_TIME_COVER,
CONF_NAME: "Name",
dynalite.CONF_ROOM_ON: 7,
}
},
}
]
}
},
)
await hass.async_block_till_done()
async def test_async_setup_bad_config2(hass):
"""Test a successful with bad config on numbers."""
host = "1.2.3.4"
@@ -55,8 +122,8 @@ async def test_async_setup_bad_config2(hass):
dynalite.DOMAIN: {
dynalite.CONF_BRIDGES: [
{
dynalite.CONF_HOST: host,
dynalite.CONF_AREA: {"WRONG": {dynalite.CONF_NAME: "Name"}},
CONF_HOST: host,
dynalite.CONF_AREA: {"WRONG": {CONF_NAME: "Name"}},
}
]
}
@@ -69,7 +136,7 @@ async def test_async_setup_bad_config2(hass):
async def test_unload_entry(hass):
"""Test being able to unload an entry."""
host = "1.2.3.4"
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host})
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={CONF_HOST: host})
entry.add_to_hass(hass)
with patch(
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",