From 40239945c1fbef878bcdb26451526d22f18d582f Mon Sep 17 00:00:00 2001 From: G Johansson Date: Fri, 6 Dec 2024 21:01:41 +0100 Subject: [PATCH] Remove not needed name from yale_smart_alarm (#132204) --- .../components/yale_smart_alarm/__init__.py | 12 ++- .../yale_smart_alarm/alarm_control_panel.py | 3 +- .../yale_smart_alarm/config_flow.py | 6 +- .../components/yale_smart_alarm/entity.py | 4 +- tests/components/yale_smart_alarm/conftest.py | 66 ++++++++----- .../snapshots/test_alarm_control_panel.ambr | 10 +- .../snapshots/test_binary_sensor.ambr | 40 ++++---- .../snapshots/test_button.ambr | 10 +- .../yale_smart_alarm/test_button.py | 4 +- .../yale_smart_alarm/test_config_flow.py | 58 ++++------- .../yale_smart_alarm/test_coordinator.py | 19 ++-- .../components/yale_smart_alarm/test_init.py | 99 +++++++++++++++++++ 12 files changed, 211 insertions(+), 120 deletions(-) create mode 100644 tests/components/yale_smart_alarm/test_init.py diff --git a/homeassistant/components/yale_smart_alarm/__init__.py b/homeassistant/components/yale_smart_alarm/__init__.py index b3fcc28ad4933..d67e136be4a10 100644 --- a/homeassistant/components/yale_smart_alarm/__init__.py +++ b/homeassistant/components/yale_smart_alarm/__init__.py @@ -4,7 +4,7 @@ from homeassistant.components.lock import CONF_DEFAULT_CODE, DOMAIN as LOCK_DOMAIN from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_CODE +from homeassistant.const import CONF_CODE, CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -42,6 +42,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bo LOGGER.debug("Migrating from version %s", entry.version) if entry.version == 1: + new_options = entry.options.copy() if config_entry_default_code := entry.options.get(CONF_CODE): entity_reg = er.async_get(hass) entries = er.async_entries_for_config_entry(entity_reg, entry.entry_id) @@ -52,12 +53,15 @@ async def async_migrate_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bo LOCK_DOMAIN, {CONF_DEFAULT_CODE: config_entry_default_code}, ) - new_options = entry.options.copy() del new_options[CONF_CODE] - hass.config_entries.async_update_entry(entry, options=new_options) + hass.config_entries.async_update_entry(entry, options=new_options, version=2) - hass.config_entries.async_update_entry(entry, version=2) + if entry.version == 2 and entry.minor_version == 1: + # Removes name from entry data + new_data = entry.data.copy() + del new_data[CONF_NAME] + hass.config_entries.async_update_entry(entry, data=new_data, minor_version=2) LOGGER.debug("Migration to version %s successful", entry.version) diff --git a/homeassistant/components/yale_smart_alarm/alarm_control_panel.py b/homeassistant/components/yale_smart_alarm/alarm_control_panel.py index 868b186be9db5..8244d96064a2b 100644 --- a/homeassistant/components/yale_smart_alarm/alarm_control_panel.py +++ b/homeassistant/components/yale_smart_alarm/alarm_control_panel.py @@ -15,7 +15,6 @@ AlarmControlPanelEntityFeature, AlarmControlPanelState, ) -from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -84,7 +83,7 @@ async def async_set_alarm(self, command: str, code: str | None = None) -> None: translation_domain=DOMAIN, translation_key="set_alarm", translation_placeholders={ - "name": self.coordinator.config_entry.data[CONF_NAME], + "name": self.coordinator.config_entry.title, "error": str(error), }, ) from error diff --git a/homeassistant/components/yale_smart_alarm/config_flow.py b/homeassistant/components/yale_smart_alarm/config_flow.py index c71b7b33a0872..3ceee367284cc 100644 --- a/homeassistant/components/yale_smart_alarm/config_flow.py +++ b/homeassistant/components/yale_smart_alarm/config_flow.py @@ -15,7 +15,7 @@ ConfigFlowResult, OptionsFlow, ) -from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback import homeassistant.helpers.config_validation as cv @@ -23,7 +23,6 @@ CONF_AREA_ID, CONF_LOCK_CODE_DIGITS, DEFAULT_AREA_ID, - DEFAULT_NAME, DOMAIN, YALE_BASE_ERRORS, ) @@ -67,6 +66,7 @@ class YaleConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Yale integration.""" VERSION = 2 + MINOR_VERSION = 2 @staticmethod @callback @@ -146,7 +146,6 @@ async def async_step_user( if user_input is not None: username = user_input[CONF_USERNAME] password = user_input[CONF_PASSWORD] - name = DEFAULT_NAME area = user_input.get(CONF_AREA_ID, DEFAULT_AREA_ID) errors = await self.hass.async_add_executor_job( @@ -161,7 +160,6 @@ async def async_step_user( data={ CONF_USERNAME: username, CONF_PASSWORD: password, - CONF_NAME: name, CONF_AREA_ID: area, }, ) diff --git a/homeassistant/components/yale_smart_alarm/entity.py b/homeassistant/components/yale_smart_alarm/entity.py index 4020c93de4e3a..2610f54f0a90f 100644 --- a/homeassistant/components/yale_smart_alarm/entity.py +++ b/homeassistant/components/yale_smart_alarm/entity.py @@ -2,7 +2,7 @@ from yalesmartalarmclient import YaleLock -from homeassistant.const import CONF_NAME, CONF_USERNAME +from homeassistant.const import CONF_USERNAME from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo from homeassistant.helpers.entity import Entity from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -61,7 +61,7 @@ def __init__(self, coordinator: YaleDataUpdateCoordinator) -> None: identifiers={(DOMAIN, coordinator.config_entry.data[CONF_USERNAME])}, manufacturer=MANUFACTURER, model=MODEL, - name=coordinator.config_entry.data[CONF_NAME], + name=coordinator.config_entry.title, connections={(CONNECTION_NETWORK_MAC, panel_info["mac"])}, sw_version=panel_info["version"], ) diff --git a/tests/components/yale_smart_alarm/conftest.py b/tests/components/yale_smart_alarm/conftest.py index 7a7abcac67cba..91c64c7a7a7ed 100644 --- a/tests/components/yale_smart_alarm/conftest.py +++ b/tests/components/yale_smart_alarm/conftest.py @@ -20,7 +20,6 @@ ENTRY_CONFIG = { "username": "test-username", "password": "new-test-password", - "name": "Yale Smart Alarm", "area_id": "1", } OPTIONS_CONFIG = {"lock_code_digits": 6} @@ -35,51 +34,64 @@ async def patch_platform_constant() -> list[Platform]: @pytest.fixture async def load_config_entry( hass: HomeAssistant, - get_data: YaleSmartAlarmData, - get_all_data: YaleSmartAlarmData, + get_client: Mock, load_platforms: list[Platform], ) -> tuple[MockConfigEntry, Mock]: """Set up the Yale Smart Living integration in Home Assistant.""" with patch("homeassistant.components.yale_smart_alarm.PLATFORMS", load_platforms): config_entry = MockConfigEntry( + title=ENTRY_CONFIG["username"], domain=DOMAIN, source=SOURCE_USER, data=ENTRY_CONFIG, options=OPTIONS_CONFIG, entry_id="1", unique_id="username", - version=1, + version=2, + minor_version=2, ) config_entry.add_to_hass(hass) - - cycle = get_data.cycle["data"] - data = {"data": cycle["device_status"]} - with patch( "homeassistant.components.yale_smart_alarm.coordinator.YaleSmartAlarmClient", - autospec=True, - ) as mock_client_class: - client = mock_client_class.return_value - client.auth = Mock() - client.auth.get_authenticated = Mock(return_value=data) - client.auth.post_authenticated = Mock(return_value={"code": "000"}) - client.auth.put_authenticated = Mock(return_value={"code": "000"}) - client.lock_api = YaleDoorManAPI(client.auth) - locks = [ - YaleLock(device, lock_api=client.lock_api) - for device in cycle["device_status"] - if device["type"] == YaleLock.DEVICE_TYPE - ] - client.get_locks.return_value = locks - client.get_all.return_value = get_all_data - client.get_information.return_value = get_data - client.get_armed_status.return_value = YALE_STATE_ARM_FULL - + return_value=get_client, + ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - return (config_entry, client) + return (config_entry, get_client) + + +@pytest.fixture(name="get_client") +async def mock_client( + get_data: YaleSmartAlarmData, + get_all_data: YaleSmartAlarmData, +) -> Mock: + """Mock the Yale client.""" + cycle = get_data.cycle["data"] + data = {"data": cycle["device_status"]} + + with patch( + "homeassistant.components.yale_smart_alarm.coordinator.YaleSmartAlarmClient", + autospec=True, + ) as mock_client_class: + client = mock_client_class.return_value + client.auth = Mock() + client.auth.get_authenticated = Mock(return_value=data) + client.auth.post_authenticated = Mock(return_value={"code": "000"}) + client.auth.put_authenticated = Mock(return_value={"code": "000"}) + client.lock_api = YaleDoorManAPI(client.auth) + locks = [ + YaleLock(device, lock_api=client.lock_api) + for device in cycle["device_status"] + if device["type"] == YaleLock.DEVICE_TYPE + ] + client.get_locks.return_value = locks + client.get_all.return_value = get_all_data + client.get_information.return_value = get_data + client.get_armed_status.return_value = YALE_STATE_ARM_FULL + + return client @pytest.fixture(name="loaded_fixture", scope="package") diff --git a/tests/components/yale_smart_alarm/snapshots/test_alarm_control_panel.ambr b/tests/components/yale_smart_alarm/snapshots/test_alarm_control_panel.ambr index 749e62252f3fa..fcdb7baca03c7 100644 --- a/tests/components/yale_smart_alarm/snapshots/test_alarm_control_panel.ambr +++ b/tests/components/yale_smart_alarm/snapshots/test_alarm_control_panel.ambr @@ -1,5 +1,5 @@ # serializer version: 1 -# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.yale_smart_alarm-entry] +# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.test_username-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -11,7 +11,7 @@ 'disabled_by': None, 'domain': 'alarm_control_panel', 'entity_category': None, - 'entity_id': 'alarm_control_panel.yale_smart_alarm', + 'entity_id': 'alarm_control_panel.test_username', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -32,17 +32,17 @@ 'unit_of_measurement': None, }) # --- -# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.yale_smart_alarm-state] +# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.test_username-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'changed_by': None, 'code_arm_required': False, 'code_format': None, - 'friendly_name': 'Yale Smart Alarm', + 'friendly_name': 'test-username', 'supported_features': , }), 'context': , - 'entity_id': 'alarm_control_panel.yale_smart_alarm', + 'entity_id': 'alarm_control_panel.test_username', 'last_changed': , 'last_reported': , 'last_updated': , diff --git a/tests/components/yale_smart_alarm/snapshots/test_binary_sensor.ambr b/tests/components/yale_smart_alarm/snapshots/test_binary_sensor.ambr index ed7e847439c60..e519a880de91e 100644 --- a/tests/components/yale_smart_alarm/snapshots/test_binary_sensor.ambr +++ b/tests/components/yale_smart_alarm/snapshots/test_binary_sensor.ambr @@ -281,7 +281,7 @@ 'state': 'off', }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_battery-entry] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_battery-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -293,7 +293,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': , - 'entity_id': 'binary_sensor.yale_smart_alarm_battery', + 'entity_id': 'binary_sensor.test_username_battery', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -314,21 +314,21 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_battery-state] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_battery-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'problem', - 'friendly_name': 'Yale Smart Alarm Battery', + 'friendly_name': 'test-username Battery', }), 'context': , - 'entity_id': 'binary_sensor.yale_smart_alarm_battery', + 'entity_id': 'binary_sensor.test_username_battery', 'last_changed': , 'last_reported': , 'last_updated': , 'state': 'off', }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_jam-entry] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_jam-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -340,7 +340,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': , - 'entity_id': 'binary_sensor.yale_smart_alarm_jam', + 'entity_id': 'binary_sensor.test_username_jam', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -361,21 +361,21 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_jam-state] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_jam-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'problem', - 'friendly_name': 'Yale Smart Alarm Jam', + 'friendly_name': 'test-username Jam', }), 'context': , - 'entity_id': 'binary_sensor.yale_smart_alarm_jam', + 'entity_id': 'binary_sensor.test_username_jam', 'last_changed': , 'last_reported': , 'last_updated': , 'state': 'off', }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_power_loss-entry] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_power_loss-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -387,7 +387,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': , - 'entity_id': 'binary_sensor.yale_smart_alarm_power_loss', + 'entity_id': 'binary_sensor.test_username_power_loss', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -408,21 +408,21 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_power_loss-state] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_power_loss-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'problem', - 'friendly_name': 'Yale Smart Alarm Power loss', + 'friendly_name': 'test-username Power loss', }), 'context': , - 'entity_id': 'binary_sensor.yale_smart_alarm_power_loss', + 'entity_id': 'binary_sensor.test_username_power_loss', 'last_changed': , 'last_reported': , 'last_updated': , 'state': 'off', }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_tamper-entry] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_tamper-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -434,7 +434,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': , - 'entity_id': 'binary_sensor.yale_smart_alarm_tamper', + 'entity_id': 'binary_sensor.test_username_tamper', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -455,14 +455,14 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_tamper-state] +# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_tamper-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'problem', - 'friendly_name': 'Yale Smart Alarm Tamper', + 'friendly_name': 'test-username Tamper', }), 'context': , - 'entity_id': 'binary_sensor.yale_smart_alarm_tamper', + 'entity_id': 'binary_sensor.test_username_tamper', 'last_changed': , 'last_reported': , 'last_updated': , diff --git a/tests/components/yale_smart_alarm/snapshots/test_button.ambr b/tests/components/yale_smart_alarm/snapshots/test_button.ambr index 8abceb0affa35..951caced17080 100644 --- a/tests/components/yale_smart_alarm/snapshots/test_button.ambr +++ b/tests/components/yale_smart_alarm/snapshots/test_button.ambr @@ -1,5 +1,5 @@ # serializer version: 1 -# name: test_button[load_platforms0][button.yale_smart_alarm_panic_button-entry] +# name: test_button[load_platforms0][button.test_username_panic_button-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -11,7 +11,7 @@ 'disabled_by': None, 'domain': 'button', 'entity_category': None, - 'entity_id': 'button.yale_smart_alarm_panic_button', + 'entity_id': 'button.test_username_panic_button', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -32,13 +32,13 @@ 'unit_of_measurement': None, }) # --- -# name: test_button[load_platforms0][button.yale_smart_alarm_panic_button-state] +# name: test_button[load_platforms0][button.test_username_panic_button-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Yale Smart Alarm Panic button', + 'friendly_name': 'test-username Panic button', }), 'context': , - 'entity_id': 'button.yale_smart_alarm_panic_button', + 'entity_id': 'button.test_username_panic_button', 'last_changed': , 'last_reported': , 'last_updated': , diff --git a/tests/components/yale_smart_alarm/test_button.py b/tests/components/yale_smart_alarm/test_button.py index ad6074345d310..cb28e60ab22af 100644 --- a/tests/components/yale_smart_alarm/test_button.py +++ b/tests/components/yale_smart_alarm/test_button.py @@ -37,7 +37,7 @@ async def test_button( BUTTON_DOMAIN, SERVICE_PRESS, { - ATTR_ENTITY_ID: "button.yale_smart_alarm_panic_button", + ATTR_ENTITY_ID: "button.test_username_panic_button", }, blocking=True, ) @@ -50,7 +50,7 @@ async def test_button( BUTTON_DOMAIN, SERVICE_PRESS, { - ATTR_ENTITY_ID: "button.yale_smart_alarm_panic_button", + ATTR_ENTITY_ID: "button.test_username_panic_button", }, blocking=True, ) diff --git a/tests/components/yale_smart_alarm/test_config_flow.py b/tests/components/yale_smart_alarm/test_config_flow.py index e5b59f7946314..51106751f0371 100644 --- a/tests/components/yale_smart_alarm/test_config_flow.py +++ b/tests/components/yale_smart_alarm/test_config_flow.py @@ -2,7 +2,7 @@ from __future__ import annotations -from unittest.mock import patch +from unittest.mock import Mock, patch import pytest from yalesmartalarmclient.exceptions import AuthenticationError, UnknownError @@ -48,7 +48,6 @@ async def test_form(hass: HomeAssistant) -> None: assert result2["data"] == { "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", } assert len(mock_setup_entry.mock_calls) == 1 @@ -112,7 +111,6 @@ async def test_form_invalid_auth( assert result2["data"] == { "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", } @@ -120,15 +118,16 @@ async def test_form_invalid_auth( async def test_reauth_flow(hass: HomeAssistant) -> None: """Test a reauthentication flow.""" entry = MockConfigEntry( + title="test-username", domain=DOMAIN, unique_id="test-username", data={ "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", }, version=2, + minor_version=2, ) entry.add_to_hass(hass) @@ -159,7 +158,6 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: assert entry.data == { "username": "test-username", "password": "new-test-password", - "name": "Yale Smart Alarm", "area_id": "1", } @@ -181,15 +179,16 @@ async def test_reauth_flow_error( ) -> None: """Test a reauthentication flow.""" entry = MockConfigEntry( + title="test-username", domain=DOMAIN, unique_id="test-username", data={ "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", }, version=2, + minor_version=2, ) entry.add_to_hass(hass) @@ -234,7 +233,6 @@ async def test_reauth_flow_error( assert entry.data == { "username": "test-username", "password": "new-test-password", - "name": "Yale Smart Alarm", "area_id": "1", } @@ -242,15 +240,16 @@ async def test_reauth_flow_error( async def test_reconfigure(hass: HomeAssistant) -> None: """Test reconfigure config flow.""" entry = MockConfigEntry( + title="test-username", domain=DOMAIN, unique_id="test-username", data={ "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", }, version=2, + minor_version=2, ) entry.add_to_hass(hass) @@ -281,7 +280,6 @@ async def test_reconfigure(hass: HomeAssistant) -> None: assert entry.data == { "username": "test-username", "password": "new-test-password", - "name": "Yale Smart Alarm", "area_id": "2", } @@ -289,27 +287,29 @@ async def test_reconfigure(hass: HomeAssistant) -> None: async def test_reconfigure_username_exist(hass: HomeAssistant) -> None: """Test reconfigure config flow abort other username already exist.""" entry = MockConfigEntry( + title="test-username", domain=DOMAIN, unique_id="test-username", data={ "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", }, version=2, + minor_version=2, ) entry.add_to_hass(hass) entry2 = MockConfigEntry( + title="other-username", domain=DOMAIN, unique_id="other-username", data={ "username": "other-username", "password": "test-password", - "name": "Yale Smart Alarm 2", "area_id": "1", }, version=2, + minor_version=2, ) entry2.add_to_hass(hass) @@ -362,7 +362,6 @@ async def test_reconfigure_username_exist(hass: HomeAssistant) -> None: assert result["reason"] == "reconfigure_successful" assert entry.data == { "username": "other-new-username", - "name": "Yale Smart Alarm", "password": "test-password", "area_id": "1", } @@ -382,15 +381,16 @@ async def test_reconfigure_flow_error( ) -> None: """Test a reauthentication flow.""" entry = MockConfigEntry( + title="test-username", domain=DOMAIN, unique_id="test-username", data={ "username": "test-username", "password": "test-password", - "name": "Yale Smart Alarm", "area_id": "1", }, version=2, + minor_version=2, ) entry.add_to_hass(hass) @@ -438,39 +438,17 @@ async def test_reconfigure_flow_error( assert result["reason"] == "reconfigure_successful" assert entry.data == { "username": "test-username", - "name": "Yale Smart Alarm", "password": "new-test-password", "area_id": "1", } -async def test_options_flow(hass: HomeAssistant) -> None: +async def test_options_flow( + hass: HomeAssistant, + load_config_entry: tuple[MockConfigEntry, Mock], +) -> None: """Test options config flow.""" - entry = MockConfigEntry( - domain=DOMAIN, - unique_id="test-username", - data={ - "username": "test-username", - "password": "test-password", - "name": "Yale Smart Alarm", - "area_id": "1", - }, - version=2, - ) - entry.add_to_hass(hass) - - with ( - patch( - "homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient", - return_value=True, - ), - patch( - "homeassistant.components.yale_smart_alarm.async_setup_entry", - return_value=True, - ), - ): - assert await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() + entry = load_config_entry[0] result = await hass.config_entries.options.async_init(entry.entry_id) diff --git a/tests/components/yale_smart_alarm/test_coordinator.py b/tests/components/yale_smart_alarm/test_coordinator.py index 386e4ad72f7eb..8d30e8ad21aea 100644 --- a/tests/components/yale_smart_alarm/test_coordinator.py +++ b/tests/components/yale_smart_alarm/test_coordinator.py @@ -48,7 +48,8 @@ async def test_coordinator_setup_errors( options=OPTIONS_CONFIG, entry_id="1", unique_id="username", - version=1, + version=2, + minor_version=2, ) config_entry.add_to_hass(hass) @@ -61,7 +62,7 @@ async def test_coordinator_setup_errors( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert not state @@ -74,7 +75,7 @@ async def test_coordinator_setup_and_update_errors( client = load_config_entry[1] - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == AlarmControlPanelState.ARMED_AWAY client.reset_mock() @@ -82,7 +83,7 @@ async def test_coordinator_setup_and_update_errors( async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=1)) await hass.async_block_till_done(wait_background_tasks=True) client.get_information.assert_called_once() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == STATE_UNAVAILABLE client.reset_mock() @@ -90,7 +91,7 @@ async def test_coordinator_setup_and_update_errors( async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=2)) await hass.async_block_till_done(wait_background_tasks=True) client.get_information.assert_called_once() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == STATE_UNAVAILABLE client.reset_mock() @@ -98,7 +99,7 @@ async def test_coordinator_setup_and_update_errors( async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=3)) await hass.async_block_till_done(wait_background_tasks=True) client.get_information.assert_called_once() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == STATE_UNAVAILABLE client.reset_mock() @@ -106,7 +107,7 @@ async def test_coordinator_setup_and_update_errors( async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=4)) await hass.async_block_till_done(wait_background_tasks=True) client.get_information.assert_called_once() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == STATE_UNAVAILABLE client.reset_mock() @@ -116,7 +117,7 @@ async def test_coordinator_setup_and_update_errors( async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done(wait_background_tasks=True) client.get_information.assert_called_once() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == AlarmControlPanelState.ARMED_AWAY client.reset_mock() @@ -124,5 +125,5 @@ async def test_coordinator_setup_and_update_errors( async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=6)) await hass.async_block_till_done(wait_background_tasks=True) client.get_information.assert_called_once() - state = hass.states.get("alarm_control_panel.yale_smart_alarm") + state = hass.states.get("alarm_control_panel.test_username") assert state.state == STATE_UNAVAILABLE diff --git a/tests/components/yale_smart_alarm/test_init.py b/tests/components/yale_smart_alarm/test_init.py new file mode 100644 index 0000000000000..c499320c29c69 --- /dev/null +++ b/tests/components/yale_smart_alarm/test_init.py @@ -0,0 +1,99 @@ +"""Test for Yale Smart Alarm component Init.""" + +from __future__ import annotations + +from unittest.mock import Mock, patch + +from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN +from homeassistant.components.yale_smart_alarm.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER, ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from .conftest import ENTRY_CONFIG, OPTIONS_CONFIG + +from tests.common import MockConfigEntry + + +async def test_setup_entry( + hass: HomeAssistant, + get_client: Mock, +) -> None: + """Test setup entry.""" + entry = MockConfigEntry( + title=ENTRY_CONFIG["username"], + domain=DOMAIN, + source=SOURCE_USER, + data=ENTRY_CONFIG, + options=OPTIONS_CONFIG, + entry_id="1", + unique_id="username", + version=2, + minor_version=2, + ) + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.yale_smart_alarm.coordinator.YaleSmartAlarmClient", + return_value=get_client, + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(entry.entry_id) + assert entry.state is ConfigEntryState.NOT_LOADED + + +async def test_migrate_entry( + hass: HomeAssistant, + get_client: Mock, + entity_registry: er.EntityRegistry, +) -> None: + """Test migrate entry unique id.""" + config = { + "username": "test-username", + "password": "new-test-password", + "name": "Yale Smart Alarm", + "area_id": "1", + } + options = {"lock_code_digits": 6, "code": "123456"} + entry = MockConfigEntry( + title=ENTRY_CONFIG["username"], + domain=DOMAIN, + source=SOURCE_USER, + data=config, + options=options, + entry_id="1", + unique_id="username", + version=1, + minor_version=1, + ) + entry.add_to_hass(hass) + lock = entity_registry.async_get_or_create( + LOCK_DOMAIN, + DOMAIN, + "1111", + config_entry=entry, + has_entity_name=True, + original_name="Device1", + ) + + with patch( + "homeassistant.components.yale_smart_alarm.coordinator.YaleSmartAlarmClient", + return_value=get_client, + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.LOADED + assert entry.version == 2 + assert entry.minor_version == 2 + assert entry.data == ENTRY_CONFIG + assert entry.options == OPTIONS_CONFIG + + lock_entity_id = entity_registry.async_get_entity_id(LOCK_DOMAIN, DOMAIN, "1111") + lock = entity_registry.async_get(lock_entity_id) + + assert lock.options == {"lock": {"default_code": "123456"}}