diff --git a/README.md b/README.md index 0954afc..c7e408b 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/custom_components/ember_mug/__init__.py b/custom_components/ember_mug/__init__.py index 2c2952e..2601e49 100644 --- a/custom_components/ember_mug/__init__.py +++ b/custom_components/ember_mug/__init__.py @@ -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={ diff --git a/tests/__init__.py b/tests/__init__.py index 25b81a4..fcd3f98 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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.""" @@ -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, diff --git a/tests/test_init.py b/tests/test_init.py index e0a55b9..a4bf505 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -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, ) @@ -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) @@ -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