Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added scale and offset to the Temper component #2853

Merged
merged 11 commits into from
Sep 4, 2016
46 changes: 37 additions & 9 deletions homeassistant/components/sensor/temper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,66 @@
https://home-assistant.io/components/sensor.temper/
"""
import logging
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, DEVICE_DEFAULT_NAME, TEMP_FAHRENHEIT
from homeassistant.helpers.entity import Entity

_LOGGER = logging.getLogger(__name__)

REQUIREMENTS = ['https://github.com/rkabadi/temper-python/archive/'
'3dbdaf2d87b8db9a3cd6e5585fc704537dd2d09b.zip'
'#temperusb==1.2.3']
REQUIREMENTS = ['temperusb==1.5.1']

CONF_SCALE = 'scale'
CONF_OFFSET = 'offset'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEVICE_DEFAULT_NAME): vol.Coerce(str),
vol.Optional(CONF_SCALE, default=1): vol.Coerce(float),
vol.Optional(CONF_OFFSET, default=0): vol.Coerce(float)
})


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Temper sensors."""
from temperusb.temper import TemperHandler

temp_unit = hass.config.units.temperature_unit
name = config.get(CONF_NAME, DEVICE_DEFAULT_NAME)
name = config.get(CONF_NAME)
scaling = {
'scale': config.get(CONF_SCALE),
'offset': config.get(CONF_OFFSET)
}
temper_devices = TemperHandler().get_devices()
add_devices_callback([TemperSensor(dev, temp_unit, name + '_' + str(idx))
for idx, dev in enumerate(temper_devices)])
devices = []

for idx, dev in enumerate(temper_devices):
if idx != 0:
name = name + '_' + str(idx)
devices.append(TemperSensor(dev, temp_unit, name, scaling))

add_devices(devices)


class TemperSensor(Entity):
"""Representation of a Temper temperature sensor."""

def __init__(self, temper_device, temp_unit, name):
def __init__(self, temper_device, temp_unit, name, scaling):
"""Initialize the sensor."""
self.temper_device = temper_device
self.temp_unit = temp_unit
self.scale = scaling['scale']
self.offset = scaling['offset']
self.current_value = None
self._name = name

# set calibration data
self.temper_device.set_calibration_data(
scale=self.scale,
offset=self.offset
)

@property
def name(self):
"""Return the name of the temperature sensor."""
Expand All @@ -58,7 +85,8 @@ def update(self):
try:
format_str = ('fahrenheit' if self.temp_unit == TEMP_FAHRENHEIT
else 'celsius')
self.current_value = self.temper_device.get_temperature(format_str)
sensor_value = self.temper_device.get_temperature(format_str)
self.current_value = round(sensor_value, 1)
except IOError:
_LOGGER.error('Failed to get temperature due to insufficient '
'permissions. Try running with "sudo"')
6 changes: 3 additions & 3 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ https://github.com/nkgilley/python-join-api/archive/3e1e849f1af0b4080f551b62270c
# homeassistant.components.switch.edimax
https://github.com/rkabadi/pyedimax/archive/365301ce3ff26129a7910c501ead09ea625f3700.zip#pyedimax==0.1

# homeassistant.components.sensor.temper
https://github.com/rkabadi/temper-python/archive/3dbdaf2d87b8db9a3cd6e5585fc704537dd2d09b.zip#temperusb==1.2.3

# homeassistant.components.sensor.gtfs
https://github.com/robbiet480/pygtfs/archive/00546724e4bbcb3053110d844ca44e2246267dd8.zip#pygtfs==0.1.3

Expand Down Expand Up @@ -437,6 +434,9 @@ tellcore-py==1.1.2
# homeassistant.components.tellduslive
tellive-py==0.5.2

# homeassistant.components.sensor.temper
temperusb==1.5.1

# homeassistant.components.sensor.transmission
# homeassistant.components.switch.transmission
transmissionrpc==0.11
Expand Down