Add 100% tests coverage for Shelly cover and switch platforms (#45001)
This commit is contained in:
parent
d270f9515b
commit
e3f38942cc
4 changed files with 212 additions and 5 deletions
|
@ -791,11 +791,9 @@ omit =
|
|||
homeassistant/components/shodan/sensor.py
|
||||
homeassistant/components/shelly/__init__.py
|
||||
homeassistant/components/shelly/binary_sensor.py
|
||||
homeassistant/components/shelly/cover.py
|
||||
homeassistant/components/shelly/entity.py
|
||||
homeassistant/components/shelly/light.py
|
||||
homeassistant/components/shelly/sensor.py
|
||||
homeassistant/components/shelly/switch.py
|
||||
homeassistant/components/shelly/utils.py
|
||||
homeassistant/components/sht31/sensor.py
|
||||
homeassistant/components/sigfox/sensor.py
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
"""Test configuration for Shelly."""
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -17,6 +17,7 @@ from tests.common import MockConfigEntry, async_mock_service, mock_device_regist
|
|||
|
||||
MOCK_SETTINGS = {
|
||||
"name": "Test name",
|
||||
"mode": "relay",
|
||||
"device": {
|
||||
"mac": "test-mac",
|
||||
"hostname": "test-host",
|
||||
|
@ -26,13 +27,38 @@ MOCK_SETTINGS = {
|
|||
"coiot": {"update_period": 15},
|
||||
"fw": "20201124-092159/v1.9.0@57ac4ad8",
|
||||
"relays": [{"btn_type": "momentary"}, {"btn_type": "toggle"}],
|
||||
"rollers": [{"positioning": True}],
|
||||
}
|
||||
|
||||
MOCK_BLOCKS = [
|
||||
Mock(sensor_ids={"inputEvent": "S", "inputEventCnt": 2}, channel="0", type="relay")
|
||||
Mock(
|
||||
sensor_ids={"inputEvent": "S", "inputEventCnt": 2},
|
||||
channel="0",
|
||||
type="relay",
|
||||
set_state=AsyncMock(side_effect=lambda turn: {"ison": turn == "on"}),
|
||||
),
|
||||
Mock(
|
||||
sensor_ids={"roller": "stop", "rollerPos": 0},
|
||||
channel="1",
|
||||
type="roller",
|
||||
set_state=AsyncMock(
|
||||
side_effect=lambda go, roller_pos=0: {
|
||||
"current_pos": roller_pos,
|
||||
"state": go,
|
||||
}
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
MOCK_SHELLY = {
|
||||
"mac": "test-mac",
|
||||
"auth": False,
|
||||
"fw": "20201124-092854/v1.9.0@57ac4ad8",
|
||||
"num_outputs": 2,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_coap():
|
||||
"""Mock out coap."""
|
||||
|
@ -68,7 +94,12 @@ async def coap_wrapper(hass):
|
|||
config_entry = MockConfigEntry(domain=DOMAIN, data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
device = Mock(blocks=MOCK_BLOCKS, settings=MOCK_SETTINGS)
|
||||
device = Mock(
|
||||
blocks=MOCK_BLOCKS,
|
||||
settings=MOCK_SETTINGS,
|
||||
shelly=MOCK_SHELLY,
|
||||
update=AsyncMock(),
|
||||
)
|
||||
|
||||
hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}}
|
||||
hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] = {}
|
||||
|
|
93
tests/components/shelly/test_cover.py
Normal file
93
tests/components/shelly/test_cover.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
"""The scene tests for the myq platform."""
|
||||
from homeassistant.components.cover import (
|
||||
ATTR_CURRENT_POSITION,
|
||||
ATTR_POSITION,
|
||||
DOMAIN as COVER_DOMAIN,
|
||||
SERVICE_CLOSE_COVER,
|
||||
SERVICE_OPEN_COVER,
|
||||
SERVICE_SET_COVER_POSITION,
|
||||
SERVICE_STOP_COVER,
|
||||
STATE_CLOSED,
|
||||
STATE_CLOSING,
|
||||
STATE_OPEN,
|
||||
STATE_OPENING,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
|
||||
ROLLER_BLOCK_ID = 1
|
||||
|
||||
|
||||
async def test_services(hass, coap_wrapper, monkeypatch):
|
||||
"""Test device turn on/off services."""
|
||||
assert coap_wrapper
|
||||
|
||||
monkeypatch.setitem(coap_wrapper.device.settings, "mode", "roller")
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, COVER_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_SET_COVER_POSITION,
|
||||
{ATTR_ENTITY_ID: "cover.test_name", ATTR_POSITION: 50},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get("cover.test_name")
|
||||
assert state.attributes[ATTR_CURRENT_POSITION] == 50
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_OPEN_COVER,
|
||||
{ATTR_ENTITY_ID: "cover.test_name"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("cover.test_name").state == STATE_OPENING
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_CLOSE_COVER,
|
||||
{ATTR_ENTITY_ID: "cover.test_name"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("cover.test_name").state == STATE_CLOSING
|
||||
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_STOP_COVER,
|
||||
{ATTR_ENTITY_ID: "cover.test_name"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("cover.test_name").state == STATE_CLOSED
|
||||
|
||||
|
||||
async def test_update(hass, coap_wrapper, monkeypatch):
|
||||
"""Test device update."""
|
||||
assert coap_wrapper
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, COVER_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
monkeypatch.setattr(coap_wrapper.device.blocks[ROLLER_BLOCK_ID], "rollerPos", 0)
|
||||
await hass.helpers.entity_component.async_update_entity("cover.test_name")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("cover.test_name").state == STATE_CLOSED
|
||||
|
||||
monkeypatch.setattr(coap_wrapper.device.blocks[ROLLER_BLOCK_ID], "rollerPos", 100)
|
||||
await hass.helpers.entity_component.async_update_entity("cover.test_name")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("cover.test_name").state == STATE_OPEN
|
||||
|
||||
|
||||
async def test_no_roller_blocks(hass, coap_wrapper, monkeypatch):
|
||||
"""Test device without roller blocks."""
|
||||
assert coap_wrapper
|
||||
|
||||
monkeypatch.setattr(coap_wrapper.device.blocks[ROLLER_BLOCK_ID], "type", None)
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, COVER_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("cover.test_name") is None
|
85
tests/components/shelly/test_switch.py
Normal file
85
tests/components/shelly/test_switch.py
Normal file
|
@ -0,0 +1,85 @@
|
|||
"""The scene tests for the myq platform."""
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
|
||||
RELAY_BLOCK_ID = 0
|
||||
|
||||
|
||||
async def test_services(hass, coap_wrapper):
|
||||
"""Test device turn on/off services."""
|
||||
assert coap_wrapper
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, SWITCH_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "switch.test_name_channel_1"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("switch.test_name_channel_1").state == STATE_ON
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: "switch.test_name_channel_1"},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get("switch.test_name_channel_1").state == STATE_OFF
|
||||
|
||||
|
||||
async def test_update(hass, coap_wrapper, monkeypatch):
|
||||
"""Test device update."""
|
||||
assert coap_wrapper
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, SWITCH_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
monkeypatch.setattr(coap_wrapper.device.blocks[RELAY_BLOCK_ID], "output", False)
|
||||
await hass.helpers.entity_component.async_update_entity(
|
||||
"switch.test_name_channel_1"
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("switch.test_name_channel_1").state == STATE_OFF
|
||||
|
||||
monkeypatch.setattr(coap_wrapper.device.blocks[RELAY_BLOCK_ID], "output", True)
|
||||
await hass.helpers.entity_component.async_update_entity(
|
||||
"switch.test_name_channel_1"
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("switch.test_name_channel_1").state == STATE_ON
|
||||
|
||||
|
||||
async def test_no_relay_blocks(hass, coap_wrapper, monkeypatch):
|
||||
"""Test device without relay blocks."""
|
||||
assert coap_wrapper
|
||||
|
||||
monkeypatch.setattr(coap_wrapper.device.blocks[RELAY_BLOCK_ID], "type", "roller")
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, SWITCH_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("switch.test_name_channel_1") is None
|
||||
|
||||
|
||||
async def test_device_mode_roller(hass, coap_wrapper, monkeypatch):
|
||||
"""Test switch device in roller mode."""
|
||||
assert coap_wrapper
|
||||
|
||||
monkeypatch.setitem(coap_wrapper.device.settings, "mode", "roller")
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(coap_wrapper.entry, SWITCH_DOMAIN)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("switch.test_name_channel_1") is None
|
Loading…
Add table
Reference in a new issue