Use DOMAIN constant in plex (#78764)

pull/78770/head
epenet 2022-09-19 15:08:40 +02:00 committed by GitHub
parent 903edfd881
commit 75e52ef389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 32 deletions

View File

@ -34,7 +34,7 @@ from .const import (
CONF_SERVER,
CONF_SERVER_IDENTIFIER,
DISPATCHERS,
DOMAIN as PLEX_DOMAIN,
DOMAIN,
GDM_DEBOUNCER,
GDM_SCANNER,
PLATFORMS,
@ -62,7 +62,7 @@ def is_plex_media_id(media_content_id):
async def async_browse_media(hass, media_content_type, media_content_id, platform=None):
"""Browse Plex media."""
plex_server = next(iter(hass.data[PLEX_DOMAIN][SERVERS].values()), None)
plex_server = next(iter(hass.data[DOMAIN][SERVERS].values()), None)
if not plex_server:
raise BrowseError("No Plex servers available")
is_internal = is_internal_request(hass)
@ -81,7 +81,7 @@ async def async_browse_media(hass, media_content_type, media_content_id, platfor
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Plex component."""
hass.data.setdefault(
PLEX_DOMAIN,
DOMAIN,
{SERVERS: {}, DISPATCHERS: {}, WEBSOCKETS: {}, PLATFORMS_COMPLETED: {}},
)
@ -89,13 +89,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
hass.http.register_view(PlexImageView())
gdm = hass.data[PLEX_DOMAIN][GDM_SCANNER] = GDM()
gdm = hass.data[DOMAIN][GDM_SCANNER] = GDM()
def gdm_scan():
_LOGGER.debug("Scanning for GDM clients")
gdm.scan(scan_for_clients=True)
hass.data[PLEX_DOMAIN][GDM_DEBOUNCER] = Debouncer[None](
hass.data[DOMAIN][GDM_DEBOUNCER] = Debouncer[None](
hass,
_LOGGER,
cooldown=10,
@ -160,8 +160,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"Connected to: %s (%s)", plex_server.friendly_name, plex_server.url_in_use
)
server_id = plex_server.machine_identifier
hass.data[PLEX_DOMAIN][SERVERS][server_id] = plex_server
hass.data[PLEX_DOMAIN][PLATFORMS_COMPLETED][server_id] = set()
hass.data[DOMAIN][SERVERS][server_id] = plex_server
hass.data[DOMAIN][PLATFORMS_COMPLETED][server_id] = set()
entry.add_update_listener(async_options_updated)
@ -170,8 +170,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id),
plex_server.async_update_platforms,
)
hass.data[PLEX_DOMAIN][DISPATCHERS].setdefault(server_id, [])
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
hass.data[DOMAIN][DISPATCHERS].setdefault(server_id, [])
hass.data[DOMAIN][DISPATCHERS][server_id].append(unsub)
@callback
def plex_websocket_callback(msgtype, data, error):
@ -213,11 +213,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
session=session,
verify_ssl=verify_ssl,
)
hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket
hass.data[DOMAIN][WEBSOCKETS][server_id] = websocket
def start_websocket_session(platform):
hass.data[PLEX_DOMAIN][PLATFORMS_COMPLETED][server_id].add(platform)
if hass.data[PLEX_DOMAIN][PLATFORMS_COMPLETED][server_id] == PLATFORMS:
hass.data[DOMAIN][PLATFORMS_COMPLETED][server_id].add(platform)
if hass.data[DOMAIN][PLATFORMS_COMPLETED][server_id] == PLATFORMS:
hass.loop.create_task(websocket.listen())
def close_websocket_session(_):
@ -226,7 +226,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
unsub = hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, close_websocket_session
)
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
hass.data[DOMAIN][DISPATCHERS][server_id].append(unsub)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -263,16 +263,16 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
server_id = entry.data[CONF_SERVER_IDENTIFIER]
websocket = hass.data[PLEX_DOMAIN][WEBSOCKETS].pop(server_id)
websocket = hass.data[DOMAIN][WEBSOCKETS].pop(server_id)
websocket.close()
dispatchers = hass.data[PLEX_DOMAIN][DISPATCHERS].pop(server_id)
dispatchers = hass.data[DOMAIN][DISPATCHERS].pop(server_id)
for unsub in dispatchers:
unsub()
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[PLEX_DOMAIN][SERVERS].pop(server_id)
hass.data[DOMAIN][SERVERS].pop(server_id)
return unload_ok
@ -282,8 +282,8 @@ async def async_options_updated(hass: HomeAssistant, entry: ConfigEntry) -> None
server_id = entry.data[CONF_SERVER_IDENTIFIER]
# Guard incomplete setup during reauth flows
if server_id in hass.data[PLEX_DOMAIN][SERVERS]:
hass.data[PLEX_DOMAIN][SERVERS][server_id].options = entry.options
if server_id in hass.data[DOMAIN][SERVERS]:
hass.data[DOMAIN][SERVERS][server_id].options = entry.options
@callback

View File

@ -35,7 +35,7 @@ from .const import (
COMMON_PLAYERS,
CONF_SERVER_IDENTIFIER,
DISPATCHERS,
DOMAIN as PLEX_DOMAIN,
DOMAIN,
NAME_FORMAT,
PLEX_NEW_MP_SIGNAL,
PLEX_UPDATE_MEDIA_PLAYER_SESSION_SIGNAL,
@ -86,7 +86,7 @@ async def async_setup_entry(
unsub = async_dispatcher_connect(
hass, PLEX_NEW_MP_SIGNAL.format(server_id), async_new_media_players
)
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
hass.data[DOMAIN][DISPATCHERS][server_id].append(unsub)
_LOGGER.debug("New entity listener created")
@ -95,14 +95,14 @@ def _async_add_entities(hass, registry, async_add_entities, server_id, new_entit
"""Set up Plex media_player entities."""
_LOGGER.debug("New entities: %s", new_entities)
entities = []
plexserver = hass.data[PLEX_DOMAIN][SERVERS][server_id]
plexserver = hass.data[DOMAIN][SERVERS][server_id]
for entity_params in new_entities:
plex_mp = PlexMediaPlayer(plexserver, **entity_params)
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
MP_DOMAIN, DOMAIN, plex_mp.machine_identifier
)
if old_entity_id is not None:
new_unique_id = f"{server_id}:{plex_mp.machine_identifier}"
@ -523,7 +523,7 @@ class PlexMediaPlayer(MediaPlayerEntity):
if self.device_product in TRANSIENT_DEVICE_MODELS:
return DeviceInfo(
identifiers={(PLEX_DOMAIN, "plex.tv-clients")},
identifiers={(DOMAIN, "plex.tv-clients")},
name="Plex Client Service",
manufacturer="Plex",
model="Plex Clients",
@ -531,12 +531,12 @@ class PlexMediaPlayer(MediaPlayerEntity):
)
return DeviceInfo(
identifiers={(PLEX_DOMAIN, self.machine_identifier)},
identifiers={(DOMAIN, self.machine_identifier)},
manufacturer=self.device_platform or "Plex",
model=self.device_product or self.device_make,
name=self.name,
sw_version=self.device_version,
via_device=(PLEX_DOMAIN, self.plex_server.machine_identifier),
via_device=(DOMAIN, self.plex_server.machine_identifier),
)
async def async_browse_media(

View File

@ -16,7 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
CONF_SERVER_IDENTIFIER,
DOMAIN as PLEX_DOMAIN,
DOMAIN,
NAME_FORMAT,
PLEX_UPDATE_LIBRARY_SIGNAL,
PLEX_UPDATE_SENSOR_SIGNAL,
@ -57,7 +57,7 @@ async def async_setup_entry(
) -> None:
"""Set up Plex sensor from a config entry."""
server_id = config_entry.data[CONF_SERVER_IDENTIFIER]
plexserver = hass.data[PLEX_DOMAIN][SERVERS][server_id]
plexserver = hass.data[DOMAIN][SERVERS][server_id]
sensors = [PlexSensor(hass, plexserver)]
def create_library_sensors():
@ -118,7 +118,7 @@ class PlexSensor(SensorEntity):
return None
return DeviceInfo(
identifiers={(PLEX_DOMAIN, self._server.machine_identifier)},
identifiers={(DOMAIN, self._server.machine_identifier)},
manufacturer="Plex",
model="Plex Media Server",
name=self._server.friendly_name,
@ -209,7 +209,7 @@ class PlexLibrarySectionSensor(SensorEntity):
return None
return DeviceInfo(
identifiers={(PLEX_DOMAIN, self.server_id)},
identifiers={(DOMAIN, self.server_id)},
manufacturer="Plex",
model="Plex Media Server",
name=self.server_name,

View File

@ -11,7 +11,7 @@ from aiohttp.typedefs import LooseHeaders
from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView
from homeassistant.components.media_player import async_fetch_image
from .const import DOMAIN as PLEX_DOMAIN, SERVERS
from .const import DOMAIN, SERVERS
_LOGGER = logging.getLogger(__name__)
@ -33,7 +33,7 @@ class PlexImageView(HomeAssistantView):
return web.Response(status=HTTPStatus.UNAUTHORIZED)
hass = request.app["hass"]
if (server := hass.data[PLEX_DOMAIN][SERVERS].get(server_id)) is None:
if (server := hass.data[DOMAIN][SERVERS].get(server_id)) is None:
return web.Response(status=HTTPStatus.NOT_FOUND)
if (image_url := server.thumbnail_cache.get(media_content_id)) is None:

View File

@ -11,7 +11,7 @@ from homeassistant.components.media_player.const import (
MEDIA_TYPE_MUSIC,
SERVICE_PLAY_MEDIA,
)
from homeassistant.components.plex.const import DOMAIN as PLEX_DOMAIN, PLEX_URI_SCHEME
from homeassistant.components.plex import DOMAIN as PLEX_DOMAIN, PLEX_URI_SCHEME
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.exceptions import HomeAssistantError