-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for cover and increase test coverage for slide_local (#133515)
- Loading branch information
1 parent
19e6867
commit 8a8be71
Showing
4 changed files
with
303 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# serializer version: 1 | ||
# name: test_all_entities[cover.slide_bedroom-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'cover', | ||
'entity_category': None, | ||
'entity_id': 'cover.slide_bedroom', | ||
'has_entity_name': True, | ||
'hidden_by': None, | ||
'icon': None, | ||
'id': <ANY>, | ||
'labels': set({ | ||
}), | ||
'name': None, | ||
'options': dict({ | ||
}), | ||
'original_device_class': <CoverDeviceClass.CURTAIN: 'curtain'>, | ||
'original_icon': None, | ||
'original_name': None, | ||
'platform': 'slide_local', | ||
'previous_unique_id': None, | ||
'supported_features': <CoverEntityFeature: 15>, | ||
'translation_key': None, | ||
'unique_id': '1234567890ab', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_entities[cover.slide_bedroom-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'assumed_state': True, | ||
'current_position': 100, | ||
'device_class': 'curtain', | ||
'friendly_name': 'slide bedroom', | ||
'supported_features': <CoverEntityFeature: 15>, | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'cover.slide_bedroom', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'open', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
"""Tests for the Slide Local cover platform.""" | ||
|
||
from datetime import timedelta | ||
from unittest.mock import AsyncMock | ||
|
||
from freezegun.api import FrozenDateTimeFactory | ||
from goslideapi.goslideapi import ClientConnectionError | ||
from syrupy import SnapshotAssertion | ||
|
||
from homeassistant.components.cover import ( | ||
ATTR_POSITION, | ||
DOMAIN as COVER_DOMAIN, | ||
SERVICE_CLOSE_COVER, | ||
SERVICE_OPEN_COVER, | ||
SERVICE_SET_COVER_POSITION, | ||
SERVICE_STOP_COVER, | ||
CoverState, | ||
) | ||
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from . import setup_platform | ||
from .const import SLIDE_INFO_DATA | ||
|
||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform | ||
|
||
|
||
async def test_all_entities( | ||
hass: HomeAssistant, | ||
snapshot: SnapshotAssertion, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test all entities.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) | ||
|
||
|
||
async def test_connection_error( | ||
hass: HomeAssistant, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
freezer: FrozenDateTimeFactory, | ||
) -> None: | ||
"""Test connection error.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
mock_slide_api.slide_info.side_effect = [ClientConnectionError, SLIDE_INFO_DATA] | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == STATE_UNAVAILABLE | ||
|
||
freezer.tick(delta=timedelta(minutes=2)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.OPEN | ||
|
||
|
||
async def test_state_change( | ||
hass: HomeAssistant, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
freezer: FrozenDateTimeFactory, | ||
) -> None: | ||
"""Test connection error.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
mock_slide_api.slide_info.side_effect = [ | ||
dict(SLIDE_INFO_DATA, pos=0.0), | ||
dict(SLIDE_INFO_DATA, pos=0.4), | ||
dict(SLIDE_INFO_DATA, pos=1.0), | ||
dict(SLIDE_INFO_DATA, pos=0.8), | ||
] | ||
|
||
freezer.tick(delta=timedelta(minutes=1)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.OPEN | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.CLOSING | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.CLOSED | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.OPENING | ||
|
||
|
||
async def test_open_cover( | ||
hass: HomeAssistant, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test open cover.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
await hass.services.async_call( | ||
COVER_DOMAIN, | ||
SERVICE_OPEN_COVER, | ||
{ | ||
ATTR_ENTITY_ID: "cover.slide_bedroom", | ||
}, | ||
blocking=True, | ||
) | ||
mock_slide_api.slide_open.assert_called_once() | ||
|
||
|
||
async def test_close_cover( | ||
hass: HomeAssistant, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test close cover.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
await hass.services.async_call( | ||
COVER_DOMAIN, | ||
SERVICE_CLOSE_COVER, | ||
{ | ||
ATTR_ENTITY_ID: "cover.slide_bedroom", | ||
}, | ||
blocking=True, | ||
) | ||
mock_slide_api.slide_close.assert_called_once() | ||
|
||
|
||
async def test_stop_cover( | ||
hass: HomeAssistant, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test stop cover.""" | ||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
await hass.services.async_call( | ||
COVER_DOMAIN, | ||
SERVICE_STOP_COVER, | ||
{ | ||
ATTR_ENTITY_ID: "cover.slide_bedroom", | ||
}, | ||
blocking=True, | ||
) | ||
mock_slide_api.slide_stop.assert_called_once() | ||
|
||
|
||
async def test_set_position( | ||
hass: HomeAssistant, | ||
mock_slide_api: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
freezer: FrozenDateTimeFactory, | ||
) -> None: | ||
"""Test set cover position.""" | ||
|
||
await setup_platform(hass, mock_config_entry, [Platform.COVER]) | ||
|
||
mock_slide_api.slide_info.side_effect = [ | ||
dict(SLIDE_INFO_DATA, pos=0.0), | ||
dict(SLIDE_INFO_DATA, pos=1.0), | ||
dict(SLIDE_INFO_DATA, pos=1.0), | ||
dict(SLIDE_INFO_DATA, pos=0.0), | ||
] | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
await hass.services.async_call( | ||
COVER_DOMAIN, | ||
SERVICE_SET_COVER_POSITION, | ||
{ATTR_ENTITY_ID: "cover.slide_bedroom", ATTR_POSITION: 1.0}, | ||
blocking=True, | ||
) | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.CLOSED | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
await hass.services.async_call( | ||
COVER_DOMAIN, | ||
SERVICE_SET_COVER_POSITION, | ||
{ATTR_ENTITY_ID: "cover.slide_bedroom", ATTR_POSITION: 0.0}, | ||
blocking=True, | ||
) | ||
|
||
freezer.tick(delta=timedelta(seconds=15)) | ||
async_fire_time_changed(hass) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get("cover.slide_bedroom").state == CoverState.OPEN | ||
|
||
assert len(mock_slide_api.slide_set_position.mock_calls) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters