Support unique_id for Universal Media Player (#77461)

* support unique id

* tests for unique_id

* use unique_id attribute
pull/77937/head
holysoles 2022-09-09 04:50:39 -05:00 committed by GitHub
parent b369c2f54c
commit c3b2e03ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -53,6 +53,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_STATE,
CONF_STATE_TEMPLATE,
CONF_UNIQUE_ID,
EVENT_HOMEASSISTANT_START,
SERVICE_MEDIA_NEXT_TRACK,
SERVICE_MEDIA_PAUSE,
@ -124,6 +125,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Optional(CONF_ATTRS, default={}): vol.Or(
cv.ensure_list(ATTRS_SCHEMA), ATTRS_SCHEMA
),
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_STATE_TEMPLATE): cv.template,
},
@ -146,6 +148,7 @@ async def async_setup_platform(
config.get(CONF_CHILDREN),
config.get(CONF_COMMANDS),
config.get(CONF_ATTRS),
config.get(CONF_UNIQUE_ID),
config.get(CONF_DEVICE_CLASS),
config.get(CONF_STATE_TEMPLATE),
)
@ -165,6 +168,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
children,
commands,
attributes,
unique_id=None,
device_class=None,
state_template=None,
):
@ -183,6 +187,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
self._state_template_result = None
self._state_template = state_template
self._device_class = device_class
self._attr_unique_id = unique_id
async def async_added_to_hass(self) -> None:
"""Subscribe to children and template state changes."""

View File

@ -21,6 +21,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import Context, callback
from homeassistant.helpers import entity_registry
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.setup import async_setup_component
@ -1092,6 +1093,26 @@ async def test_device_class(hass):
assert hass.states.get("media_player.tv").attributes["device_class"] == "tv"
async def test_unique_id(hass):
"""Test unique_id property."""
hass.states.async_set("sensor.test_sensor", "on")
await async_setup_component(
hass,
"media_player",
{
"media_player": {
"platform": "universal",
"name": "tv",
"unique_id": "universal_master_bed_tv",
}
},
)
await hass.async_block_till_done()
er = entity_registry.async_get(hass)
assert er.async_get("media_player.tv").unique_id == "universal_master_bed_tv"
async def test_invalid_state_template(hass):
"""Test invalid state template sets state to None."""
hass.states.async_set("sensor.test_sensor", "on")
@ -1220,3 +1241,4 @@ async def test_reload(hass):
assert (
"device_class" not in hass.states.get("media_player.master_bed_tv").attributes
)
assert "unique_id" not in hass.states.get("media_player.master_bed_tv").attributes