mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 18:08:37 -04:00
Switchbot Cloud: Add Service AI for Art Frame(UploadImage) (#172888)
This commit is contained in:
@@ -121,3 +121,11 @@ LOCK_ULTRA_INFO = Device(
|
||||
deviceType="Smart Lock Ultra",
|
||||
hubDeviceId="test-hub-id",
|
||||
)
|
||||
|
||||
AI_ART_FRAME_DEVICE = Device(
|
||||
version="V1.0",
|
||||
deviceId="AABBCCDDEEFF",
|
||||
deviceName="test-art-frame",
|
||||
deviceType="AI Art Frame",
|
||||
hubDeviceId="test-hub-id",
|
||||
)
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
"""Tests for switchbot_cloud service."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from switchbot_api import SwitchBotAPI
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switchbot_cloud.const import (
|
||||
AI_ART_FRAME_UPLOAD_IMAGE_SERVICE,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from . import AI_ART_FRAME_DEVICE, configure_integration
|
||||
|
||||
|
||||
async def _setup(
|
||||
hass: HomeAssistant,
|
||||
mock_list_devices,
|
||||
mock_get_status,
|
||||
device=AI_ART_FRAME_DEVICE,
|
||||
):
|
||||
"""Load default entry."""
|
||||
mock_list_devices.return_value = [device]
|
||||
mock_get_status.return_value = None
|
||||
with patch("homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.SWITCH]):
|
||||
entry = await configure_integration(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
return entry
|
||||
|
||||
|
||||
async def test_upload_image_success(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_list_devices,
|
||||
mock_get_status,
|
||||
mock_setup_webhook,
|
||||
) -> None:
|
||||
"""Test successful image upload to AI Art Frame."""
|
||||
entry = await _setup(hass, mock_list_devices, mock_get_status)
|
||||
device = device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, "AABBCCDDEEFF")},
|
||||
name="test-art-frame",
|
||||
model="AI Art Frame",
|
||||
)
|
||||
with (
|
||||
patch.object(SwitchBotAPI, "send_command") as mock_send_command,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
AI_ART_FRAME_UPLOAD_IMAGE_SERVICE,
|
||||
{"device_id": device.id, "image_url": "https://example.com/img.jpg"},
|
||||
blocking=True,
|
||||
)
|
||||
mock_send_command.assert_awaited_once_with(
|
||||
device_id="AABBCCDDEEFF",
|
||||
command="uploadImage",
|
||||
command_type="command",
|
||||
parameters={"imageUrl": "https://example.com/img.jpg"},
|
||||
)
|
||||
|
||||
|
||||
async def test_upload_image_no_device_id_raises(
|
||||
hass: HomeAssistant,
|
||||
mock_list_devices,
|
||||
mock_get_status,
|
||||
mock_setup_webhook,
|
||||
) -> None:
|
||||
"""Test service raises when no device_id is provided."""
|
||||
await _setup(hass, mock_list_devices, mock_get_status)
|
||||
with pytest.raises(vol.Invalid):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
AI_ART_FRAME_UPLOAD_IMAGE_SERVICE,
|
||||
{"image_url": "https://example.com/img.jpg"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_device_not_in_registry_skips(
|
||||
hass: HomeAssistant,
|
||||
mock_list_devices,
|
||||
mock_get_status,
|
||||
mock_setup_webhook,
|
||||
) -> None:
|
||||
"""Test service skips when device_id is not found in device registry."""
|
||||
entry = await _setup(hass, mock_list_devices, mock_get_status)
|
||||
with patch.object(
|
||||
entry.runtime_data.api, "send_command", new_callable=AsyncMock
|
||||
) as mock_send:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
AI_ART_FRAME_UPLOAD_IMAGE_SERVICE,
|
||||
{
|
||||
"device_id": "nonexistent-device-id",
|
||||
"image_url": "https://example.com/img.jpg",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
mock_send.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_device_without_mac_raises_service_validation_error(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_list_devices,
|
||||
mock_get_status,
|
||||
mock_setup_webhook,
|
||||
) -> None:
|
||||
"""Test service device without mac."""
|
||||
entry = await _setup(hass, mock_list_devices, mock_get_status)
|
||||
|
||||
device_no_mac = device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={("other_integration", "some-id")},
|
||||
name="no-mac-device",
|
||||
)
|
||||
|
||||
with pytest.raises(ServiceValidationError, match="No valid MAC address obtained"):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
AI_ART_FRAME_UPLOAD_IMAGE_SERVICE,
|
||||
{
|
||||
"device_id": [device_no_mac.id],
|
||||
"image_url": "https://example.com/img.jpg",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
Reference in New Issue
Block a user