mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Smarla integration improve tests (#145803)
* Improve smarla integration tests * Do not import descriptions instead use seperate list
This commit is contained in:
parent
980dbf364d
commit
d439bb68eb
@ -6,6 +6,7 @@ from collections.abc import Generator
|
|||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from pysmarlaapi.classes import AuthToken
|
from pysmarlaapi.classes import AuthToken
|
||||||
|
from pysmarlaapi.federwiege.classes import Property, Service
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.smarla.const import DOMAIN
|
from homeassistant.components.smarla.const import DOMAIN
|
||||||
@ -60,4 +61,22 @@ def mock_federwiege(mock_connection: MagicMock) -> Generator[MagicMock]:
|
|||||||
) as mock_federwiege:
|
) as mock_federwiege:
|
||||||
federwiege = mock_federwiege.return_value
|
federwiege = mock_federwiege.return_value
|
||||||
federwiege.serial_number = MOCK_SERIAL_NUMBER
|
federwiege.serial_number = MOCK_SERIAL_NUMBER
|
||||||
|
|
||||||
|
mock_babywiege_service = MagicMock(spec=Service)
|
||||||
|
mock_babywiege_service.props = {
|
||||||
|
"swing_active": MagicMock(spec=Property),
|
||||||
|
"smart_mode": MagicMock(spec=Property),
|
||||||
|
}
|
||||||
|
|
||||||
|
mock_babywiege_service.props["swing_active"].get.return_value = False
|
||||||
|
mock_babywiege_service.props["smart_mode"].get.return_value = False
|
||||||
|
|
||||||
|
federwiege.services = {
|
||||||
|
"babywiege": mock_babywiege_service,
|
||||||
|
}
|
||||||
|
|
||||||
|
federwiege.get_property = MagicMock(
|
||||||
|
side_effect=lambda service, prop: federwiege.services[service].props[prop]
|
||||||
|
)
|
||||||
|
|
||||||
yield federwiege
|
yield federwiege
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from pysmarlaapi.federwiege.classes import Property
|
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
@ -22,26 +21,28 @@ from . import setup_integration, update_property_listeners
|
|||||||
|
|
||||||
from tests.common import MockConfigEntry, snapshot_platform
|
from tests.common import MockConfigEntry, snapshot_platform
|
||||||
|
|
||||||
|
SWITCH_ENTITIES = [
|
||||||
@pytest.fixture
|
{
|
||||||
def mock_switch_property() -> MagicMock:
|
"entity_id": "switch.smarla",
|
||||||
"""Mock a switch property."""
|
"service": "babywiege",
|
||||||
mock = MagicMock(spec=Property)
|
"property": "swing_active",
|
||||||
mock.get.return_value = False
|
},
|
||||||
return mock
|
{
|
||||||
|
"entity_id": "switch.smarla_smart_mode",
|
||||||
|
"service": "babywiege",
|
||||||
|
"property": "smart_mode",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def test_entities(
|
async def test_entities(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_federwiege: MagicMock,
|
mock_federwiege: MagicMock,
|
||||||
mock_switch_property: MagicMock,
|
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the Smarla entities."""
|
"""Test the Smarla entities."""
|
||||||
mock_federwiege.get_property.return_value = mock_switch_property
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch("homeassistant.components.smarla.PLATFORMS", [Platform.SWITCH]),
|
patch("homeassistant.components.smarla.PLATFORMS", [Platform.SWITCH]),
|
||||||
):
|
):
|
||||||
@ -59,45 +60,55 @@ async def test_entities(
|
|||||||
(SERVICE_TURN_OFF, False),
|
(SERVICE_TURN_OFF, False),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@pytest.mark.parametrize("entity_info", SWITCH_ENTITIES)
|
||||||
async def test_switch_action(
|
async def test_switch_action(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
mock_federwiege: MagicMock,
|
mock_federwiege: MagicMock,
|
||||||
mock_switch_property: MagicMock,
|
entity_info: dict[str, str],
|
||||||
service: str,
|
service: str,
|
||||||
parameter: bool,
|
parameter: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test Smarla Switch on/off behavior."""
|
"""Test Smarla Switch on/off behavior."""
|
||||||
mock_federwiege.get_property.return_value = mock_switch_property
|
|
||||||
|
|
||||||
assert await setup_integration(hass, mock_config_entry)
|
assert await setup_integration(hass, mock_config_entry)
|
||||||
|
|
||||||
|
mock_switch_property = mock_federwiege.get_property(
|
||||||
|
entity_info["service"], entity_info["property"]
|
||||||
|
)
|
||||||
|
|
||||||
|
entity_id = entity_info["entity_id"]
|
||||||
|
|
||||||
# Turn on
|
# Turn on
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SWITCH_DOMAIN,
|
SWITCH_DOMAIN,
|
||||||
service,
|
service,
|
||||||
{ATTR_ENTITY_ID: "switch.smarla"},
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
mock_switch_property.set.assert_called_once_with(parameter)
|
mock_switch_property.set.assert_called_once_with(parameter)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("entity_info", SWITCH_ENTITIES)
|
||||||
async def test_switch_state_update(
|
async def test_switch_state_update(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
mock_federwiege: MagicMock,
|
mock_federwiege: MagicMock,
|
||||||
mock_switch_property: MagicMock,
|
entity_info: dict[str, str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test Smarla Switch callback."""
|
"""Test Smarla Switch callback."""
|
||||||
mock_federwiege.get_property.return_value = mock_switch_property
|
|
||||||
|
|
||||||
assert await setup_integration(hass, mock_config_entry)
|
assert await setup_integration(hass, mock_config_entry)
|
||||||
|
|
||||||
assert hass.states.get("switch.smarla").state == STATE_OFF
|
mock_switch_property = mock_federwiege.get_property(
|
||||||
|
entity_info["service"], entity_info["property"]
|
||||||
|
)
|
||||||
|
|
||||||
|
entity_id = entity_info["entity_id"]
|
||||||
|
|
||||||
|
assert hass.states.get(entity_id).state == STATE_OFF
|
||||||
|
|
||||||
mock_switch_property.get.return_value = True
|
mock_switch_property.get.return_value = True
|
||||||
|
|
||||||
await update_property_listeners(mock_switch_property)
|
await update_property_listeners(mock_switch_property)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert hass.states.get("switch.smarla").state == STATE_ON
|
assert hass.states.get(entity_id).state == STATE_ON
|
||||||
|
Loading…
x
Reference in New Issue
Block a user