1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00
Files
core/tests/components/fluss/test_button.py
T
2026-05-04 14:59:05 +02:00

128 lines
4.0 KiB
Python

"""Tests for the Fluss Buttons."""
from unittest.mock import AsyncMock
from fluss_api import FlussApiClient, FlussApiClientError
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from tests.common import MockConfigEntry, snapshot_platform
async def test_buttons(
hass: HomeAssistant,
mock_api_client: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test setup with multiple devices."""
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_button_press(
hass: HomeAssistant,
mock_api_client: FlussApiClient,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test successful button press."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.device_1"},
blocking=True,
)
mock_api_client.async_trigger_device.assert_called_once_with("2a303030sdj1")
async def test_devices_without_wifi_permission_are_filtered(
hass: HomeAssistant,
mock_api_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Devices whose userPermissions.canUseWiFi is false must not be surfaced."""
mock_api_client.async_get_devices.return_value = {
"devices": [
{
"deviceId": "allowed",
"deviceName": "Allowed",
"userPermissions": {"canUseWiFi": True},
},
{
"deviceId": "blocked",
"deviceName": "Blocked",
"userPermissions": {"canUseWiFi": False},
},
]
}
await setup_integration(hass, mock_config_entry)
assert hass.states.get("button.allowed") is not None
assert hass.states.get("button.blocked") is None
mock_api_client.async_get_device_status.assert_called_once_with("allowed")
async def test_button_unavailable_on_status_error(
hass: HomeAssistant,
mock_api_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Buttons become unavailable when the status call errors."""
mock_api_client.async_get_device_status.side_effect = FlussApiClientError(
"device offline"
)
await setup_integration(hass, mock_config_entry)
assert hass.states.get("button.device_1").state == STATE_UNAVAILABLE
assert hass.states.get("button.device_2").state == STATE_UNAVAILABLE
async def test_button_unavailable_when_internet_disconnected(
hass: HomeAssistant,
mock_api_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Buttons become unavailable when the device reports no internet."""
mock_api_client.async_get_device_status.return_value = {
"status": {"internetConnected": False}
}
await setup_integration(hass, mock_config_entry)
assert hass.states.get("button.device_1").state == STATE_UNAVAILABLE
assert hass.states.get("button.device_2").state == STATE_UNAVAILABLE
async def test_button_press_error(
hass: HomeAssistant,
mock_api_client: FlussApiClient,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test button press with API error."""
await setup_integration(hass, mock_config_entry)
mock_api_client.async_trigger_device.side_effect = FlussApiClientError("API Boom")
with pytest.raises(HomeAssistantError, match="Failed to trigger device: API Boom"):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.device_1"},
blocking=True,
)