From fe85b20502dff82ead74dcf1d93390b8927b5cfb Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 13 Sep 2023 01:24:49 +0200 Subject: [PATCH] SamsungTV: Add unique_id for when missing (legacy models) (#96829) * Add unique_id for when missing (legacy models) * add comment * update tests, thx @epenet --- homeassistant/components/samsungtv/entity.py | 3 +- .../samsungtv/snapshots/test_init.ambr | 55 +++++++++++++++++++ tests/components/samsungtv/test_init.py | 13 ++++- 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 tests/components/samsungtv/snapshots/test_init.ambr diff --git a/homeassistant/components/samsungtv/entity.py b/homeassistant/components/samsungtv/entity.py index e0ecbaac024..2b6373efc24 100644 --- a/homeassistant/components/samsungtv/entity.py +++ b/homeassistant/components/samsungtv/entity.py @@ -21,7 +21,8 @@ class SamsungTVEntity(Entity): self._bridge = bridge self._mac = config_entry.data.get(CONF_MAC) self._attr_name = config_entry.data.get(CONF_NAME) - self._attr_unique_id = config_entry.unique_id + # Fallback for legacy models that doesn't have a API to retrieve MAC or SerialNumber + self._attr_unique_id = config_entry.unique_id or config_entry.entry_id self._attr_device_info = DeviceInfo( # Instead of setting the device name to the entity name, samsungtv # should be updated to set has_entity_name = True diff --git a/tests/components/samsungtv/snapshots/test_init.ambr b/tests/components/samsungtv/snapshots/test_init.ambr new file mode 100644 index 00000000000..f8b11bd864a --- /dev/null +++ b/tests/components/samsungtv/snapshots/test_init.ambr @@ -0,0 +1,55 @@ +# serializer version: 1 +# name: test_setup_updates_from_ssdp + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'tv', + 'friendly_name': 'any', + 'is_volume_muted': False, + 'source_list': list([ + 'TV', + 'HDMI', + ]), + 'supported_features': , + }), + 'context': , + 'entity_id': 'media_player.any', + 'last_changed': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_setup_updates_from_ssdp.1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'source_list': list([ + 'TV', + 'HDMI', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'media_player', + 'entity_category': None, + 'entity_id': 'media_player.any', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'any', + 'platform': 'samsungtv', + 'supported_features': , + 'translation_key': None, + 'unique_id': 'sample-entry-id', + 'unit_of_measurement': None, + }) +# --- diff --git a/tests/components/samsungtv/test_init.py b/tests/components/samsungtv/test_init.py index 7491f3b76b7..526f7a12fed 100644 --- a/tests/components/samsungtv/test_init.py +++ b/tests/components/samsungtv/test_init.py @@ -2,6 +2,7 @@ from unittest.mock import Mock, patch import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.media_player import DOMAIN, MediaPlayerEntityFeature from homeassistant.components.samsungtv.const import ( @@ -30,6 +31,7 @@ from homeassistant.const import ( SERVICE_VOLUME_UP, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from . import setup_samsungtv_entry from .const import ( @@ -115,9 +117,13 @@ async def test_setup_h_j_model( @pytest.mark.usefixtures("remotews", "remoteencws_failing", "rest_api") -async def test_setup_updates_from_ssdp(hass: HomeAssistant) -> None: +async def test_setup_updates_from_ssdp( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: """Test setting up the entry fetches data from ssdp cache.""" - entry = MockConfigEntry(domain="samsungtv", data=MOCK_ENTRYDATA_WS) + entry = MockConfigEntry( + domain="samsungtv", data=MOCK_ENTRYDATA_WS, entry_id="sample-entry-id" + ) entry.add_to_hass(hass) async def _mock_async_get_discovery_info_by_st(hass: HomeAssistant, mock_st: str): @@ -135,7 +141,8 @@ async def test_setup_updates_from_ssdp(hass: HomeAssistant) -> None: await hass.async_block_till_done() await hass.async_block_till_done() - assert hass.states.get("media_player.any") + assert hass.states.get("media_player.any") == snapshot + assert entity_registry.async_get("media_player.any") == snapshot assert ( entry.data[CONF_SSDP_MAIN_TV_AGENT_LOCATION] == "https://fake_host:12345/tv_agent"