2019-02-13 20:21:14 +00:00
|
|
|
"""Support for monitoring an SABnzbd NZB client."""
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2018-05-07 07:35:55 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DATA_SABNZBD, SENSOR_TYPES, SIGNAL_SABNZBD_UPDATED
|
|
|
|
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-05-07 07:35:55 +00:00
|
|
|
"""Set up the SABnzbd sensors."""
|
|
|
|
if discovery_info is None:
|
2017-08-04 21:24:55 +00:00
|
|
|
return
|
|
|
|
|
2018-05-07 07:35:55 +00:00
|
|
|
sab_api_data = hass.data[DATA_SABNZBD]
|
|
|
|
sensors = sab_api_data.sensors
|
|
|
|
client_name = sab_api_data.name
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(
|
|
|
|
[SabnzbdSensor(sensor, sab_api_data, client_name) for sensor in sensors]
|
|
|
|
)
|
2017-08-04 21:24:55 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SabnzbdSensor(SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of an SABnzbd sensor."""
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2018-05-07 07:35:55 +00:00
|
|
|
def __init__(self, sensor_type, sabnzbd_api_data, client_name):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2018-05-07 07:35:55 +00:00
|
|
|
self._client_name = client_name
|
|
|
|
self._field_name = SENSOR_TYPES[sensor_type][2]
|
2015-03-08 18:28:12 +00:00
|
|
|
self._name = SENSOR_TYPES[sensor_type][0]
|
2018-05-07 07:35:55 +00:00
|
|
|
self._sabnzbd_api = sabnzbd_api_data
|
2015-03-08 18:28:12 +00:00
|
|
|
self._state = None
|
2018-05-07 07:35:55 +00:00
|
|
|
self._type = sensor_type
|
2015-03-22 03:43:20 +00:00
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2018-05-07 07:35:55 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Call when entity about to be added to hass."""
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_SABNZBD_UPDATED, self.update_state
|
|
|
|
)
|
|
|
|
)
|
2018-05-07 07:35:55 +00:00
|
|
|
|
2015-03-08 18:28:12 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._client_name} {self._name}"
|
2015-03-08 18:28:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-03-08 18:28:12 +00:00
|
|
|
return self._state
|
|
|
|
|
2019-10-07 15:17:39 +00:00
|
|
|
@property
|
2018-05-07 07:35:55 +00:00
|
|
|
def should_poll(self):
|
|
|
|
"""Don't poll. Will be updated by dispatcher signal."""
|
|
|
|
return False
|
|
|
|
|
2015-03-08 18:28:12 +00:00
|
|
|
@property
|
2015-03-22 04:18:58 +00:00
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-03-22 04:18:58 +00:00
|
|
|
return self._unit_of_measurement
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2018-05-07 07:35:55 +00:00
|
|
|
def update_state(self, args):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data and updates the states."""
|
2018-05-07 07:35:55 +00:00
|
|
|
self._state = self._sabnzbd_api.get_queue_field(self._field_name)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._type == "speed":
|
2018-05-07 07:35:55 +00:00
|
|
|
self._state = round(float(self._state) / 1024, 1)
|
2019-07-31 19:25:30 +00:00
|
|
|
elif "size" in self._type:
|
2018-05-07 07:35:55 +00:00
|
|
|
self._state = round(float(self._state), 2)
|
|
|
|
|
|
|
|
self.schedule_update_ha_state()
|