mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Use entity class attributes for Plex (#52617)
This commit is contained in:
parent
4d16cda957
commit
2970931d8d
@ -119,14 +119,18 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
||||
self.machine_identifier = device.machineIdentifier
|
||||
self.session_device = None
|
||||
|
||||
self._available = False
|
||||
self._device_protocol_capabilities = None
|
||||
self._name = None
|
||||
self._previous_volume_level = 1 # Used in fake muting
|
||||
self._state = STATE_IDLE
|
||||
self._volume_level = 1 # since we can't retrieve remotely
|
||||
self._volume_muted = False # since we can't retrieve remotely
|
||||
|
||||
self._attr_available = False
|
||||
self._attr_should_poll = False
|
||||
self._attr_state = STATE_IDLE
|
||||
self._attr_unique_id = (
|
||||
f"{self.plex_server.machine_identifier}:{self.machine_identifier}"
|
||||
)
|
||||
|
||||
# Initializes other attributes
|
||||
self.session = session
|
||||
|
||||
@ -180,10 +184,10 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
||||
if not self.session:
|
||||
self.force_idle()
|
||||
if not self.device:
|
||||
self._available = False
|
||||
self._attr_available = False
|
||||
return
|
||||
|
||||
self._available = True
|
||||
self._attr_available = True
|
||||
|
||||
try:
|
||||
device_url = self.device.url("/")
|
||||
@ -207,25 +211,15 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
||||
if self.username and self.username != self.plex_server.owner:
|
||||
# Prepend username for shared/managed clients
|
||||
name_parts.insert(0, self.username)
|
||||
self._name = NAME_FORMAT.format(" - ".join(name_parts))
|
||||
self._attr_name = NAME_FORMAT.format(" - ".join(name_parts))
|
||||
|
||||
def force_idle(self):
|
||||
"""Force client to idle."""
|
||||
self._state = STATE_IDLE
|
||||
self._attr_state = STATE_IDLE
|
||||
if self.player_source == "session":
|
||||
self.device = None
|
||||
self.session_device = None
|
||||
self._available = False
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the id of this plex client."""
|
||||
return f"{self.plex_server.machine_identifier}:{self.machine_identifier}"
|
||||
self._attr_available = False
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
@ -239,17 +233,7 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
||||
self.session_device = self.session.player
|
||||
self.update_state(self.session.state)
|
||||
else:
|
||||
self._state = STATE_IDLE
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return the availability of the client."""
|
||||
return self._available
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return self._name
|
||||
self._attr_state = STATE_IDLE
|
||||
|
||||
@property
|
||||
@needs_session
|
||||
@ -257,22 +241,17 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
||||
"""Return the username of the client owner."""
|
||||
return self.session.username
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
def update_state(self, state):
|
||||
"""Set the state of the device, handle session termination."""
|
||||
if state == "playing":
|
||||
self._state = STATE_PLAYING
|
||||
self._attr_state = STATE_PLAYING
|
||||
elif state == "paused":
|
||||
self._state = STATE_PAUSED
|
||||
self._attr_state = STATE_PAUSED
|
||||
elif state == "stopped":
|
||||
self.session = None
|
||||
self.force_idle()
|
||||
else:
|
||||
self._state = STATE_IDLE
|
||||
self._attr_state = STATE_IDLE
|
||||
|
||||
@property
|
||||
def _is_player_active(self):
|
||||
|
@ -57,10 +57,13 @@ class PlexSensor(SensorEntity):
|
||||
|
||||
def __init__(self, hass, plex_server):
|
||||
"""Initialize the sensor."""
|
||||
self._state = None
|
||||
self._attr_icon = "mdi:plex"
|
||||
self._attr_name = NAME_FORMAT.format(plex_server.friendly_name)
|
||||
self._attr_should_poll = False
|
||||
self._attr_unique_id = f"sensor-{plex_server.machine_identifier}"
|
||||
self._attr_unit_of_measurement = "Watching"
|
||||
|
||||
self._server = plex_server
|
||||
self._name = NAME_FORMAT.format(plex_server.friendly_name)
|
||||
self._unique_id = f"sensor-{plex_server.machine_identifier}"
|
||||
self.async_refresh_sensor = Debouncer(
|
||||
hass,
|
||||
_LOGGER,
|
||||
@ -83,39 +86,9 @@ class PlexSensor(SensorEntity):
|
||||
async def _async_refresh_sensor(self):
|
||||
"""Set instance object and trigger an entity state update."""
|
||||
_LOGGER.debug("Refreshing sensor [%s]", self.unique_id)
|
||||
self._state = len(self._server.sensor_attributes)
|
||||
self._attr_state = len(self._server.sensor_attributes)
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the id of this plex client."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit this state is expressed in."""
|
||||
return "Watching"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of the sensor."""
|
||||
return "mdi:plex"
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
@ -146,11 +119,15 @@ class PlexLibrarySectionSensor(SensorEntity):
|
||||
self.server_id = plex_server.machine_identifier
|
||||
self.library_section = plex_library_section
|
||||
self.library_type = plex_library_section.type
|
||||
self._name = f"{self.server_name} Library - {plex_library_section.title}"
|
||||
self._unique_id = f"library-{self.server_id}-{plex_library_section.uuid}"
|
||||
self._state = None
|
||||
self._available = True
|
||||
self._attributes = {}
|
||||
|
||||
self._attr_available = True
|
||||
self._attr_entity_registry_enabled_default = False
|
||||
self._attr_extra_state_attributes = {}
|
||||
self._attr_icon = LIBRARY_ICON_LOOKUP.get(self.library_type, "mdi:plex")
|
||||
self._attr_name = f"{self.server_name} Library - {plex_library_section.title}"
|
||||
self._attr_should_poll = False
|
||||
self._attr_unique_id = f"library-{self.server_id}-{plex_library_section.uuid}"
|
||||
self._attr_unit_of_measurement = "Items"
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when about to be added to hass."""
|
||||
@ -168,9 +145,9 @@ class PlexLibrarySectionSensor(SensorEntity):
|
||||
_LOGGER.debug("Refreshing library sensor for '%s'", self.name)
|
||||
try:
|
||||
await self.hass.async_add_executor_job(self._update_state_and_attrs)
|
||||
self._available = True
|
||||
self._attr_available = True
|
||||
except NotFound:
|
||||
self._available = False
|
||||
self._attr_available = False
|
||||
self.async_write_ha_state()
|
||||
|
||||
def _update_state_and_attrs(self):
|
||||
@ -179,59 +156,16 @@ class PlexLibrarySectionSensor(SensorEntity):
|
||||
self.library_type, self.library_type
|
||||
)
|
||||
|
||||
self._state = self.library_section.totalViewSize(
|
||||
self._attr_state = self.library_section.totalViewSize(
|
||||
libtype=primary_libtype, includeCollections=False
|
||||
)
|
||||
for libtype in LIBRARY_ATTRIBUTE_TYPES.get(self.library_type, []):
|
||||
self._attributes[f"{libtype}s"] = self.library_section.totalViewSize(
|
||||
self._attr_extra_state_attributes[
|
||||
f"{libtype}s"
|
||||
] = self.library_section.totalViewSize(
|
||||
libtype=libtype, includeCollections=False
|
||||
)
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return the availability of the client."""
|
||||
return self._available
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self):
|
||||
"""Return if sensor should be enabled by default."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the id of this plex client."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit this state is expressed in."""
|
||||
return "Items"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of the sensor."""
|
||||
return LIBRARY_ICON_LOOKUP.get(self.library_type, "mdi:plex")
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._attributes
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return a device description for device registry."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user