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 @callback
def _async_get_device_bridge( def _async_get_device_bridge(
hass: HomeAssistant, data: dict[str, Any] hass: HomeAssistant, data: Mapping[str, Any]
) -> SamsungTVBridge: ) -> SamsungTVBridge:
"""Get device bridge.""" """Get device bridge."""
return SamsungTVBridge.get_bridge( return SamsungTVBridge.get_bridge(
@ -171,20 +171,18 @@ async def _async_create_bridge_with_updated_data(
hass: HomeAssistant, entry: SamsungTVConfigEntry hass: HomeAssistant, entry: SamsungTVConfigEntry
) -> SamsungTVBridge: ) -> SamsungTVBridge:
"""Create a bridge object and update any missing data in the config entry.""" """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] host: str = entry.data[CONF_HOST]
method: str = entry.data[CONF_METHOD] method: str = entry.data[CONF_METHOD]
load_info_attempted = False
info: dict[str, Any] | None = None 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) mac: str | None = entry.data.get(CONF_MAC)
model: str | None = entry.data.get(CONF_MODEL) 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 mac_is_incorrectly_formatted = mac and dr.format_mac(mac) != mac
if ( if not mac or not model or mac_is_incorrectly_formatted:
not mac or not model or mac_is_incorrectly_formatted
) and not load_info_attempted:
info = await bridge.async_device_info() info = await bridge.async_device_info()
if not mac or mac_is_incorrectly_formatted: 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 current_unformatted_mac: str, formatted_mac: str
) -> bool: ) -> bool:
"""Check if two macs are the same but formatted incorrectly.""" """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) current_formatted_mac = format_mac(current_unformatted_mac)
return ( return (
current_formatted_mac == formatted_mac 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" assert entry2.unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4"
@pytest.mark.usefixtures( @pytest.mark.usefixtures("remote_websocket", "rest_api")
"remote_websocket", "rest_api", "remote_encrypted_websocket_failing"
)
async def test_update_missing_mac_unique_id_added_from_dhcp( async def test_update_missing_mac_unique_id_added_from_dhcp(
hass: HomeAssistant, mock_setup_entry: AsyncMock hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None: ) -> None:
"""Test missing mac and unique id added.""" """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) entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View File

@ -1,10 +1,9 @@
"""Tests for the Samsung TV Integration.""" """Tests for the Samsung TV Integration."""
from typing import Any from typing import Any
from unittest.mock import AsyncMock, Mock, patch from unittest.mock import Mock, patch
import pytest import pytest
from samsungtvws.async_remote import SamsungTVWSAsyncRemote
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN 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") @pytest.mark.usefixtures("remote_websocket", "rest_api")
async def test_incorrectly_formatted_mac_fixed(hass: HomeAssistant) -> None: async def test_incorrectly_formatted_mac_fixed(hass: HomeAssistant) -> None:
"""Test incorrectly formatted mac is corrected.""" """Test incorrectly formatted mac is corrected."""
with patch( # Incorrect MAC cleanup introduced in #110599, can be removed in 2026.3
"homeassistant.components.samsungtv.bridge.SamsungTVWSAsyncRemote" await setup_samsungtv_entry(
) as remote_class: hass,
remote = Mock(SamsungTVWSAsyncRemote) {**ENTRYDATA_WEBSOCKET, CONF_MAC: "aabbaaaaaaaa"},
remote.__aenter__ = AsyncMock(return_value=remote) )
remote.__aexit__ = AsyncMock() await hass.async_block_till_done()
remote.token = "123456789"
remote_class.return_value = remote
await setup_samsungtv_entry( config_entries = hass.config_entries.async_entries(DOMAIN)
hass, assert len(config_entries) == 1
{ assert config_entries[0].data[CONF_MAC] == "aa:bb:aa:aa:aa:aa"
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"