Use server-specific unique_ids for Plex media_players (#28447)

This commit is contained in:
jjlawren 2019-11-01 16:41:26 -05:00 committed by Martin Hjelmare
parent 557e585e56
commit 21d48218aa
2 changed files with 26 additions and 4 deletions

View File

@ -6,7 +6,7 @@ from xml.etree.ElementTree import ParseError
import plexapi.exceptions import plexapi.exceptions
import requests.exceptions import requests.exceptions
from homeassistant.components.media_player import MediaPlayerDevice from homeassistant.components.media_player import DOMAIN as MP_DOMAIN, MediaPlayerDevice
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
MEDIA_TYPE_MOVIE, MEDIA_TYPE_MOVIE,
MEDIA_TYPE_MUSIC, MEDIA_TYPE_MUSIC,
@ -30,6 +30,7 @@ from homeassistant.const import (
) )
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_registry import async_get_registry
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .const import ( from .const import (
@ -56,10 +57,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Plex media_player from a config entry.""" """Set up Plex media_player from a config entry."""
server_id = config_entry.data[CONF_SERVER_IDENTIFIER] server_id = config_entry.data[CONF_SERVER_IDENTIFIER]
registry = await async_get_registry(hass)
def async_new_media_players(new_entities): def async_new_media_players(new_entities):
_async_add_entities( _async_add_entities(
hass, config_entry, async_add_entities, server_id, new_entities hass, registry, config_entry, async_add_entities, server_id, new_entities
) )
unsub = async_dispatcher_connect( unsub = async_dispatcher_connect(
@ -70,7 +72,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
@callback @callback
def _async_add_entities( def _async_add_entities(
hass, config_entry, async_add_entities, server_id, new_entities hass, registry, config_entry, async_add_entities, server_id, new_entities
): ):
"""Set up Plex media_player entities.""" """Set up Plex media_player entities."""
entities = [] entities = []
@ -79,6 +81,19 @@ def _async_add_entities(
plex_mp = PlexMediaPlayer(plexserver, **entity_params) plex_mp = PlexMediaPlayer(plexserver, **entity_params)
entities.append(plex_mp) entities.append(plex_mp)
# Migration to per-server unique_ids
old_entity_id = registry.async_get_entity_id(
MP_DOMAIN, PLEX_DOMAIN, plex_mp.machine_identifier
)
if old_entity_id is not None:
new_unique_id = f"{server_id}:{plex_mp.machine_identifier}"
_LOGGER.debug(
"Migrating unique_id from [%s] to [%s]",
plex_mp.machine_identifier,
new_unique_id,
)
registry.async_update_entity(old_entity_id, new_unique_id=new_unique_id)
async_add_entities(entities, True) async_add_entities(entities, True)
@ -126,6 +141,7 @@ class PlexMediaPlayer(MediaPlayerDevice):
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Run when about to be added to hass.""" """Run when about to be added to hass."""
server_id = self.plex_server.machine_identifier server_id = self.plex_server.machine_identifier
unsub = async_dispatcher_connect( unsub = async_dispatcher_connect(
self.hass, self.hass,
PLEX_UPDATE_MEDIA_PLAYER_SIGNAL.format(self.unique_id), PLEX_UPDATE_MEDIA_PLAYER_SIGNAL.format(self.unique_id),
@ -315,6 +331,11 @@ class PlexMediaPlayer(MediaPlayerDevice):
@property @property
def unique_id(self): def unique_id(self):
"""Return the id of this plex client.""" """Return the id of this plex client."""
return f"{self.plex_server.machine_identifier}:{self._machine_identifier}"
@property
def machine_identifier(self):
"""Return the Plex-provided identifier of this plex client."""
return self._machine_identifier return self._machine_identifier
@property @property

View File

@ -94,9 +94,10 @@ class PlexServer:
def refresh_entity(self, machine_identifier, device, session): def refresh_entity(self, machine_identifier, device, session):
"""Forward refresh dispatch to media_player.""" """Forward refresh dispatch to media_player."""
unique_id = f"{self.machine_identifier}:{machine_identifier}"
dispatcher_send( dispatcher_send(
self._hass, self._hass,
PLEX_UPDATE_MEDIA_PLAYER_SIGNAL.format(machine_identifier), PLEX_UPDATE_MEDIA_PLAYER_SIGNAL.format(unique_id),
device, device,
session, session,
) )