2021-12-29 14:38:44 -05:00
|
|
|
"""Test the UniFi Protect button platform."""
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-12-31 17:20:23 -05:00
|
|
|
from unittest.mock import AsyncMock
|
2021-12-29 14:38:44 -05:00
|
|
|
|
|
|
|
import pytest
|
2022-05-20 16:16:01 -04:00
|
|
|
from pyunifiprotect.data.devices import Chime
|
2021-12-29 14:38:44 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-12-31 17:20:23 -05:00
|
|
|
from .conftest import MockEntityFixture, assert_entity_counts, enable_entity
|
2021-12-29 14:38:44 -05:00
|
|
|
|
|
|
|
|
2022-05-20 16:16:01 -04:00
|
|
|
@pytest.fixture(name="chime")
|
|
|
|
async def chime_fixture(
|
|
|
|
hass: HomeAssistant, mock_entry: MockEntityFixture, mock_chime: Chime
|
2021-12-29 14:38:44 -05:00
|
|
|
):
|
2022-01-01 16:23:10 -05:00
|
|
|
"""Fixture for a single camera for testing the button platform."""
|
2021-12-29 14:38:44 -05:00
|
|
|
|
2022-06-20 23:09:13 -04:00
|
|
|
chime_obj = mock_chime.copy()
|
2022-05-20 16:16:01 -04:00
|
|
|
chime_obj._api = mock_entry.api
|
|
|
|
chime_obj.name = "Test Chime"
|
2021-12-29 14:38:44 -05:00
|
|
|
|
2022-05-20 16:16:01 -04:00
|
|
|
mock_entry.api.bootstrap.chimes = {
|
|
|
|
chime_obj.id: chime_obj,
|
2021-12-29 14:38:44 -05:00
|
|
|
}
|
|
|
|
|
2021-12-31 17:20:23 -05:00
|
|
|
await hass.config_entries.async_setup(mock_entry.entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2021-12-29 14:38:44 -05:00
|
|
|
|
2022-05-20 16:16:01 -04:00
|
|
|
assert_entity_counts(hass, Platform.BUTTON, 3, 2)
|
2021-12-29 14:38:44 -05:00
|
|
|
|
2022-05-20 16:16:01 -04:00
|
|
|
return chime_obj
|
2021-12-29 14:38:44 -05:00
|
|
|
|
|
|
|
|
2022-05-20 16:16:01 -04:00
|
|
|
async def test_reboot_button(
|
2021-12-29 14:38:44 -05:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_entry: MockEntityFixture,
|
2022-05-20 16:16:01 -04:00
|
|
|
chime: Chime,
|
2021-12-29 14:38:44 -05:00
|
|
|
):
|
|
|
|
"""Test button entity."""
|
|
|
|
|
|
|
|
mock_entry.api.reboot_device = AsyncMock()
|
|
|
|
|
2022-06-19 10:22:33 -04:00
|
|
|
unique_id = f"{chime.mac}_reboot"
|
2022-05-20 16:16:01 -04:00
|
|
|
entity_id = "button.test_chime_reboot_device"
|
2021-12-29 14:38:44 -05:00
|
|
|
|
|
|
|
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()
|
2022-05-20 16:16:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def test_chime_button(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_entry: MockEntityFixture,
|
|
|
|
chime: Chime,
|
|
|
|
):
|
|
|
|
"""Test button entity."""
|
|
|
|
|
|
|
|
mock_entry.api.play_speaker = AsyncMock()
|
|
|
|
|
2022-06-19 10:22:33 -04:00
|
|
|
unique_id = f"{chime.mac}_play"
|
2022-05-20 16:16:01 -04:00
|
|
|
entity_id = "button.test_chime_play_chime"
|
|
|
|
|
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
entity = entity_registry.async_get(entity_id)
|
|
|
|
assert entity
|
|
|
|
assert not entity.disabled
|
|
|
|
assert entity.unique_id == unique_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.play_speaker.assert_called_once()
|