hass-core/tests/components/vallox/test_switch.py
Jevgeni Kiski 5453b346dd
Add Vallox bypass locking switch entity (#75857)
* Switch entity

* adding missing function for tests

* Apply suggestions from code review

Co-authored-by: Sebastian Lövdahl <slovdahl@hibox.fi>

* Review

* fix

* Update homeassistant/components/vallox/switch.py

Co-authored-by: Andre Richter <andre-richter@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Adding a separate parameter for the expected state

Co-authored-by: Sebastian Lövdahl <slovdahl@hibox.fi>
Co-authored-by: Andre Richter <andre-richter@users.noreply.github.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-11-18 10:16:54 +01:00

68 lines
2 KiB
Python

"""Tests for Vallox switch platform."""
import pytest
from homeassistant.components.switch.const import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.core import HomeAssistant
from .conftest import patch_metrics, patch_metrics_set
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
"entity_id, metric_key, value, expected_state",
[
("switch.vallox_bypass_locked", "A_CYC_BYPASS_LOCKED", 1, "on"),
("switch.vallox_bypass_locked", "A_CYC_BYPASS_LOCKED", 0, "off"),
],
)
async def test_switch_entities(
entity_id: str,
metric_key: str,
value: int,
expected_state: str,
mock_entry: MockConfigEntry,
hass: HomeAssistant,
) -> None:
"""Test switch entities."""
# Arrange
metrics = {metric_key: value}
# Act
with patch_metrics(metrics=metrics):
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
# Assert
sensor = hass.states.get(entity_id)
assert sensor
assert sensor.state == expected_state
@pytest.mark.parametrize(
"service, metric_key, value",
[
(SERVICE_TURN_ON, "A_CYC_BYPASS_LOCKED", 1),
(SERVICE_TURN_OFF, "A_CYC_BYPASS_LOCKED", 0),
],
)
async def test_bypass_lock_switch_entitity_set(
service: str,
metric_key: str,
value: int,
mock_entry: MockConfigEntry,
hass: HomeAssistant,
) -> None:
"""Test bypass lock switch set."""
# Act
with patch_metrics(metrics={}), patch_metrics_set() as metrics_set:
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
SWITCH_DOMAIN,
service,
service_data={ATTR_ENTITY_ID: "switch.vallox_bypass_locked"},
)
await hass.async_block_till_done()
metrics_set.assert_called_once_with({metric_key: value})