core/homeassistant/components/sensor/sabnzbd.py

79 lines
2.5 KiB
Python
Raw Normal View History

2015-03-08 18:28:12 +00:00
"""
2016-02-23 05:21:49 +00:00
Support for monitoring an SABnzbd NZB client.
2015-03-08 18:28:12 +00:00
2015-10-14 06:35:47 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/sensor.sabnzbd/
2015-03-08 18:28:12 +00:00
"""
2015-11-29 21:49:05 +00:00
import logging
from homeassistant.components.sabnzbd import DATA_SABNZBD, \
SIGNAL_SABNZBD_UPDATED, SENSOR_TYPES
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
2015-03-08 18:28:12 +00:00
DEPENDENCIES = ['sabnzbd']
2015-09-09 03:11:25 +00:00
_LOGGER = logging.getLogger(__name__)
2015-03-08 18:28:12 +00:00
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the SABnzbd sensors."""
if discovery_info is None:
return
sab_api_data = hass.data[DATA_SABNZBD]
sensors = sab_api_data.sensors
client_name = sab_api_data.name
async_add_entities([SabnzbdSensor(sensor, sab_api_data, client_name)
for sensor in sensors])
class SabnzbdSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of an SABnzbd sensor."""
2015-03-08 18:28:12 +00:00
def __init__(self, sensor_type, sabnzbd_api_data, client_name):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
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]
self._sabnzbd_api = sabnzbd_api_data
2015-03-08 18:28:12 +00:00
self._state = None
self._type = sensor_type
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
2015-03-08 18:28:12 +00:00
async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
async_dispatcher_connect(self.hass, SIGNAL_SABNZBD_UPDATED,
self.update_state)
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."""
return '{} {}'.format(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
def should_poll(self):
"""Don't poll. Will be updated by dispatcher signal."""
return False
2015-03-08 18:28:12 +00:00
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
2015-03-08 18:28:12 +00:00
def update_state(self, args):
2016-03-08 15:46:34 +00:00
"""Get the latest data and updates the states."""
self._state = self._sabnzbd_api.get_queue_field(self._field_name)
if self._type == 'speed':
self._state = round(float(self._state) / 1024, 1)
elif 'size' in self._type:
self._state = round(float(self._state), 2)
self.schedule_update_ha_state()