init home-assistant
This commit is contained in:
44
home-assistant/tests/components/mopeka/__init__.py
Normal file
44
home-assistant/tests/components/mopeka/__init__.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Tests for the Mopeka integration."""
|
||||
|
||||
from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo
|
||||
|
||||
NOT_MOPEKA_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="Not it",
|
||||
address="aa:bb:cc:dd:ee:ff",
|
||||
rssi=-63,
|
||||
manufacturer_data={3234: b"\x00\x01"},
|
||||
service_data={},
|
||||
service_uuids=[],
|
||||
source="local",
|
||||
)
|
||||
|
||||
PRO_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="",
|
||||
address="aa:bb:cc:dd:ee:ff",
|
||||
rssi=-60,
|
||||
manufacturer_data={89: b"\x08rF\x00@\xe0\xf5\t\xf0\xd8"},
|
||||
service_data={},
|
||||
service_uuids=["0000fee5-0000-1000-8000-00805f9b34fb"],
|
||||
source="local",
|
||||
)
|
||||
|
||||
PRO_UNUSABLE_SIGNAL_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="",
|
||||
address="aa:bb:cc:dd:ee:ff",
|
||||
rssi=-60,
|
||||
manufacturer_data={89: b"\x08rF\x00\x00\xe0\xf5\t\xf0\xd8"},
|
||||
service_data={},
|
||||
service_uuids=["0000fee5-0000-1000-8000-00805f9b34fb"],
|
||||
source="local",
|
||||
)
|
||||
|
||||
|
||||
PRO_GOOD_SIGNAL_SERVICE_INFO = BluetoothServiceInfo(
|
||||
name="",
|
||||
address="aa:bb:cc:dd:ee:ff",
|
||||
rssi=-60,
|
||||
manufacturer_data={89: b"\x08pC\xb6\xc3\xe0\xf5\t\xfa\xe3"},
|
||||
service_data={},
|
||||
service_uuids=["0000fee5-0000-1000-8000-00805f9b34fb"],
|
||||
source="local",
|
||||
)
|
||||
8
home-assistant/tests/components/mopeka/conftest.py
Normal file
8
home-assistant/tests/components/mopeka/conftest.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Mopeka session fixtures."""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_bluetooth(enable_bluetooth: None) -> None:
|
||||
"""Auto mock bluetooth."""
|
||||
239
home-assistant/tests/components/mopeka/test_config_flow.py
Normal file
239
home-assistant/tests/components/mopeka/test_config_flow.py
Normal file
@@ -0,0 +1,239 @@
|
||||
"""Test the Mopeka config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.mopeka.const import CONF_MEDIUM_TYPE, DOMAIN, MediumType
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from . import NOT_MOPEKA_SERVICE_INFO, PRO_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=PRO_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "bluetooth_confirm"
|
||||
|
||||
with patch("homeassistant.components.mopeka.async_setup_entry", return_value=True):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_MEDIUM_TYPE: MediumType.PROPANE.value}
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Pro Plus EEFF"
|
||||
assert result2["data"] == {CONF_MEDIUM_TYPE: MediumType.PROPANE.value}
|
||||
assert result2["result"].unique_id == "aa:bb:cc:dd:ee:ff"
|
||||
|
||||
|
||||
async def test_async_step_bluetooth_not_mopeka(hass: HomeAssistant) -> None:
|
||||
"""Test discovery via bluetooth not mopeka."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=NOT_MOPEKA_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.mopeka.config_flow.async_discovered_service_info",
|
||||
return_value=[PRO_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.mopeka.async_setup_entry", return_value=True):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"address": "aa:bb:cc:dd:ee:ff"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Pro Plus EEFF"
|
||||
assert CONF_MEDIUM_TYPE in result2["data"]
|
||||
assert result2["data"][CONF_MEDIUM_TYPE] in [
|
||||
medium_type.value for medium_type in MediumType
|
||||
]
|
||||
assert result2["result"].unique_id == "aa:bb:cc:dd:ee:ff"
|
||||
|
||||
|
||||
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.mopeka.config_flow.async_discovered_service_info",
|
||||
return_value=[PRO_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="aa:bb:cc:dd:ee:ff",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.mopeka.async_setup_entry", return_value=True):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"address": "aa:bb:cc:dd:ee:ff"},
|
||||
)
|
||||
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="aa:bb:cc:dd:ee:ff",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.mopeka.config_flow.async_discovered_service_info",
|
||||
return_value=[PRO_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="aa:bb:cc:dd:ee:ff",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_BLUETOOTH},
|
||||
data=PRO_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=PRO_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=PRO_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=PRO_SERVICE_INFO,
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "bluetooth_confirm"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.mopeka.config_flow.async_discovered_service_info",
|
||||
return_value=[PRO_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.mopeka.async_setup_entry", return_value=True):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={"address": "aa:bb:cc:dd:ee:ff"},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Pro Plus EEFF"
|
||||
assert CONF_MEDIUM_TYPE in result2["data"]
|
||||
assert result2["data"][CONF_MEDIUM_TYPE] in [
|
||||
medium_type.value for medium_type in MediumType
|
||||
]
|
||||
assert result2["result"].unique_id == "aa:bb:cc:dd:ee:ff"
|
||||
|
||||
# Verify the original one was aborted
|
||||
assert not hass.config_entries.flow.async_progress(DOMAIN)
|
||||
|
||||
|
||||
async def test_async_step_reconfigure_options(hass: HomeAssistant) -> None:
|
||||
"""Test reconfig options: change MediumType from air to fresh water."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="aa:bb:cc:dd:75:10",
|
||||
title="TD40/TD200 7510",
|
||||
data={CONF_MEDIUM_TYPE: MediumType.AIR.value},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert entry.data[CONF_MEDIUM_TYPE] == MediumType.AIR.value
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
schema: vol.Schema = result["data_schema"]
|
||||
medium_type_key = next(
|
||||
iter(key for key in schema.schema if key == CONF_MEDIUM_TYPE)
|
||||
)
|
||||
assert medium_type_key.default() == MediumType.AIR.value
|
||||
|
||||
result2 = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_MEDIUM_TYPE: MediumType.FRESH_WATER.value},
|
||||
)
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
|
||||
# Verify the new configuration
|
||||
assert entry.data[CONF_MEDIUM_TYPE] == MediumType.FRESH_WATER.value
|
||||
123
home-assistant/tests/components/mopeka/test_sensor.py
Normal file
123
home-assistant/tests/components/mopeka/test_sensor.py
Normal file
@@ -0,0 +1,123 @@
|
||||
"""Test the Mopeka sensors."""
|
||||
|
||||
from homeassistant.components.mopeka.const import DOMAIN
|
||||
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
STATE_UNKNOWN,
|
||||
UnitOfLength,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import (
|
||||
PRO_GOOD_SIGNAL_SERVICE_INFO,
|
||||
PRO_SERVICE_INFO,
|
||||
PRO_UNUSABLE_SIGNAL_SERVICE_INFO,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.bluetooth import inject_bluetooth_service_info
|
||||
|
||||
|
||||
async def test_sensors_unusable_signal(hass: HomeAssistant) -> None:
|
||||
"""Test setting up creates the sensors when there is unusable signal."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="aa:bb:cc:dd:ee:ff",
|
||||
)
|
||||
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("sensor")) == 0
|
||||
inject_bluetooth_service_info(hass, PRO_UNUSABLE_SIGNAL_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 4
|
||||
|
||||
temp_sensor = hass.states.get("sensor.pro_plus_eeff_temperature")
|
||||
temp_sensor_attrs = temp_sensor.attributes
|
||||
assert temp_sensor.state == "30"
|
||||
assert temp_sensor_attrs[ATTR_FRIENDLY_NAME] == "Pro Plus EEFF Temperature"
|
||||
assert temp_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
|
||||
assert temp_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
tank_sensor = hass.states.get("sensor.pro_plus_eeff_tank_level")
|
||||
tank_sensor_attrs = tank_sensor.attributes
|
||||
assert tank_sensor.state == STATE_UNKNOWN
|
||||
assert tank_sensor_attrs[ATTR_FRIENDLY_NAME] == "Pro Plus EEFF Tank Level"
|
||||
assert tank_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == UnitOfLength.MILLIMETERS
|
||||
assert tank_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_sensors_poor_signal(hass: HomeAssistant) -> None:
|
||||
"""Test setting up creates the sensors when there is poor signal."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="aa:bb:cc:dd:ee:ff",
|
||||
)
|
||||
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("sensor")) == 0
|
||||
inject_bluetooth_service_info(hass, PRO_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 4
|
||||
|
||||
temp_sensor = hass.states.get("sensor.pro_plus_eeff_temperature")
|
||||
temp_sensor_attrs = temp_sensor.attributes
|
||||
assert temp_sensor.state == "30"
|
||||
assert temp_sensor_attrs[ATTR_FRIENDLY_NAME] == "Pro Plus EEFF Temperature"
|
||||
assert temp_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
|
||||
assert temp_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
tank_sensor = hass.states.get("sensor.pro_plus_eeff_tank_level")
|
||||
tank_sensor_attrs = tank_sensor.attributes
|
||||
assert tank_sensor.state == "0"
|
||||
assert tank_sensor_attrs[ATTR_FRIENDLY_NAME] == "Pro Plus EEFF Tank Level"
|
||||
assert tank_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == UnitOfLength.MILLIMETERS
|
||||
assert tank_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_sensors_good_signal(hass: HomeAssistant) -> None:
|
||||
"""Test setting up creates the sensors when there is good signal."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="aa:bb:cc:dd:ee:ff",
|
||||
)
|
||||
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("sensor")) == 0
|
||||
inject_bluetooth_service_info(hass, PRO_GOOD_SIGNAL_SERVICE_INFO)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all("sensor")) == 4
|
||||
|
||||
temp_sensor = hass.states.get("sensor.pro_plus_eeff_temperature")
|
||||
temp_sensor_attrs = temp_sensor.attributes
|
||||
assert temp_sensor.state == "27"
|
||||
assert temp_sensor_attrs[ATTR_FRIENDLY_NAME] == "Pro Plus EEFF Temperature"
|
||||
assert temp_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
|
||||
assert temp_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
tank_sensor = hass.states.get("sensor.pro_plus_eeff_tank_level")
|
||||
tank_sensor_attrs = tank_sensor.attributes
|
||||
assert tank_sensor.state == "341"
|
||||
assert tank_sensor_attrs[ATTR_FRIENDLY_NAME] == "Pro Plus EEFF Tank Level"
|
||||
assert tank_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == UnitOfLength.MILLIMETERS
|
||||
assert tank_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
Reference in New Issue
Block a user