SamsungTV: Add unique_id for when missing (legacy models) (#96829)

* Add unique_id for when missing (legacy models)

* add comment

* update tests, thx @epenet
This commit is contained in:
Simone Chemelli 2023-09-13 01:24:49 +02:00 committed by GitHub
parent 5272387bd3
commit fe85b20502
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 4 deletions

View file

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

View file

@ -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': <MediaPlayerEntityFeature: 20413>,
}),
'context': <ANY>,
'entity_id': 'media_player.any',
'last_changed': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'media_player',
'entity_category': None,
'entity_id': 'media_player.any',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <MediaPlayerDeviceClass.TV: 'tv'>,
'original_icon': None,
'original_name': 'any',
'platform': 'samsungtv',
'supported_features': <MediaPlayerEntityFeature: 20413>,
'translation_key': None,
'unique_id': 'sample-entry-id',
'unit_of_measurement': None,
})
# ---

View file

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