Skip to content

Commit

Permalink
Use entity class attributes for Plex (home-assistant#52617)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlawren authored Jul 12, 2021
1 parent 4d16cda commit 2970931
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 125 deletions.
53 changes: 16 additions & 37 deletions homeassistant/components/plex/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ def __init__(self, plex_server, device, player_source, session=None):
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

Expand Down Expand Up @@ -180,10 +184,10 @@ def update(self):
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("/")
Expand All @@ -207,25 +211,15 @@ def update(self):
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):
Expand All @@ -239,40 +233,25 @@ def session(self, session):
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
def username(self):
"""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):
Expand Down
110 changes: 22 additions & 88 deletions homeassistant/components/plex/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -83,39 +86,9 @@ async def async_added_to_hass(self):
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."""
Expand Down Expand Up @@ -146,11 +119,15 @@ def __init__(self, hass, plex_server, plex_library_section):
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."""
Expand All @@ -168,9 +145,9 @@ async def async_refresh_sensor(self):
_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):
Expand All @@ -179,59 +156,16 @@ def _update_state_and_attrs(self):
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."""
Expand Down

0 comments on commit 2970931

Please sign in to comment.