mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 18:08:37 -04:00
Switchbot Cloud: Add new supported device Ai Art Frame (#160754)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from switchbot_api import BotCommands, Device
|
||||
from switchbot_api.commands import ArtFrameCommands
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.components.switchbot_cloud import SwitchBotAPI
|
||||
@@ -40,7 +42,7 @@ async def test_pressmode_bot(
|
||||
BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||
)
|
||||
mock_send_command.assert_called_once_with(
|
||||
"bot-id-1", BotCommands.PRESS, "command", "default"
|
||||
"bot-id-1", BotCommands.PRESS.value, "command", "default"
|
||||
)
|
||||
|
||||
assert hass.states.get(entity_id).state != STATE_UNKNOWN
|
||||
@@ -65,3 +67,43 @@ async def test_switchmode_bot_no_button_entity(
|
||||
entry = await configure_integration(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
assert not hass.states.async_entity_ids(BUTTON_DOMAIN)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"buttons",
|
||||
[
|
||||
("AI Art Frame", ArtFrameCommands.PREVIOUS),
|
||||
("AI Art Frame", ArtFrameCommands.NEXT),
|
||||
],
|
||||
)
|
||||
async def test_loaded_buttons(
|
||||
hass: HomeAssistant, mock_list_devices, mock_get_status, buttons
|
||||
) -> None:
|
||||
"""Test press."""
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
version="V1.0",
|
||||
deviceId="device_id",
|
||||
deviceName="device_name",
|
||||
deviceType=buttons[0],
|
||||
hubDeviceId="test-hub-id",
|
||||
),
|
||||
]
|
||||
|
||||
mock_get_status.return_value = {
|
||||
"deviceType": buttons[0],
|
||||
}
|
||||
|
||||
entry = await configure_integration(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_id = f"button.device_name_{buttons[1].value}"
|
||||
assert hass.states.get(entity_id).state == STATE_UNKNOWN
|
||||
|
||||
with patch.object(SwitchBotAPI, "send_command") as mock_send_command:
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||
)
|
||||
mock_send_command.assert_called()
|
||||
|
||||
assert hass.states.get(entity_id).state != STATE_UNKNOWN
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
"""Test for the switchbot_cloud image."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from switchbot_api import Device
|
||||
|
||||
from homeassistant.components.switchbot_cloud import DOMAIN
|
||||
from homeassistant.components.switchbot_cloud.image import SwitchBotCloudImage
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import configure_integration
|
||||
|
||||
|
||||
async def test_coordinator_data_is_none(
|
||||
hass: HomeAssistant, mock_list_devices, mock_get_status
|
||||
) -> None:
|
||||
"""Test coordinator data is none."""
|
||||
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
version="V1.0",
|
||||
deviceId="ai-art-frame-id-1",
|
||||
deviceName="ai-art-frame-1",
|
||||
deviceType="AI Art Frame",
|
||||
hubDeviceId="test-hub-id",
|
||||
),
|
||||
]
|
||||
mock_get_status.side_effect = [None, None]
|
||||
entry = await configure_integration(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
entity_id = "image.ai_art_frame_1_display"
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state is STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_async_image(
|
||||
hass: HomeAssistant, mock_list_devices, mock_get_status
|
||||
) -> None:
|
||||
"""Test coordinator data is none."""
|
||||
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
version="V1.0",
|
||||
deviceId="ai-art-frame-id-1",
|
||||
deviceName="ai-art-frame-1",
|
||||
deviceType="AI Art Frame",
|
||||
hubDeviceId="test-hub-id",
|
||||
),
|
||||
]
|
||||
mock_get_status.side_effect = [
|
||||
{
|
||||
"deviceId": "B0E9FEA5D7F0",
|
||||
"deviceType": "AI Art Frame",
|
||||
"hubDeviceId": "B0E9FEA5D7F0",
|
||||
"battery": 0,
|
||||
"displayMode": 1,
|
||||
"imageUrl": "https://p3.itc.cn/images01/20231215/2f2db37e221c4ad3af575254c7769ca1.jpeg",
|
||||
"version": "V0.0-0.5",
|
||||
}
|
||||
]
|
||||
entry = await configure_integration(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
cloud_data = hass.data[DOMAIN][entry.entry_id]
|
||||
device, coordinator = cloud_data.devices.images[0]
|
||||
image_entity = SwitchBotCloudImage(cloud_data.api, device, coordinator)
|
||||
|
||||
# 1. load before refresh
|
||||
await image_entity.async_image()
|
||||
assert image_entity._image_content == b""
|
||||
|
||||
# 2. load after refresh
|
||||
with patch(
|
||||
"homeassistant.components.switchbot_cloud.image.get_file_stream_from_cloud",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_get:
|
||||
mock_get.return_value = b"this is a bytes"
|
||||
image_entity._attr_image_url = (
|
||||
"https://p3.itc.cn/images01/20231215/2f2db37e221c4ad3af575254c7769ca1.jpeg"
|
||||
)
|
||||
await image_entity.async_image()
|
||||
assert image_entity._image_content == mock_get.return_value
|
||||
Reference in New Issue
Block a user