Adjust handling of SamsungTV misaligned MAC (#144810)

* Cleanup SamsungTV misaligned MAC formatting

* Simplify

* One more

* Revert and add comment

* Adjust comment

* One more
This commit is contained in:
epenet 2025-05-14 07:57:33 +02:00 committed by GitHub
parent 9729f1f38b
commit 31847d8cfb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 37 deletions

View File

@ -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:

View File

@ -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

View File

@ -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(

View File

@ -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"