init home-assistant
This commit is contained in:
108
home-assistant/tests/components/refoss/__init__.py
Normal file
108
home-assistant/tests/components/refoss/__init__.py
Normal file
@@ -0,0 +1,108 @@
|
||||
"""Common helpers for refoss test cases."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
from refoss_ha.discovery import Listener
|
||||
|
||||
from homeassistant.components.refoss.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FakeDiscovery:
|
||||
"""Mock class replacing refoss device discovery."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the class."""
|
||||
self.mock_devices = {"abc": build_device_mock()}
|
||||
self.last_mock_infos = {}
|
||||
self._listeners = []
|
||||
self.sock = None
|
||||
|
||||
def add_listener(self, listener: Listener) -> None:
|
||||
"""Add an event listener."""
|
||||
self._listeners.append(listener)
|
||||
|
||||
async def initialize(self) -> None:
|
||||
"""Initialize socket server."""
|
||||
self.sock = Mock()
|
||||
|
||||
async def broadcast_msg(self, wait_for: int = 0):
|
||||
"""Search for devices, return mocked data."""
|
||||
|
||||
mock_infos = self.mock_devices
|
||||
last_mock_infos = self.last_mock_infos
|
||||
|
||||
new_infos = []
|
||||
updated_infos = []
|
||||
|
||||
for info in mock_infos.values():
|
||||
uuid = info.uuid
|
||||
if uuid not in last_mock_infos:
|
||||
new_infos.append(info)
|
||||
else:
|
||||
last_info = self.last_mock_infos[uuid]
|
||||
if info.inner_ip != last_info.inner_ip:
|
||||
updated_infos.append(info)
|
||||
|
||||
self.last_mock_infos = mock_infos
|
||||
for listener in self._listeners:
|
||||
[await listener.device_found(x) for x in new_infos]
|
||||
[await listener.device_update(x) for x in updated_infos]
|
||||
|
||||
if wait_for:
|
||||
await asyncio.sleep(wait_for)
|
||||
|
||||
return new_infos
|
||||
|
||||
|
||||
def build_device_mock(name="r10", ip="1.1.1.1", mac="aabbcc112233"):
|
||||
"""Build mock device object."""
|
||||
return Mock(
|
||||
uuid="abc",
|
||||
dev_name=name,
|
||||
device_type="r10",
|
||||
fmware_version="1.1.1",
|
||||
hdware_version="1.1.2",
|
||||
inner_ip=ip,
|
||||
port="80",
|
||||
mac=mac,
|
||||
sub_type="eu",
|
||||
channels=[0],
|
||||
)
|
||||
|
||||
|
||||
def build_base_device_mock(name="r10", ip="1.1.1.1", mac="aabbcc112233"):
|
||||
"""Build mock base device object."""
|
||||
mock = Mock(
|
||||
device_info=build_device_mock(name=name, ip=ip, mac=mac),
|
||||
uuid="abc",
|
||||
dev_name=name,
|
||||
device_type="r10",
|
||||
fmware_version="1.1.1",
|
||||
hdware_version="1.1.2",
|
||||
inner_ip=ip,
|
||||
port="80",
|
||||
mac=mac,
|
||||
sub_type="eu",
|
||||
channels=[0],
|
||||
async_handle_update=AsyncMock(),
|
||||
async_turn_on=AsyncMock(),
|
||||
async_turn_off=AsyncMock(),
|
||||
async_toggle=AsyncMock(),
|
||||
)
|
||||
mock.status = {0: True}
|
||||
return mock
|
||||
|
||||
|
||||
async def async_setup_refoss(hass: HomeAssistant) -> MockConfigEntry:
|
||||
"""Set up the refoss platform."""
|
||||
entry = MockConfigEntry(domain=DOMAIN)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
return entry
|
||||
15
home-assistant/tests/components/refoss/conftest.py
Normal file
15
home-assistant/tests/components/refoss/conftest.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""Pytest module configuration."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.refoss.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
71
home-assistant/tests/components/refoss/test_config_flow.py
Normal file
71
home-assistant/tests/components/refoss/test_config_flow.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""Tests for the refoss Integration."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.refoss.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from . import FakeDiscovery, build_base_device_mock
|
||||
|
||||
|
||||
@patch("homeassistant.components.refoss.config_flow.DISCOVERY_TIMEOUT", 0)
|
||||
async def test_creating_entry_sets_up(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test setting up refoss."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.refoss.util.Discovery",
|
||||
return_value=FakeDiscovery(),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.refoss.bridge.async_build_base_device",
|
||||
return_value=build_base_device_mock(),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.refoss.switch.isinstance",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
# Confirmation form
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@patch("homeassistant.components.refoss.config_flow.DISCOVERY_TIMEOUT", 0)
|
||||
async def test_creating_entry_has_no_devices(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test setting up Refoss no devices."""
|
||||
with patch(
|
||||
"homeassistant.components.refoss.util.Discovery",
|
||||
return_value=FakeDiscovery(),
|
||||
) as discovery:
|
||||
discovery.return_value.mock_devices = {}
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
# Confirmation form
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
Reference in New Issue
Block a user