mirror of
https://github.com/home-assistant/core.git
synced 2026-09-12 18:25:41 -05:00
Add eq3btsmart integration (#109291)
Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
co-authored by
Sid
J. Nick Koston
parent
4adbf7c730
commit
282cbfc048
@@ -0,0 +1 @@
|
||||
"""Tests for the eq3btsmart component."""
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Fixtures for eq3btsmart tests."""
|
||||
|
||||
from bleak.backends.scanner import AdvertisementData
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
|
||||
|
||||
from .const import MAC
|
||||
|
||||
from tests.components.bluetooth import generate_ble_device
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_bluetooth(enable_bluetooth):
|
||||
"""Auto mock bluetooth."""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_service_info():
|
||||
"""Return a BluetoothServiceInfoBleak for use in testing."""
|
||||
return BluetoothServiceInfoBleak(
|
||||
name="CC-RT-BLE",
|
||||
address=MAC,
|
||||
rssi=0,
|
||||
manufacturer_data={},
|
||||
service_data={},
|
||||
service_uuids=[],
|
||||
source="local",
|
||||
connectable=False,
|
||||
time=0,
|
||||
device=generate_ble_device(address=MAC, name="CC-RT-BLE", rssi=0),
|
||||
advertisement=AdvertisementData(
|
||||
local_name="CC-RT-BLE",
|
||||
manufacturer_data={},
|
||||
service_data={},
|
||||
service_uuids=[],
|
||||
rssi=0,
|
||||
tx_power=-127,
|
||||
platform_data=(),
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
"""Constants for the eq3btsmart tests."""
|
||||
|
||||
MAC = "aa:bb:cc:dd:ee:ff"
|
||||
RSSI = -60
|
||||
@@ -0,0 +1,135 @@
|
||||
"""Test the eq3btsmart config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
|
||||
from homeassistant.components.eq3btsmart.const import DOMAIN
|
||||
from homeassistant.const import CONF_MAC
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers.device_registry import format_mac
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from .const import MAC
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_user_flow(hass: HomeAssistant) -> None:
|
||||
"""Test we can handle a regular successflow setup flow."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.eq3btsmart.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_MAC: MAC},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == slugify(MAC)
|
||||
assert result["data"] == {}
|
||||
assert result["context"]["unique_id"] == MAC
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_user_flow_invalid_mac(hass: HomeAssistant) -> None:
|
||||
"""Test we handle invalid mac address."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.eq3btsmart.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_MAC: "invalid"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {CONF_MAC: "invalid_mac_address"}
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_MAC: MAC},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == slugify(MAC)
|
||||
assert result["data"] == {}
|
||||
assert result["context"]["unique_id"] == MAC
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_bluetooth_flow(
|
||||
hass: HomeAssistant, fake_service_info: BluetoothServiceInfoBleak
|
||||
) -> None:
|
||||
"""Test we can handle a bluetooth discovery flow."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=fake_service_info,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.eq3btsmart.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == slugify(MAC)
|
||||
assert result["data"] == {}
|
||||
assert result["context"]["unique_id"] == MAC
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_duplicate_entry(hass: HomeAssistant) -> None:
|
||||
"""Test duplicate setup handling."""
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_MAC: MAC,
|
||||
},
|
||||
unique_id=format_mac(MAC),
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.eq3btsmart.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_MAC: MAC,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert mock_setup_entry.call_count == 0
|
||||
Reference in New Issue
Block a user