init home-assistant
This commit is contained in:
44
home-assistant/tests/components/sensorpush/__init__.py
Normal file
44
home-assistant/tests/components/sensorpush/__init__.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Tests for the SensorPush integration."""
|
||||
|
||||
from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo
|
||||
|
||||
NOT_SENSOR_PUSH_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="Not it",
|
||||
address="61DE521B-F0BF-9F44-64D4-75BBE1738105",
|
||||
rssi=-63,
|
||||
manufacturer_data={3234: b"\x00\x01"},
|
||||
service_data={},
|
||||
service_uuids=[],
|
||||
source="local",
|
||||
)
|
||||
|
||||
HTW_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="SensorPush HT.w 0CA1",
|
||||
address="61DE521B-F0BF-9F44-64D4-75BBE1738105",
|
||||
rssi=-63,
|
||||
manufacturer_data={11271: b"\xfe\x00\x01"},
|
||||
service_data={},
|
||||
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"],
|
||||
source="local",
|
||||
)
|
||||
|
||||
HTPWX_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="SensorPush HTP.xw F4D",
|
||||
address="4125DDBA-2774-4851-9889-6AADDD4CAC3D",
|
||||
rssi=-56,
|
||||
manufacturer_data={7168: b"\xcd=!\xd1\xb9"},
|
||||
service_data={},
|
||||
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"],
|
||||
source="local",
|
||||
)
|
||||
|
||||
|
||||
HTPWX_EMPTY_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="SensorPush HTP.xw F4D",
|
||||
address="4125DDBA-2774-4851-9889-6AADDD4CAC3D",
|
||||
rssi=-56,
|
||||
manufacturer_data={},
|
||||
service_data={},
|
||||
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"],
|
||||
source="local",
|
||||
)
|
||||
8
home-assistant/tests/components/sensorpush/conftest.py
Normal file
8
home-assistant/tests/components/sensorpush/conftest.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""SensorPush session fixtures."""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_bluetooth(enable_bluetooth: None) -> None:
|
||||
"""Auto mock bluetooth."""
|
||||
205
home-assistant/tests/components/sensorpush/test_config_flow.py
Normal file
205
home-assistant/tests/components/sensorpush/test_config_flow.py
Normal file
@@ -0,0 +1,205 @@
|
||||
"""Test the SensorPush config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.sensorpush.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from . import HTPWX_SERVICE_INFO, HTW_SERVICE_INFO, NOT_SENSOR_PUSH_SERVICE_INFO
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_async_step_bluetooth_valid_device(hass: HomeAssistant) -> None:
|
||||
"""Test discovery via bluetooth with a valid device."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=HTPWX_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "bluetooth_confirm"
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.async_setup_entry", return_value=True
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "HTP.xw F4D"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "4125DDBA-2774-4851-9889-6AADDD4CAC3D"
|
||||
|
||||
|
||||
async def test_async_step_bluetooth_not_sensorpush(hass: HomeAssistant) -> None:
|
||||
"""Test discovery via bluetooth not sensorpush."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=NOT_SENSOR_PUSH_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "not_supported"
|
||||
|
||||
|
||||
async def test_async_step_user_no_devices_found(hass: HomeAssistant) -> None:
|
||||
"""Test setup from service info cache with no devices found."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "no_devices_found"
|
||||
|
||||
|
||||
async def test_async_step_user_with_found_devices(hass: HomeAssistant) -> None:
|
||||
"""Test setup from service info cache with devices found."""
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.config_flow.async_discovered_service_info",
|
||||
return_value=[HTW_SERVICE_INFO],
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.async_setup_entry", return_value=True
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"address": "61DE521B-F0BF-9F44-64D4-75BBE1738105"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "HT.w 0CA1"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "61DE521B-F0BF-9F44-64D4-75BBE1738105"
|
||||
|
||||
|
||||
async def test_async_step_user_device_added_between_steps(hass: HomeAssistant) -> None:
|
||||
"""Test the device gets added via another flow between steps."""
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.config_flow.async_discovered_service_info",
|
||||
return_value=[HTW_SERVICE_INFO],
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="61DE521B-F0BF-9F44-64D4-75BBE1738105",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.async_setup_entry", return_value=True
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"address": "61DE521B-F0BF-9F44-64D4-75BBE1738105"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.ABORT
|
||||
assert result2["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_async_step_user_with_found_devices_already_setup(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test setup from service info cache with devices found."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="61DE521B-F0BF-9F44-64D4-75BBE1738105",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.config_flow.async_discovered_service_info",
|
||||
return_value=[HTW_SERVICE_INFO],
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "no_devices_found"
|
||||
|
||||
|
||||
async def test_async_step_bluetooth_devices_already_setup(hass: HomeAssistant) -> None:
|
||||
"""Test we can't start a flow if there is already a config entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="61DE521B-F0BF-9F44-64D4-75BBE1738105",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=HTW_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_async_step_bluetooth_already_in_progress(hass: HomeAssistant) -> None:
|
||||
"""Test we can't start a flow for the same device twice."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=HTW_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "bluetooth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=HTW_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_in_progress"
|
||||
|
||||
|
||||
async def test_async_step_user_takes_precedence_over_discovery(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test manual setup takes precedence over discovery."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=HTW_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "bluetooth_confirm"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.config_flow.async_discovered_service_info",
|
||||
return_value=[HTW_SERVICE_INFO],
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.sensorpush.async_setup_entry", return_value=True
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"address": "61DE521B-F0BF-9F44-64D4-75BBE1738105"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "HT.w 0CA1"
|
||||
assert result2["data"] == {}
|
||||
assert result2["result"].unique_id == "61DE521B-F0BF-9F44-64D4-75BBE1738105"
|
||||
|
||||
# Verify the original one was aborted
|
||||
assert not hass.config_entries.flow.async_progress(DOMAIN)
|
||||
84
home-assistant/tests/components/sensorpush/test_sensor.py
Normal file
84
home-assistant/tests/components/sensorpush/test_sensor.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""Test the SensorPush sensors."""
|
||||
|
||||
from datetime import timedelta
|
||||
import time
|
||||
|
||||
from homeassistant.components.bluetooth import (
|
||||
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
|
||||
)
|
||||
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
||||
from homeassistant.components.sensorpush.const import DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import HTPWX_EMPTY_SERVICE_INFO, HTPWX_SERVICE_INFO
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
from tests.components.bluetooth import (
|
||||
inject_bluetooth_service_info,
|
||||
patch_all_discovered_devices,
|
||||
patch_bluetooth_time,
|
||||
)
|
||||
|
||||
|
||||
async def test_sensors(hass: HomeAssistant) -> None:
|
||||
"""Test setting up creates the sensors."""
|
||||
start_monotonic = time.monotonic()
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="4125DDBA-2774-4851-9889-6AADDD4CAC3D",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
inject_bluetooth_service_info(hass, HTPWX_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
temp_sensor = hass.states.get("sensor.htp_xw_f4d_temperature")
|
||||
temp_sensor_attributes = temp_sensor.attributes
|
||||
assert temp_sensor.state == "20.11"
|
||||
assert temp_sensor_attributes[ATTR_FRIENDLY_NAME] == "HTP.xw F4D Temperature"
|
||||
assert temp_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "°C"
|
||||
assert temp_sensor_attributes[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Fastforward time without BLE advertisements
|
||||
monotonic_now = start_monotonic + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1
|
||||
|
||||
with (
|
||||
patch_bluetooth_time(
|
||||
monotonic_now,
|
||||
),
|
||||
patch_all_discovered_devices([]),
|
||||
):
|
||||
async_fire_time_changed(
|
||||
hass,
|
||||
dt_util.utcnow()
|
||||
+ timedelta(seconds=FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
temp_sensor = hass.states.get("sensor.htp_xw_f4d_temperature")
|
||||
assert temp_sensor.state == STATE_UNAVAILABLE
|
||||
inject_bluetooth_service_info(hass, HTPWX_EMPTY_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
temp_sensor = hass.states.get("sensor.htp_xw_f4d_temperature")
|
||||
assert temp_sensor.state == "20.11"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
Reference in New Issue
Block a user