mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Improve cloud binary sensor tests (#106238)
* Clean up cloud binary sensor test * Test remove entity
This commit is contained in:
parent
c41173bb29
commit
087eb86e37
@ -1,44 +1,68 @@
|
||||
"""Tests for the cloud binary sensor."""
|
||||
from unittest.mock import Mock, patch
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from hass_nabucasa.const import DISPATCH_REMOTE_CONNECT, DISPATCH_REMOTE_DISCONNECT
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.discovery import async_load_platform
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_remote_connection_sensor(hass: HomeAssistant) -> None:
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_wait_until() -> Generator[None, None, None]:
|
||||
"""Mock WAIT_UNTIL_CHANGE to execute callback immediately."""
|
||||
with patch("homeassistant.components.cloud.binary_sensor.WAIT_UNTIL_CHANGE", 0):
|
||||
yield
|
||||
|
||||
|
||||
async def test_remote_connection_sensor(
|
||||
hass: HomeAssistant,
|
||||
cloud: MagicMock,
|
||||
entity_registry: EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the remote connection sensor."""
|
||||
entity_id = "binary_sensor.remote_ui"
|
||||
cloud.remote.certificate = None
|
||||
|
||||
assert await async_setup_component(hass, "cloud", {"cloud": {}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("binary_sensor.remote_ui") is None
|
||||
assert hass.states.get(entity_id) is None
|
||||
|
||||
# Fake connection/discovery
|
||||
await async_load_platform(hass, "binary_sensor", "cloud", {}, {"cloud": {}})
|
||||
on_start_callback = cloud.register_on_start.call_args[0][0]
|
||||
await on_start_callback()
|
||||
|
||||
# Mock test env
|
||||
cloud = hass.data["cloud"] = Mock()
|
||||
cloud.remote.certificate = None
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.remote_ui")
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is not None
|
||||
assert state.state == "unavailable"
|
||||
|
||||
with patch("homeassistant.components.cloud.binary_sensor.WAIT_UNTIL_CHANGE", 0):
|
||||
cloud.remote.is_connected = False
|
||||
cloud.remote.certificate = object()
|
||||
async_dispatcher_send(hass, DISPATCHER_REMOTE_UPDATE, {})
|
||||
await hass.async_block_till_done()
|
||||
cloud.remote.is_connected = False
|
||||
cloud.remote.certificate = object()
|
||||
cloud.client.dispatcher_message(DISPATCH_REMOTE_DISCONNECT)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.remote_ui")
|
||||
assert state.state == "off"
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is not None
|
||||
assert state.state == "off"
|
||||
|
||||
cloud.remote.is_connected = True
|
||||
async_dispatcher_send(hass, DISPATCHER_REMOTE_UPDATE, {})
|
||||
await hass.async_block_till_done()
|
||||
cloud.remote.is_connected = True
|
||||
cloud.client.dispatcher_message(DISPATCH_REMOTE_CONNECT)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.remote_ui")
|
||||
assert state.state == "on"
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is not None
|
||||
assert state.state == "on"
|
||||
|
||||
# Test that a state is not set if the entity is removed.
|
||||
entity_registry.async_remove(entity_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(entity_id) is None
|
||||
|
||||
cloud.remote.is_connected = False
|
||||
cloud.client.dispatcher_message(DISPATCH_REMOTE_DISCONNECT)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(entity_id) is None
|
||||
|
Loading…
x
Reference in New Issue
Block a user