hass-core/homeassistant/components/sensor/temper.py

65 lines
2.1 KiB
Python
Raw Normal View History

"""
2015-08-06 19:15:37 +02:00
Support for getting temperature from TEMPer devices.
2015-10-20 22:14:51 +02:00
For more details about this platform, please refer to the documentation at
2015-11-09 13:12:18 +01:00
https://home-assistant.io/components/sensor.temper/
2015-08-06 19:15:37 +02:00
"""
import logging
2016-02-18 21:27:50 -08:00
from homeassistant.const import CONF_NAME, DEVICE_DEFAULT_NAME, TEMP_FAHRENHEIT
2016-02-18 21:27:50 -08:00
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['https://github.com/rkabadi/temper-python/archive/'
'3dbdaf2d87b8db9a3cd6e5585fc704537dd2d09b.zip'
'#temperusb==1.2.3']
2015-08-02 18:58:30 -07:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-08 16:46:34 +01:00
"""Setup the Temper sensors."""
2016-01-28 21:37:08 -08:00
from temperusb.temper import TemperHandler
temp_unit = hass.config.temperature_unit
name = config.get(CONF_NAME, DEVICE_DEFAULT_NAME)
temper_devices = TemperHandler().get_devices()
add_devices_callback([TemperSensor(dev, temp_unit, name + '_' + str(idx))
for idx, dev in enumerate(temper_devices)])
class TemperSensor(Entity):
2016-03-08 16:46:34 +01:00
"""Representation of a Temper temperature sensor."""
2016-02-23 06:21:49 +01:00
def __init__(self, temper_device, temp_unit, name):
2016-03-08 16:46:34 +01:00
"""Initialize the sensor."""
self.temper_device = temper_device
self.temp_unit = temp_unit
self.current_value = None
self._name = name
@property
def name(self):
2016-03-08 16:46:34 +01:00
"""Return the name of the temperature sensor."""
return self._name
@property
def state(self):
2016-03-08 16:46:34 +01:00
"""Return the state of the entity."""
return self.current_value
@property
def unit_of_measurement(self):
2016-03-08 16:46:34 +01:00
"""Return the unit of measurement of this entity, if any."""
return self.temp_unit
def update(self):
2016-02-23 06:21:49 +01:00
"""Retrieve latest state."""
try:
format_str = ('fahrenheit' if self.temp_unit == TEMP_FAHRENHEIT
else 'celsius')
self.current_value = self.temper_device.get_temperature(format_str)
2015-08-02 18:51:13 -07:00
except IOError:
_LOGGER.error('Failed to get temperature due to insufficient '
'permissions. Try running with "sudo"')