Use setup_test_component_platform
func for cover entity component tests instead of hass.components
(#114010)
* Use `mock_platform` for cover entity component tests instead of `hass.components` * Remove setup fixture and use helper function * Remove is_on from MockCover * Run ruff * Do not override state in `MockCover` in cover tests * Remove is_on from MockCover
This commit is contained in:
parent
3929273b41
commit
34cf0c5721
7 changed files with 241 additions and 274 deletions
77
tests/components/cover/common.py
Normal file
77
tests/components/cover/common.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
"""Collection of helper methods and classes for cover tests."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.cover import CoverEntity, CoverEntityFeature
|
||||
from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
class MockCover(MockEntity, CoverEntity):
|
||||
"""Mock Cover class."""
|
||||
|
||||
def __init__(
|
||||
self, reports_opening_closing: bool | None = None, **values: Any
|
||||
) -> None:
|
||||
"""Initialize a mock cover entity."""
|
||||
|
||||
super().__init__(**values)
|
||||
self._reports_opening_closing = (
|
||||
reports_opening_closing
|
||||
if reports_opening_closing is not None
|
||||
else CoverEntityFeature.STOP in self.supported_features
|
||||
)
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return if the cover is closed or not."""
|
||||
if "state" in self._values and self._values["state"] == STATE_CLOSED:
|
||||
return True
|
||||
|
||||
return self.current_cover_position == 0
|
||||
|
||||
@property
|
||||
def is_opening(self):
|
||||
"""Return if the cover is opening or not."""
|
||||
if "state" in self._values:
|
||||
return self._values["state"] == STATE_OPENING
|
||||
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_closing(self):
|
||||
"""Return if the cover is closing or not."""
|
||||
if "state" in self._values:
|
||||
return self._values["state"] == STATE_CLOSING
|
||||
|
||||
return False
|
||||
|
||||
def open_cover(self, **kwargs) -> None:
|
||||
"""Open cover."""
|
||||
if self._reports_opening_closing:
|
||||
self._values["state"] = STATE_OPENING
|
||||
else:
|
||||
self._values["state"] = STATE_OPEN
|
||||
|
||||
def close_cover(self, **kwargs) -> None:
|
||||
"""Close cover."""
|
||||
if self._reports_opening_closing:
|
||||
self._values["state"] = STATE_CLOSING
|
||||
else:
|
||||
self._values["state"] = STATE_CLOSED
|
||||
|
||||
def stop_cover(self, **kwargs) -> None:
|
||||
"""Stop cover."""
|
||||
assert CoverEntityFeature.STOP in self.supported_features
|
||||
self._values["state"] = STATE_CLOSED if self.is_closed else STATE_OPEN
|
||||
|
||||
@property
|
||||
def current_cover_position(self):
|
||||
"""Return current position of cover."""
|
||||
return self._handle("current_cover_position")
|
||||
|
||||
@property
|
||||
def current_cover_tilt_position(self):
|
||||
"""Return current position of cover tilt."""
|
||||
return self._handle("current_cover_tilt_position")
|
67
tests/components/cover/conftest.py
Normal file
67
tests/components/cover/conftest.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
"""Fixtures for cover entity components tests."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.cover import CoverEntityFeature
|
||||
|
||||
from .common import MockCover
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_cover_entities() -> list[MockCover]:
|
||||
"""Return a list of MockCover instances."""
|
||||
return [
|
||||
MockCover(
|
||||
name="Simple cover",
|
||||
unique_id="unique_cover",
|
||||
supported_features=CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE,
|
||||
),
|
||||
MockCover(
|
||||
name="Set position cover",
|
||||
unique_id="unique_set_pos_cover",
|
||||
current_cover_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.SET_POSITION,
|
||||
),
|
||||
MockCover(
|
||||
name="Simple tilt cover",
|
||||
unique_id="unique_tilt_cover",
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT,
|
||||
),
|
||||
MockCover(
|
||||
name="Set tilt position cover",
|
||||
unique_id="unique_set_pos_tilt_cover",
|
||||
current_cover_tilt_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT
|
||||
| CoverEntityFeature.SET_TILT_POSITION,
|
||||
),
|
||||
MockCover(
|
||||
name="All functions cover",
|
||||
unique_id="unique_all_functions_cover",
|
||||
current_cover_position=50,
|
||||
current_cover_tilt_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.SET_POSITION
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT
|
||||
| CoverEntityFeature.SET_TILT_POSITION,
|
||||
),
|
||||
MockCover(
|
||||
name="Simple with opening/closing cover",
|
||||
unique_id="unique_opening_closing_cover",
|
||||
supported_features=CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE,
|
||||
reports_opening_closing=True,
|
||||
),
|
||||
]
|
|
@ -17,7 +17,9 @@ from tests.common import (
|
|||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.components.cover.common import MockCover
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
|
@ -145,26 +147,20 @@ async def test_get_action_capabilities(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover action."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES.append(
|
||||
platform.MockCover(
|
||||
name="Set position cover",
|
||||
is_on=True,
|
||||
unique_id="unique_set_pos_cover",
|
||||
current_cover_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT,
|
||||
),
|
||||
ent = MockCover(
|
||||
name="Set position cover",
|
||||
unique_id="unique_set_pos_cover",
|
||||
current_cover_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT,
|
||||
)
|
||||
ent = platform.ENTITIES[0]
|
||||
setup_test_component_platform(hass, DOMAIN, [ent])
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -195,26 +191,20 @@ async def test_get_action_capabilities_legacy(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover action."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES.append(
|
||||
platform.MockCover(
|
||||
name="Set position cover",
|
||||
is_on=True,
|
||||
unique_id="unique_set_pos_cover",
|
||||
current_cover_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT,
|
||||
),
|
||||
ent = MockCover(
|
||||
name="Set position cover",
|
||||
unique_id="unique_set_pos_cover",
|
||||
current_cover_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT,
|
||||
)
|
||||
ent = platform.ENTITIES[0]
|
||||
setup_test_component_platform(hass, DOMAIN, [ent])
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -246,12 +236,11 @@ async def test_get_action_capabilities_set_pos(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover action."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[1]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[1]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -297,12 +286,11 @@ async def test_get_action_capabilities_set_tilt_pos(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover action."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[3]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[3]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -354,7 +342,7 @@ async def test_action(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for cover actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
|
@ -441,7 +429,7 @@ async def test_action_legacy(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for cover actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
|
@ -488,7 +476,7 @@ async def test_action_tilt(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for cover tilt actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
|
@ -559,7 +547,7 @@ async def test_action_set_position(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for cover set position actions."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
|
|
|
@ -25,7 +25,9 @@ from tests.common import (
|
|||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.components.cover.common import MockCover
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
|
@ -174,12 +176,11 @@ async def test_get_condition_capabilities(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[0]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[0]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -208,12 +209,11 @@ async def test_get_condition_capabilities_legacy(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[0]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[0]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -245,12 +245,11 @@ async def test_get_condition_capabilities_set_pos(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[1]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[1]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -302,12 +301,12 @@ async def test_get_condition_capabilities_set_tilt_pos(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover condition."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[3]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
|
||||
ent = mock_cover_entities[3]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -560,12 +559,11 @@ async def test_if_position(
|
|||
entity_registry: er.EntityRegistry,
|
||||
calls,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for position conditions."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[1]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[1]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -721,12 +719,11 @@ async def test_if_tilt_position(
|
|||
entity_registry: er.EntityRegistry,
|
||||
calls,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for tilt position conditions."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[3]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[3]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
|
|
@ -28,7 +28,9 @@ from tests.common import (
|
|||
async_get_device_automation_capabilities,
|
||||
async_get_device_automations,
|
||||
async_mock_service,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.components.cover.common import MockCover
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
|
||||
|
@ -175,12 +177,11 @@ async def test_get_trigger_capabilities(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[0]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[0]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -213,12 +214,11 @@ async def test_get_trigger_capabilities_legacy(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[0]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[0]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -252,12 +252,11 @@ async def test_get_trigger_capabilities_set_pos(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[1]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[1]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -317,12 +316,11 @@ async def test_get_trigger_capabilities_set_tilt_pos(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test we get the expected capabilities from a cover trigger."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[3]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[3]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -663,13 +661,12 @@ async def test_if_fires_on_position(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_cover_entities: list[MockCover],
|
||||
calls,
|
||||
enable_custom_integrations: None,
|
||||
) -> None:
|
||||
"""Test for position triggers."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[1]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[1]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -780,11 +777,11 @@ async def test_if_fires_on_position(
|
|||
) == sorted(
|
||||
[
|
||||
(
|
||||
"is_pos_gt_45_lt_90 - device - cover.set_position_cover - closed - open"
|
||||
f"is_pos_gt_45_lt_90 - device - {entry.entity_id} - closed - open"
|
||||
" - None"
|
||||
),
|
||||
"is_pos_lt_90 - device - cover.set_position_cover - closed - open - None",
|
||||
"is_pos_gt_45 - device - cover.set_position_cover - open - closed - None",
|
||||
f"is_pos_lt_90 - device - {entry.entity_id} - closed - open - None",
|
||||
f"is_pos_gt_45 - device - {entry.entity_id} - open - closed - None",
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -799,7 +796,7 @@ async def test_if_fires_on_position(
|
|||
assert len(calls) == 4
|
||||
assert (
|
||||
calls[3].data["some"]
|
||||
== "is_pos_lt_90 - device - cover.set_position_cover - closed - closed - None"
|
||||
== f"is_pos_lt_90 - device - {entry.entity_id} - closed - closed - None"
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
|
@ -809,7 +806,7 @@ async def test_if_fires_on_position(
|
|||
assert len(calls) == 5
|
||||
assert (
|
||||
calls[4].data["some"]
|
||||
== "is_pos_gt_45 - device - cover.set_position_cover - closed - closed - None"
|
||||
== f"is_pos_gt_45 - device - {entry.entity_id} - closed - closed - None"
|
||||
)
|
||||
|
||||
|
||||
|
@ -818,12 +815,11 @@ async def test_if_fires_on_tilt_position(
|
|||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
calls,
|
||||
enable_custom_integrations: None,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test for tilt position triggers."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
ent = platform.ENTITIES[1]
|
||||
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
|
||||
ent = mock_cover_entities[1]
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
@ -936,11 +932,11 @@ async def test_if_fires_on_tilt_position(
|
|||
) == sorted(
|
||||
[
|
||||
(
|
||||
"is_pos_gt_45_lt_90 - device - cover.set_position_cover - closed - open"
|
||||
f"is_pos_gt_45_lt_90 - device - {entry.entity_id} - closed - open"
|
||||
" - None"
|
||||
),
|
||||
"is_pos_lt_90 - device - cover.set_position_cover - closed - open - None",
|
||||
"is_pos_gt_45 - device - cover.set_position_cover - open - closed - None",
|
||||
f"is_pos_lt_90 - device - {entry.entity_id} - closed - open - None",
|
||||
f"is_pos_gt_45 - device - {entry.entity_id} - open - closed - None",
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -955,7 +951,7 @@ async def test_if_fires_on_tilt_position(
|
|||
assert len(calls) == 4
|
||||
assert (
|
||||
calls[3].data["some"]
|
||||
== "is_pos_lt_90 - device - cover.set_position_cover - closed - closed - None"
|
||||
== f"is_pos_lt_90 - device - {entry.entity_id} - closed - closed - None"
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
|
@ -965,5 +961,5 @@ async def test_if_fires_on_tilt_position(
|
|||
assert len(calls) == 5
|
||||
assert (
|
||||
calls[4].data["some"]
|
||||
== "is_pos_gt_45 - device - cover.set_position_cover - closed - closed - None"
|
||||
== f"is_pos_gt_45 - device - {entry.entity_id} - closed - closed - None"
|
||||
)
|
||||
|
|
|
@ -17,14 +17,21 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import help_test_all, import_and_test_deprecated_constant_enum
|
||||
from tests.common import (
|
||||
help_test_all,
|
||||
import_and_test_deprecated_constant_enum,
|
||||
setup_test_component_platform,
|
||||
)
|
||||
from tests.components.cover.common import MockCover
|
||||
|
||||
|
||||
async def test_services(hass: HomeAssistant, enable_custom_integrations: None) -> None:
|
||||
async def test_services(
|
||||
hass: HomeAssistant,
|
||||
mock_cover_entities: list[MockCover],
|
||||
) -> None:
|
||||
"""Test the provided services."""
|
||||
platform = getattr(hass.components, "test.cover")
|
||||
setup_test_component_platform(hass, cover.DOMAIN, mock_cover_entities)
|
||||
|
||||
platform.init()
|
||||
assert await async_setup_component(
|
||||
hass, cover.DOMAIN, {cover.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||
)
|
||||
|
@ -36,7 +43,7 @@ async def test_services(hass: HomeAssistant, enable_custom_integrations: None) -
|
|||
# ent4 = cover with all tilt functions but no position
|
||||
# ent5 = cover with all functions
|
||||
# ent6 = cover with only open/close, but also reports opening/closing
|
||||
ent1, ent2, ent3, ent4, ent5, ent6 = platform.ENTITIES
|
||||
ent1, ent2, ent3, ent4, ent5, ent6 = mock_cover_entities
|
||||
|
||||
# Test init all covers should be open
|
||||
assert is_open(hass, ent1)
|
||||
|
|
|
@ -1,165 +0,0 @@
|
|||
"""Provide a mock cover platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.cover import CoverEntity, CoverEntityFeature
|
||||
from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
ENTITIES = []
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
[]
|
||||
if empty
|
||||
else [
|
||||
MockCover(
|
||||
name="Simple cover",
|
||||
is_on=True,
|
||||
unique_id="unique_cover",
|
||||
supported_features=CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE,
|
||||
),
|
||||
MockCover(
|
||||
name="Set position cover",
|
||||
is_on=True,
|
||||
unique_id="unique_set_pos_cover",
|
||||
current_cover_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.SET_POSITION,
|
||||
),
|
||||
MockCover(
|
||||
name="Simple tilt cover",
|
||||
is_on=True,
|
||||
unique_id="unique_tilt_cover",
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT,
|
||||
),
|
||||
MockCover(
|
||||
name="Set tilt position cover",
|
||||
is_on=True,
|
||||
unique_id="unique_set_pos_tilt_cover",
|
||||
current_cover_tilt_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT
|
||||
| CoverEntityFeature.SET_TILT_POSITION,
|
||||
),
|
||||
MockCover(
|
||||
name="All functions cover",
|
||||
is_on=True,
|
||||
unique_id="unique_all_functions_cover",
|
||||
current_cover_position=50,
|
||||
current_cover_tilt_position=50,
|
||||
supported_features=CoverEntityFeature.OPEN
|
||||
| CoverEntityFeature.CLOSE
|
||||
| CoverEntityFeature.STOP
|
||||
| CoverEntityFeature.SET_POSITION
|
||||
| CoverEntityFeature.OPEN_TILT
|
||||
| CoverEntityFeature.CLOSE_TILT
|
||||
| CoverEntityFeature.STOP_TILT
|
||||
| CoverEntityFeature.SET_TILT_POSITION,
|
||||
),
|
||||
MockCover(
|
||||
name="Simple with opening/closing cover",
|
||||
is_on=True,
|
||||
unique_id="unique_opening_closing_cover",
|
||||
supported_features=CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE,
|
||||
reports_opening_closing=True,
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities_callback, discovery_info=None
|
||||
):
|
||||
"""Return mock entities."""
|
||||
async_add_entities_callback(ENTITIES)
|
||||
|
||||
|
||||
class MockCover(MockEntity, CoverEntity):
|
||||
"""Mock Cover class."""
|
||||
|
||||
def __init__(
|
||||
self, reports_opening_closing: bool | None = None, **values: Any
|
||||
) -> None:
|
||||
"""Initialize a mock cover entity."""
|
||||
|
||||
super().__init__(**values)
|
||||
self._reports_opening_closing = (
|
||||
reports_opening_closing
|
||||
if reports_opening_closing is not None
|
||||
else CoverEntityFeature.STOP in self.supported_features
|
||||
)
|
||||
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return if the cover is closed or not."""
|
||||
if "state" in self._values and self._values["state"] == STATE_CLOSED:
|
||||
return True
|
||||
|
||||
return self.current_cover_position == 0
|
||||
|
||||
@property
|
||||
def is_opening(self):
|
||||
"""Return if the cover is opening or not."""
|
||||
if "state" in self._values:
|
||||
return self._values["state"] == STATE_OPENING
|
||||
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_closing(self):
|
||||
"""Return if the cover is closing or not."""
|
||||
if "state" in self._values:
|
||||
return self._values["state"] == STATE_CLOSING
|
||||
|
||||
return False
|
||||
|
||||
def open_cover(self, **kwargs) -> None:
|
||||
"""Open cover."""
|
||||
if self._reports_opening_closing:
|
||||
self._values["state"] = STATE_OPENING
|
||||
else:
|
||||
self._values["state"] = STATE_OPEN
|
||||
|
||||
def close_cover(self, **kwargs) -> None:
|
||||
"""Close cover."""
|
||||
if self._reports_opening_closing:
|
||||
self._values["state"] = STATE_CLOSING
|
||||
else:
|
||||
self._values["state"] = STATE_CLOSED
|
||||
|
||||
def stop_cover(self, **kwargs) -> None:
|
||||
"""Stop cover."""
|
||||
assert CoverEntityFeature.STOP in self.supported_features
|
||||
self._values["state"] = STATE_CLOSED if self.is_closed else STATE_OPEN
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Fake State."""
|
||||
return CoverEntity.state.fget(self)
|
||||
|
||||
@property
|
||||
def current_cover_position(self):
|
||||
"""Return current position of cover."""
|
||||
return self._handle("current_cover_position")
|
||||
|
||||
@property
|
||||
def current_cover_tilt_position(self):
|
||||
"""Return current position of cover tilt."""
|
||||
return self._handle("current_cover_tilt_position")
|
Loading…
Add table
Reference in a new issue