Use EntityDescription - ombi (#55086)

This commit is contained in:
Marc Mueller 2021-08-23 23:00:14 +02:00 committed by GitHub
parent dae40530bd
commit 354dbe91b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 49 deletions

View File

@ -1,4 +1,8 @@
"""Support for Ombi.""" """Support for Ombi."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntityDescription
ATTR_SEASON = "season" ATTR_SEASON = "season"
CONF_URLBASE = "urlbase" CONF_URLBASE = "urlbase"
@ -13,11 +17,35 @@ SERVICE_MOVIE_REQUEST = "submit_movie_request"
SERVICE_MUSIC_REQUEST = "submit_music_request" SERVICE_MUSIC_REQUEST = "submit_music_request"
SERVICE_TV_REQUEST = "submit_tv_request" SERVICE_TV_REQUEST = "submit_tv_request"
SENSOR_TYPES = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
"movies": {"type": "Movie requests", "icon": "mdi:movie"}, SensorEntityDescription(
"tv": {"type": "TV show requests", "icon": "mdi:television-classic"}, key="movies",
"music": {"type": "Music album requests", "icon": "mdi:album"}, name="Movie requests",
"pending": {"type": "Pending requests", "icon": "mdi:clock-alert-outline"}, icon="mdi:movie",
"approved": {"type": "Approved requests", "icon": "mdi:check"}, ),
"available": {"type": "Available requests", "icon": "mdi:download"}, SensorEntityDescription(
} key="tv",
name="TV show requests",
icon="mdi:television-classic",
),
SensorEntityDescription(
key="music",
name="Music album requests",
icon="mdi:album",
),
SensorEntityDescription(
key="pending",
name="Pending requests",
icon="mdi:clock-alert-outline",
),
SensorEntityDescription(
key="approved",
name="Approved requests",
icon="mdi:check",
),
SensorEntityDescription(
key="available",
name="Available requests",
icon="mdi:download",
),
)

View File

@ -4,7 +4,7 @@ import logging
from pyombi import OmbiError from pyombi import OmbiError
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from .const import DOMAIN, SENSOR_TYPES from .const import DOMAIN, SENSOR_TYPES
@ -18,60 +18,39 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if discovery_info is None: if discovery_info is None:
return return
sensors = []
ombi = hass.data[DOMAIN]["instance"] ombi = hass.data[DOMAIN]["instance"]
for sensor, sensor_val in SENSOR_TYPES.items(): entities = [OmbiSensor(ombi, description) for description in SENSOR_TYPES]
sensor_label = sensor
sensor_type = sensor_val["type"]
sensor_icon = sensor_val["icon"]
sensors.append(OmbiSensor(sensor_label, sensor_type, ombi, sensor_icon))
add_entities(sensors, True) add_entities(entities, True)
class OmbiSensor(SensorEntity): class OmbiSensor(SensorEntity):
"""Representation of an Ombi sensor.""" """Representation of an Ombi sensor."""
def __init__(self, label, sensor_type, ombi, icon): def __init__(self, ombi, description: SensorEntityDescription):
"""Initialize the sensor.""" """Initialize the sensor."""
self._state = None self.entity_description = description
self._label = label
self._type = sensor_type
self._ombi = ombi self._ombi = ombi
self._icon = icon
@property self._attr_name = f"Ombi {description.name}"
def name(self):
"""Return the name of the sensor."""
return f"Ombi {self._type}"
@property
def icon(self):
"""Return the icon to use in the frontend."""
return self._icon
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
def update(self): def update(self):
"""Update the sensor.""" """Update the sensor."""
try: try:
if self._label == "movies": sensor_type = self.entity_description.key
self._state = self._ombi.movie_requests if sensor_type == "movies":
elif self._label == "tv": self._attr_native_value = self._ombi.movie_requests
self._state = self._ombi.tv_requests elif sensor_type == "tv":
elif self._label == "music": self._attr_native_value = self._ombi.tv_requests
self._state = self._ombi.music_requests elif sensor_type == "music":
elif self._label == "pending": self._attr_native_value = self._ombi.music_requests
self._state = self._ombi.total_requests["pending"] elif sensor_type == "pending":
elif self._label == "approved": self._attr_native_value = self._ombi.total_requests["pending"]
self._state = self._ombi.total_requests["approved"] elif sensor_type == "approved":
elif self._label == "available": self._attr_native_value = self._ombi.total_requests["approved"]
self._state = self._ombi.total_requests["available"] elif sensor_type == "available":
self._attr_native_value = self._ombi.total_requests["available"]
except OmbiError as err: except OmbiError as err:
_LOGGER.warning("Unable to update Ombi sensor: %s", err) _LOGGER.warning("Unable to update Ombi sensor: %s", err)
self._state = None self._attr_native_value = None