From 31847d8cfbd322e64069d254d49724e5f7159acd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 14 May 2025 07:57:33 +0200 Subject: [PATCH] Adjust handling of SamsungTV misaligned MAC (#144810) * Cleanup SamsungTV misaligned MAC formatting * Simplify * One more * Revert and add comment * Adjust comment * One more --- .../components/samsungtv/__init__.py | 12 +++---- .../components/samsungtv/config_flow.py | 1 + .../components/samsungtv/test_config_flow.py | 9 ++--- tests/components/samsungtv/test_init.py | 36 ++++++------------- 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/samsungtv/__init__.py b/homeassistant/components/samsungtv/__init__.py index 0fbd0f6d1e0..f7af5efc899 100644 --- a/homeassistant/components/samsungtv/__init__.py +++ b/homeassistant/components/samsungtv/__init__.py @@ -44,7 +44,7 @@ PLATFORMS = [Platform.MEDIA_PLAYER, Platform.REMOTE] @callback def _async_get_device_bridge( - hass: HomeAssistant, data: dict[str, Any] + hass: HomeAssistant, data: Mapping[str, Any] ) -> SamsungTVBridge: """Get device bridge.""" return SamsungTVBridge.get_bridge( @@ -171,20 +171,18 @@ async def _async_create_bridge_with_updated_data( hass: HomeAssistant, entry: SamsungTVConfigEntry ) -> SamsungTVBridge: """Create a bridge object and update any missing data in the config entry.""" - updated_data: dict[str, str | int] = {} + updated_data: dict[str, str] = {} host: str = entry.data[CONF_HOST] method: str = entry.data[CONF_METHOD] - load_info_attempted = False info: dict[str, Any] | None = None - bridge = _async_get_device_bridge(hass, {**entry.data, **updated_data}) + bridge = _async_get_device_bridge(hass, entry.data) mac: str | None = entry.data.get(CONF_MAC) model: str | None = entry.data.get(CONF_MODEL) + # Incorrect MAC cleanup introduced in #110599, can be removed in 2026.3 mac_is_incorrectly_formatted = mac and dr.format_mac(mac) != mac - if ( - not mac or not model or mac_is_incorrectly_formatted - ) and not load_info_attempted: + if not mac or not model or mac_is_incorrectly_formatted: info = await bridge.async_device_info() if not mac or mac_is_incorrectly_formatted: diff --git a/homeassistant/components/samsungtv/config_flow.py b/homeassistant/components/samsungtv/config_flow.py index c7406cc740f..dbde1ee1ef3 100644 --- a/homeassistant/components/samsungtv/config_flow.py +++ b/homeassistant/components/samsungtv/config_flow.py @@ -96,6 +96,7 @@ def _mac_is_same_with_incorrect_formatting( current_unformatted_mac: str, formatted_mac: str ) -> bool: """Check if two macs are the same but formatted incorrectly.""" + # Incorrect MAC cleanup introduced in #110599, can be removed in 2026.3 current_formatted_mac = format_mac(current_unformatted_mac) return ( current_formatted_mac == formatted_mac diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index f62c3cc1ae8..25c8bf9bab9 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -1301,14 +1301,15 @@ async def test_update_old_entry(hass: HomeAssistant) -> None: assert entry2.unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4" -@pytest.mark.usefixtures( - "remote_websocket", "rest_api", "remote_encrypted_websocket_failing" -) +@pytest.mark.usefixtures("remote_websocket", "rest_api") async def test_update_missing_mac_unique_id_added_from_dhcp( hass: HomeAssistant, mock_setup_entry: AsyncMock ) -> None: """Test missing mac and unique id added.""" - entry = MockConfigEntry(domain=DOMAIN, data=ENTRYDATA_LEGACY, unique_id=None) + # Incorrect MAC cleanup introduced in #110599, can be removed in 2026.3 + entry_data = deepcopy(ENTRYDATA_WEBSOCKET) + del entry_data[CONF_MAC] + entry = MockConfigEntry(domain=DOMAIN, data=entry_data, unique_id=None) entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( diff --git a/tests/components/samsungtv/test_init.py b/tests/components/samsungtv/test_init.py index abafb1854ba..26bfbf328fa 100644 --- a/tests/components/samsungtv/test_init.py +++ b/tests/components/samsungtv/test_init.py @@ -1,10 +1,9 @@ """Tests for the Samsung TV Integration.""" from typing import Any -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import Mock, patch import pytest -from samsungtvws.async_remote import SamsungTVWSAsyncRemote from syrupy.assertion import SnapshotAssertion from homeassistant.components.media_player import DOMAIN as MP_DOMAIN @@ -148,28 +147,13 @@ async def test_reauth_triggered_encrypted(hass: HomeAssistant) -> None: @pytest.mark.usefixtures("remote_websocket", "rest_api") async def test_incorrectly_formatted_mac_fixed(hass: HomeAssistant) -> None: """Test incorrectly formatted mac is corrected.""" - with patch( - "homeassistant.components.samsungtv.bridge.SamsungTVWSAsyncRemote" - ) as remote_class: - remote = Mock(SamsungTVWSAsyncRemote) - remote.__aenter__ = AsyncMock(return_value=remote) - remote.__aexit__ = AsyncMock() - remote.token = "123456789" - remote_class.return_value = remote + # Incorrect MAC cleanup introduced in #110599, can be removed in 2026.3 + await setup_samsungtv_entry( + hass, + {**ENTRYDATA_WEBSOCKET, CONF_MAC: "aabbaaaaaaaa"}, + ) + await hass.async_block_till_done() - await setup_samsungtv_entry( - hass, - { - CONF_HOST: "fake_host", - CONF_NAME: "fake", - CONF_PORT: 8001, - CONF_TOKEN: "123456789", - CONF_METHOD: METHOD_WEBSOCKET, - CONF_MAC: "aabbaaaaaaaa", - }, - ) - await hass.async_block_till_done() - - config_entries = hass.config_entries.async_entries(DOMAIN) - assert len(config_entries) == 1 - assert config_entries[0].data[CONF_MAC] == "aa:bb:aa:aa:aa:aa" + config_entries = hass.config_entries.async_entries(DOMAIN) + assert len(config_entries) == 1 + assert config_entries[0].data[CONF_MAC] == "aa:bb:aa:aa:aa:aa"