Skip to content

Commit

Permalink
feat: Add tests for migrating config files from V1 and V2
Browse files Browse the repository at this point in the history
  • Loading branch information
sopelj committed Feb 25, 2024
1 parent 2ed4090 commit 376fe9a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Bluetooth Proxies seem to be far more reliable now, so, if you have that option
Ensure you have at least one [Bluetooth Proxy](https://www.home-assistant.io/integrations/bluetooth/#remote-adapters-bluetooth-proxies) setup with Home Assistant that supports **active connections**.
Currently, that only includes ESPHome proxies with firmware 2022.9.3 or later. In theory, you can have multiple proxies setup throughout the house as well.

### Option 2 - [Home Assistant's Bluetooth Integration](https://www.home-assistant.io/integrations/bluetooth/) with integrated adapter or dongle
#### Option 2 - [Home Assistant's Bluetooth Integration](https://www.home-assistant.io/integrations/bluetooth/) with integrated adapter or dongle

Ensure you have the [Home Assistant Bluetooth Integration](https://www.home-assistant.io/integrations/bluetooth/) enabled and have integrated Bluetooth or Bluetooth Dongle setup with it.
It is recommended to use one of the [officially supported adapters](https://www.home-assistant.io/integrations/bluetooth/#known-working-adapters).
Expand Down
8 changes: 5 additions & 3 deletions custom_components/ember_mug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,15 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
# No migrations to run
return False

config_entry.version = 3

old_data = {**config_entry.data}
if config_entry.version == 1:
old_data[CONF_ADDRESS] = old_data[CONF_MAC]

config_entry.version = 3
hass.config_entries.async_update_entry(
config_entry,
data={
CONF_ADDRESS: old_data.get(CONF_MAC, old_data.get(CONF_ADDRESS)),
CONF_ADDRESS: old_data[CONF_ADDRESS],
CONF_NAME: old_data[CONF_NAME],
},
options={
Expand Down
12 changes: 10 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from unittest.mock import patch

from bleak.backends.scanner import AdvertisementData, BLEDevice
from ember_mug.consts import DEFAULT_NAME
from ember_mug.consts import DEFAULT_NAME, TemperatureUnit
from home_assistant_bluetooth import BluetoothServiceInfoBleak
from homeassistant.const import CONF_ADDRESS, CONF_MAC, CONF_NAME, CONF_TEMPERATURE_UNIT

from custom_components.ember_mug import CONF_DEBUG


def patch_async_setup_entry(return_value=True):
"""Patch async setup entry to return True."""
Expand All @@ -24,11 +26,17 @@ def patch_async_setup_entry(return_value=True):
CONF_ADDRESS: TEST_MAC,
CONF_NAME: TEST_MUG_NAME,
}
V1_CONFIG_DATA = {
CONFIG_DATA_V1 = {
CONF_MAC: TEST_MAC,
CONF_NAME: TEST_MUG_NAME,
CONF_TEMPERATURE_UNIT: "°C",
}
CONFIG_DATA_V2 = {
CONF_ADDRESS: TEST_MAC,
CONF_NAME: TEST_MUG_NAME,
CONF_TEMPERATURE_UNIT: TemperatureUnit.CELSIUS,
CONF_DEBUG: True,
}

MUG_SERVICE_INFO = BluetoothServiceInfoBleak(
name=MUG_DEVICE_NAME,
Expand Down
24 changes: 19 additions & 5 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""Test Ember Mug Integration init."""
from typing import Any
from unittest.mock import AsyncMock, Mock, patch

import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.ember_mug import DOMAIN
from custom_components.ember_mug.const import CONFIG_VERSION
from custom_components.ember_mug.const import CONF_DEBUG, CONFIG_VERSION
from tests import (
CONFIG_DATA_V1,
CONFIG_DATA_V2,
DEFAULT_CONFIG_DATA,
MUG_SERVICE_INFO,
TEST_MAC_UNIQUE_ID,
TEST_MUG_NAME,
V1_CONFIG_DATA,
)


Expand Down Expand Up @@ -50,25 +53,35 @@ async def test_init(
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED


@pytest.mark.parametrize(
("start_version", "input_config", "expected_options"),
[
(1, CONFIG_DATA_V1, {CONF_DEBUG: False}),
(2, CONFIG_DATA_V2, {CONF_DEBUG: True}),
],
)
@patch(
"custom_components.ember_mug.bluetooth.async_last_service_info",
return_value=MUG_SERVICE_INFO,
)
@patch("custom_components.ember_mug.EmberMug._update_multiple", return_value=[])
@patch("custom_components.ember_mug.asyncio.Event", new=AsyncMock)
async def test_init_migration(
async def test_init_migration_v1(
mock_setup: Mock,
mock_update_multiple: Mock,
hass: HomeAssistant,
start_version: int,
input_config: dict[str, Any],
expected_options: dict[str, bool],
):
"""Test upgrading from V1 config to V3."""
mock_config_entry = MockConfigEntry(
domain=DOMAIN,
title=TEST_MUG_NAME,
data=V1_CONFIG_DATA,
data=input_config,
options=None,
unique_id=TEST_MAC_UNIQUE_ID,
version=1,
version=start_version,
)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
Expand All @@ -77,3 +90,4 @@ async def test_init_migration(
assert mock_config_entry.state is ConfigEntryState.LOADED
assert mock_config_entry.version == CONFIG_VERSION
assert mock_config_entry.data == DEFAULT_CONFIG_DATA
assert mock_config_entry.options == expected_options

0 comments on commit 376fe9a

Please sign in to comment.