mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +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.machine_identifier = device.machineIdentifier
|
||||||
self.session_device = None
|
self.session_device = None
|
||||||
|
|
||||||
self._available = False
|
|
||||||
self._device_protocol_capabilities = None
|
self._device_protocol_capabilities = None
|
||||||
self._name = None
|
|
||||||
self._previous_volume_level = 1 # Used in fake muting
|
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_level = 1 # since we can't retrieve remotely
|
||||||
self._volume_muted = False # 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
|
# Initializes other attributes
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
@ -180,10 +184,10 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
|||||||
if not self.session:
|
if not self.session:
|
||||||
self.force_idle()
|
self.force_idle()
|
||||||
if not self.device:
|
if not self.device:
|
||||||
self._available = False
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
self._available = True
|
self._attr_available = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
device_url = self.device.url("/")
|
device_url = self.device.url("/")
|
||||||
@ -207,25 +211,15 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
|||||||
if self.username and self.username != self.plex_server.owner:
|
if self.username and self.username != self.plex_server.owner:
|
||||||
# Prepend username for shared/managed clients
|
# Prepend username for shared/managed clients
|
||||||
name_parts.insert(0, self.username)
|
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):
|
def force_idle(self):
|
||||||
"""Force client to idle."""
|
"""Force client to idle."""
|
||||||
self._state = STATE_IDLE
|
self._attr_state = STATE_IDLE
|
||||||
if self.player_source == "session":
|
if self.player_source == "session":
|
||||||
self.device = None
|
self.device = None
|
||||||
self.session_device = None
|
self.session_device = None
|
||||||
self._available = False
|
self._attr_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}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self):
|
def session(self):
|
||||||
@ -239,17 +233,7 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
|||||||
self.session_device = self.session.player
|
self.session_device = self.session.player
|
||||||
self.update_state(self.session.state)
|
self.update_state(self.session.state)
|
||||||
else:
|
else:
|
||||||
self._state = STATE_IDLE
|
self._attr_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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@needs_session
|
@needs_session
|
||||||
@ -257,22 +241,17 @@ class PlexMediaPlayer(MediaPlayerEntity):
|
|||||||
"""Return the username of the client owner."""
|
"""Return the username of the client owner."""
|
||||||
return self.session.username
|
return self.session.username
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""Return the state of the device."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
def update_state(self, state):
|
def update_state(self, state):
|
||||||
"""Set the state of the device, handle session termination."""
|
"""Set the state of the device, handle session termination."""
|
||||||
if state == "playing":
|
if state == "playing":
|
||||||
self._state = STATE_PLAYING
|
self._attr_state = STATE_PLAYING
|
||||||
elif state == "paused":
|
elif state == "paused":
|
||||||
self._state = STATE_PAUSED
|
self._attr_state = STATE_PAUSED
|
||||||
elif state == "stopped":
|
elif state == "stopped":
|
||||||
self.session = None
|
self.session = None
|
||||||
self.force_idle()
|
self.force_idle()
|
||||||
else:
|
else:
|
||||||
self._state = STATE_IDLE
|
self._attr_state = STATE_IDLE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _is_player_active(self):
|
def _is_player_active(self):
|
||||||
|
@ -57,10 +57,13 @@ class PlexSensor(SensorEntity):
|
|||||||
|
|
||||||
def __init__(self, hass, plex_server):
|
def __init__(self, hass, plex_server):
|
||||||
"""Initialize the sensor."""
|
"""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._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(
|
self.async_refresh_sensor = Debouncer(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
@ -83,39 +86,9 @@ class PlexSensor(SensorEntity):
|
|||||||
async def _async_refresh_sensor(self):
|
async def _async_refresh_sensor(self):
|
||||||
"""Set instance object and trigger an entity state update."""
|
"""Set instance object and trigger an entity state update."""
|
||||||
_LOGGER.debug("Refreshing sensor [%s]", self.unique_id)
|
_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()
|
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
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
@ -146,11 +119,15 @@ class PlexLibrarySectionSensor(SensorEntity):
|
|||||||
self.server_id = plex_server.machine_identifier
|
self.server_id = plex_server.machine_identifier
|
||||||
self.library_section = plex_library_section
|
self.library_section = plex_library_section
|
||||||
self.library_type = plex_library_section.type
|
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._attr_available = True
|
||||||
self._state = None
|
self._attr_entity_registry_enabled_default = False
|
||||||
self._available = True
|
self._attr_extra_state_attributes = {}
|
||||||
self._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):
|
async def async_added_to_hass(self):
|
||||||
"""Run when about to be added to hass."""
|
"""Run when about to be added to hass."""
|
||||||
@ -168,9 +145,9 @@ class PlexLibrarySectionSensor(SensorEntity):
|
|||||||
_LOGGER.debug("Refreshing library sensor for '%s'", self.name)
|
_LOGGER.debug("Refreshing library sensor for '%s'", self.name)
|
||||||
try:
|
try:
|
||||||
await self.hass.async_add_executor_job(self._update_state_and_attrs)
|
await self.hass.async_add_executor_job(self._update_state_and_attrs)
|
||||||
self._available = True
|
self._attr_available = True
|
||||||
except NotFound:
|
except NotFound:
|
||||||
self._available = False
|
self._attr_available = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def _update_state_and_attrs(self):
|
def _update_state_and_attrs(self):
|
||||||
@ -179,59 +156,16 @@ class PlexLibrarySectionSensor(SensorEntity):
|
|||||||
self.library_type, self.library_type
|
self.library_type, self.library_type
|
||||||
)
|
)
|
||||||
|
|
||||||
self._state = self.library_section.totalViewSize(
|
self._attr_state = self.library_section.totalViewSize(
|
||||||
libtype=primary_libtype, includeCollections=False
|
libtype=primary_libtype, includeCollections=False
|
||||||
)
|
)
|
||||||
for libtype in LIBRARY_ATTRIBUTE_TYPES.get(self.library_type, []):
|
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
|
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
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return a device description for device registry."""
|
"""Return a device description for device registry."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user