Use attrs instead of properties in sonarr (#51737)
* Use attrs instead of properties in sonarr * Create entity.py * Update sensor.py * Update __init__.py * Update entity.py * Update entity.py * Update sensor.pypull/51747/head
parent
343e0e0933
commit
e0013648f6
|
@ -8,7 +8,6 @@ from sonarr import Sonarr, SonarrAccessRestricted, SonarrError
|
|||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_NAME,
|
||||
CONF_API_KEY,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
|
@ -18,12 +17,8 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
||||
from .const import (
|
||||
ATTR_IDENTIFIERS,
|
||||
ATTR_MANUFACTURER,
|
||||
ATTR_SOFTWARE_VERSION,
|
||||
CONF_BASE_PATH,
|
||||
CONF_UPCOMING_DAYS,
|
||||
CONF_WANTED_MAX_ITEMS,
|
||||
|
@ -99,54 +94,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
||||
|
||||
class SonarrEntity(Entity):
|
||||
"""Defines a base Sonarr entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
sonarr: Sonarr,
|
||||
entry_id: str,
|
||||
device_id: str,
|
||||
name: str,
|
||||
icon: str,
|
||||
enabled_default: bool = True,
|
||||
) -> None:
|
||||
"""Initialize the Sonarr entity."""
|
||||
self._entry_id = entry_id
|
||||
self._device_id = device_id
|
||||
self._enabled_default = enabled_default
|
||||
self._icon = icon
|
||||
self._name = name
|
||||
self.sonarr = sonarr
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the mdi icon of the entity."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self) -> bool:
|
||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
||||
return self._enabled_default
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo | None:
|
||||
"""Return device information about the application."""
|
||||
if self._device_id is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)},
|
||||
ATTR_NAME: "Activity Sensor",
|
||||
ATTR_MANUFACTURER: "Sonarr",
|
||||
ATTR_SOFTWARE_VERSION: self.sonarr.app.info.version,
|
||||
"entry_type": "service",
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
"""Base Entity for Sonarr."""
|
||||
from __future__ import annotations
|
||||
|
||||
from sonarr import Sonarr
|
||||
|
||||
from homeassistant.const import ATTR_NAME
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
||||
from .const import ATTR_IDENTIFIERS, ATTR_MANUFACTURER, ATTR_SOFTWARE_VERSION, DOMAIN
|
||||
|
||||
|
||||
class SonarrEntity(Entity):
|
||||
"""Defines a base Sonarr entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
sonarr: Sonarr,
|
||||
entry_id: str,
|
||||
device_id: str,
|
||||
) -> None:
|
||||
"""Initialize the Sonarr entity."""
|
||||
self._entry_id = entry_id
|
||||
self._device_id = device_id
|
||||
self.sonarr = sonarr
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo | None:
|
||||
"""Return device information about the application."""
|
||||
if self._device_id is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)},
|
||||
ATTR_NAME: "Activity Sensor",
|
||||
ATTR_MANUFACTURER: "Sonarr",
|
||||
ATTR_SOFTWARE_VERSION: self.sonarr.app.info.version,
|
||||
"entry_type": "service",
|
||||
}
|
|
@ -14,8 +14,8 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import SonarrEntity
|
||||
from .const import CONF_UPCOMING_DAYS, CONF_WANTED_MAX_ITEMS, DATA_SONARR, DOMAIN
|
||||
from .entity import SonarrEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -81,35 +81,25 @@ class SonarrSensor(SonarrEntity, SensorEntity):
|
|||
unit_of_measurement: str | None = None,
|
||||
) -> None:
|
||||
"""Initialize Sonarr sensor."""
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._key = key
|
||||
self._unique_id = f"{entry_id}_{key}"
|
||||
self._attr_name = name
|
||||
self._attr_icon = icon
|
||||
self._attr_unique_id = f"{entry_id}_{key}"
|
||||
self._attr_unit_of_measurement = unit_of_measurement
|
||||
self._attr_entity_registry_enabled_default = enabled_default
|
||||
self.last_update_success = False
|
||||
|
||||
super().__init__(
|
||||
sonarr=sonarr,
|
||||
entry_id=entry_id,
|
||||
device_id=entry_id,
|
||||
name=name,
|
||||
icon=icon,
|
||||
enabled_default=enabled_default,
|
||||
)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return the unique ID for this sensor."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return sensor availability."""
|
||||
return self.last_update_success
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit this state is expressed in."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
|
||||
class SonarrCommandsSensor(SonarrSensor):
|
||||
"""Defines a Sonarr Commands sensor."""
|
||||
|
@ -186,7 +176,7 @@ class SonarrDiskspaceSensor(SonarrSensor):
|
|||
|
||||
attrs[
|
||||
disk.path
|
||||
] = f"{free:.2f}/{total:.2f}{self._unit_of_measurement} ({usage:.2f}%)"
|
||||
] = f"{free:.2f}/{total:.2f}{self.unit_of_measurement} ({usage:.2f}%)"
|
||||
|
||||
return attrs
|
||||
|
||||
|
|
Loading…
Reference in New Issue