Skip to content

Commit

Permalink
enable all advertisement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patman15 committed Dec 26, 2024
1 parent 80a90e7 commit 55540c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ bluetooth-data-tools
pyserial-asyncio
pyudev
pytest-homeassistant-custom-component==0.13.132

kegtron-ble
22 changes: 11 additions & 11 deletions tests/advertisement_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
from .bluetooth import generate_advertisement_data

ADVERTISEMENTS: Final[list] = [
( # conflicting integrated component: https://github.com/patman15/BMS_BLE-HA/issues/123
(
generate_advertisement_data(
local_name="NWJ20221223010330",#\x11",
local_name="NWJ20221223010330\x11",
manufacturer_data={65535: b"0UD7\xa2\xd2"},
service_uuids=["0000ffe0-0000-1000-8000-00805f9b34fb"],
rssi=-56,
),
"ective_bms",
),
# (
# generate_advertisement_data(
# local_name="NWJ20221223010388",#\x11",
# manufacturer_data={65535: b"0UD7b\xec"},
# service_uuids=["0000ffe0-0000-1000-8000-00805f9b34fb"],
# rssi=-47,
# ),
# "ective_bms",
# ),
(
generate_advertisement_data(
local_name="NWJ20221223010388\x11",
manufacturer_data={65535: b"0UD7b\xec"},
service_uuids=["0000ffe0-0000-1000-8000-00805f9b34fb"],
rssi=-47,
),
"ective_bms",
),
(
generate_advertisement_data(
local_name="BatteryOben-00",
Expand Down
31 changes: 14 additions & 17 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def bms_advertisement(request) -> BluetoothServiceInfoBleak:
address: Final[str] = "c0:ff:ee:c0:ff:ee"
return BluetoothServiceInfoBleak(
name=str(dev.local_name),
address=request.param[1],
address=f"{address}_{request.param[1]}",
device=generate_ble_device(address=address, name=dev.local_name),
rssi=dev.rssi,
service_uuids=dev.service_uuids,
Expand All @@ -56,20 +56,17 @@ def bms_advertisement(request) -> BluetoothServiceInfoBleak:
)


async def test_device_discovery(
patch_bleakclient, advertisement: BluetoothServiceInfoBleak, hass: HomeAssistant
async def test_bluetooth_discovery(
hass: HomeAssistant, advertisement: BluetoothServiceInfoBleak
) -> None:
"""Test discovery via bluetooth with a valid device."""
"""Test bluetooth device discovery."""

result: ConfigFlowResult = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_BLUETOOTH},
data=advertisement,
)
inject_bluetooth_service_info_bleak(hass, advertisement)
await hass.async_block_till_done(wait_background_tasks=True)

assert result.get("type") == FlowResultType.FORM
result: ConfigFlowResult = hass.config_entries.flow.async_progress_by_handler(DOMAIN)[0]
assert result.get("step_id") == "bluetooth_confirm"
assert result.get("description_placeholders") == {"name": advertisement.name}
assert result.get("context", {}).get("unique_id") == advertisement.address

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"not": "empty"}
Expand All @@ -78,10 +75,10 @@ async def test_device_discovery(
assert result.get("type") == FlowResultType.CREATE_ENTRY
assert result.get("title") == advertisement.name

# BluetoothServiceInfoBleak contains BMS type in the address, see bms_advertisement
# BluetoothServiceInfoBleak contains BMS type as trailer to the address, see bms_advertisement
assert (
hass.config_entries.async_entries()[1].data["type"]
== f"custom_components.bms_ble.plugins.{advertisement.address}"
== f"custom_components.bms_ble.plugins.{advertisement.address.split('_',1)[-1]}"
)


Expand All @@ -93,7 +90,7 @@ async def test_device_setup(
) -> None:
"""Test discovery via bluetooth with a valid device."""

result = await hass.config_entries.flow.async_init(
result: ConfigFlowResult = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_BLUETOOTH},
data=BTdiscovery,
Expand Down Expand Up @@ -152,7 +149,7 @@ async def test_invalid_plugin(monkeypatch, BTdiscovery, hass: HomeAssistant) ->
"""

monkeypatch.delattr(BaseBMS, "supported")
result = await hass.config_entries.flow.async_init(
result: ConfigFlowResult = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_BLUETOOTH},
data=BTdiscovery,
Expand Down Expand Up @@ -229,7 +226,7 @@ async def test_user_setup(

inject_bluetooth_service_info_bleak(hass, BTdiscovery)

result = await hass.config_entries.flow.async_init(
result: ConfigFlowResult = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") == FlowResultType.FORM
Expand Down Expand Up @@ -295,7 +292,7 @@ def patch_async_current_ids(_self) -> set[str | None]:

inject_bluetooth_service_info_bleak(hass, BTdiscovery)

result = await hass.config_entries.flow.async_init(
result: ConfigFlowResult = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result.get("type") == FlowResultType.ABORT
Expand Down

0 comments on commit 55540c7

Please sign in to comment.