2019-04-03 15:40:03 +00:00
|
|
|
"""Support for getting temperature from TEMPer devices."""
|
2015-08-01 19:20:29 +00:00
|
|
|
import logging
|
2019-11-27 00:02:50 +00:00
|
|
|
|
|
|
|
from temperusb.temper import TemperHandler
|
2016-09-04 16:37:10 +00:00
|
|
|
import voluptuous as vol
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-09-04 16:37:10 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2016-04-20 03:00:03 +00:00
|
|
|
from homeassistant.const import CONF_NAME, DEVICE_DEFAULT_NAME, TEMP_FAHRENHEIT
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-08-01 19:20:29 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
)
|
2015-08-01 19:20:29 +00:00
|
|
|
|
2016-11-28 06:01:13 +00:00
|
|
|
TEMPER_SENSORS = []
|
|
|
|
|
|
|
|
|
|
|
|
def get_temper_devices():
|
|
|
|
"""Scan the Temper devices from temperusb."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-11-28 06:01:13 +00:00
|
|
|
return TemperHandler().get_devices()
|
|
|
|
|
2015-08-03 01:58:30 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-04 05:46:43 +00:00
|
|
|
"""Set up the Temper sensors."""
|
2016-07-31 20:24:49 +00:00
|
|
|
temp_unit = hass.config.units.temperature_unit
|
2016-09-04 16:37:10 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2019-07-31 19:25:30 +00:00
|
|
|
scaling = {"scale": config.get(CONF_SCALE), "offset": config.get(CONF_OFFSET)}
|
2016-11-28 06:01:13 +00:00
|
|
|
temper_devices = get_temper_devices()
|
2016-09-04 16:37:10 +00:00
|
|
|
|
|
|
|
for idx, dev in enumerate(temper_devices):
|
|
|
|
if idx != 0:
|
2020-01-03 13:47:06 +00:00
|
|
|
name = f"{name}_{idx!s}"
|
2016-11-28 06:01:13 +00:00
|
|
|
TEMPER_SENSORS.append(TemperSensor(dev, temp_unit, name, scaling))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(TEMPER_SENSORS)
|
2016-09-04 16:37:10 +00:00
|
|
|
|
2016-11-28 06:01:13 +00:00
|
|
|
|
|
|
|
def reset_devices():
|
|
|
|
"""
|
|
|
|
Re-scan for underlying Temper sensors and assign them to our devices.
|
|
|
|
|
|
|
|
This assumes the same sensor devices are present in the same order.
|
|
|
|
"""
|
|
|
|
temper_devices = get_temper_devices()
|
|
|
|
for sensor, device in zip(TEMPER_SENSORS, temper_devices):
|
|
|
|
sensor.set_temper_device(device)
|
2015-08-01 19:20:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TemperSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Temper temperature sensor."""
|
2016-02-23 05:21:49 +00:00
|
|
|
|
2016-09-04 16:37:10 +00:00
|
|
|
def __init__(self, temper_device, temp_unit, name, scaling):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-08-01 19:20:29 +00:00
|
|
|
self.temp_unit = temp_unit
|
2019-07-31 19:25:30 +00:00
|
|
|
self.scale = scaling["scale"]
|
|
|
|
self.offset = scaling["offset"]
|
2015-08-01 19:20:29 +00:00
|
|
|
self.current_value = None
|
2015-08-01 19:46:28 +00:00
|
|
|
self._name = name
|
2016-11-28 06:01:13 +00:00
|
|
|
self.set_temper_device(temper_device)
|
2016-09-04 16:37:10 +00:00
|
|
|
|
2015-08-01 19:46:28 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the temperature sensor."""
|
2015-08-01 19:46:28 +00:00
|
|
|
return self._name
|
2015-08-01 19:20:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the entity."""
|
2015-08-01 19:20:29 +00:00
|
|
|
return self.current_value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-08-01 19:20:29 +00:00
|
|
|
return self.temp_unit
|
|
|
|
|
2016-11-28 06:01:13 +00:00
|
|
|
def set_temper_device(self, temper_device):
|
|
|
|
"""Assign the underlying device for this sensor."""
|
|
|
|
self.temper_device = temper_device
|
|
|
|
|
|
|
|
# set calibration data
|
2019-07-31 19:25:30 +00:00
|
|
|
self.temper_device.set_calibration_data(scale=self.scale, offset=self.offset)
|
2016-11-28 06:01:13 +00:00
|
|
|
|
2015-08-01 19:20:29 +00:00
|
|
|
def update(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Retrieve latest state."""
|
2015-08-01 19:20:29 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
format_str = (
|
|
|
|
"fahrenheit" if self.temp_unit == TEMP_FAHRENHEIT else "celsius"
|
|
|
|
)
|
2016-09-04 16:37:10 +00:00
|
|
|
sensor_value = self.temper_device.get_temperature(format_str)
|
|
|
|
self.current_value = round(sensor_value, 1)
|
2019-09-04 17:09:24 +00:00
|
|
|
except OSError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Failed to get temperature. The device address may"
|
|
|
|
"have changed. Attempting to reset device"
|
|
|
|
)
|
2016-11-28 06:01:13 +00:00
|
|
|
reset_devices()
|