core/tests/components/ohme/test_switch.py
Dan Raper d365092bcc
Add price cap support to Ohme (#140537)
* Add price cap support

* Change service input to box mode

* Add icon for set_price_cap service

* Improve test coverage

* Change ohme service description wording
2025-03-16 14:05:08 +01:00

115 lines
3.0 KiB
Python

"""Tests for switches."""
from unittest.mock import AsyncMock, MagicMock, patch
from syrupy import SnapshotAssertion
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from tests.common import MockConfigEntry, snapshot_platform
async def test_switches(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
) -> None:
"""Test the Ohme switches."""
with patch("homeassistant.components.ohme.PLATFORMS", [Platform.SWITCH]):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_cap_switch_on(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
) -> None:
"""Test the switch turn_on action."""
await setup_integration(hass, mock_config_entry)
mock_client.async_change_price_cap = AsyncMock()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "switch.ohme_home_pro_price_cap",
},
blocking=True,
)
mock_client.async_change_price_cap.assert_called_once_with(True)
async def test_cap_switch_off(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
) -> None:
"""Test the switch turn_off action."""
await setup_integration(hass, mock_config_entry)
mock_client.async_change_price_cap = AsyncMock()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{
ATTR_ENTITY_ID: "switch.ohme_home_pro_price_cap",
},
blocking=True,
)
mock_client.async_change_price_cap.assert_called_once_with(False)
async def test_config_switch_on(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
) -> None:
"""Test the switch turn_on action."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "switch.ohme_home_pro_lock_buttons",
},
blocking=True,
)
assert len(mock_client.async_set_configuration_value.mock_calls) == 1
async def test_config_switch_off(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: MagicMock,
) -> None:
"""Test the switch turn_off action."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{
ATTR_ENTITY_ID: "switch.ohme_home_pro_lock_buttons",
},
blocking=True,
)
assert len(mock_client.async_set_configuration_value.mock_calls) == 1