74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
|
"""Test the UniFi Protect button platform."""
|
||
|
# pylint: disable=protected-access
|
||
|
from __future__ import annotations
|
||
|
|
||
|
from unittest.mock import AsyncMock, patch
|
||
|
|
||
|
import pytest
|
||
|
from pyunifiprotect.data import Camera
|
||
|
|
||
|
from homeassistant.components.unifiprotect.const import DEFAULT_ATTRIBUTION
|
||
|
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_ENTITY_ID, Platform
|
||
|
from homeassistant.core import HomeAssistant
|
||
|
from homeassistant.helpers import entity_registry as er
|
||
|
|
||
|
from .conftest import MockEntityFixture, enable_entity
|
||
|
|
||
|
|
||
|
@pytest.fixture(name="camera")
|
||
|
async def camera_fixture(
|
||
|
hass: HomeAssistant, mock_entry: MockEntityFixture, mock_camera: Camera
|
||
|
):
|
||
|
"""Fixture for a single camera with only the button platform active, no extra setup."""
|
||
|
|
||
|
camera_obj = mock_camera.copy(deep=True)
|
||
|
camera_obj._api = mock_entry.api
|
||
|
camera_obj.channels[0]._api = mock_entry.api
|
||
|
camera_obj.channels[1]._api = mock_entry.api
|
||
|
camera_obj.channels[2]._api = mock_entry.api
|
||
|
camera_obj.name = "Test Camera"
|
||
|
|
||
|
mock_entry.api.bootstrap.cameras = {
|
||
|
camera_obj.id: camera_obj,
|
||
|
}
|
||
|
|
||
|
with patch("homeassistant.components.unifiprotect.PLATFORMS", [Platform.BUTTON]):
|
||
|
await hass.config_entries.async_setup(mock_entry.entry.entry_id)
|
||
|
await hass.async_block_till_done()
|
||
|
|
||
|
entity_registry = er.async_get(hass)
|
||
|
|
||
|
assert len(hass.states.async_all()) == 0
|
||
|
assert len(entity_registry.entities) == 1
|
||
|
|
||
|
yield (camera_obj, "button.test_camera_reboot_device")
|
||
|
|
||
|
|
||
|
async def test_button(
|
||
|
hass: HomeAssistant,
|
||
|
mock_entry: MockEntityFixture,
|
||
|
camera: tuple[Camera, str],
|
||
|
):
|
||
|
"""Test button entity."""
|
||
|
|
||
|
mock_entry.api.reboot_device = AsyncMock()
|
||
|
|
||
|
unique_id = f"{camera[0].id}"
|
||
|
entity_id = camera[1]
|
||
|
|
||
|
entity_registry = er.async_get(hass)
|
||
|
entity = entity_registry.async_get(entity_id)
|
||
|
assert entity
|
||
|
assert entity.disabled
|
||
|
assert entity.unique_id == unique_id
|
||
|
|
||
|
await enable_entity(hass, mock_entry.entry.entry_id, entity_id)
|
||
|
state = hass.states.get(entity_id)
|
||
|
assert state
|
||
|
assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION
|
||
|
|
||
|
await hass.services.async_call(
|
||
|
"button", "press", {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||
|
)
|
||
|
mock_entry.api.reboot_device.assert_called_once()
|