mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
TEMPer component: reset devices on address change (#4596)
Fixes https://github.com/home-assistant/home-assistant/issues/4389 The USB address of these devices periodically changes, causing home-assistant to fail to read the temperature data. This PR fixes this by re-reading the available devices on failure. I've been running this for several days and for the first time have consistent temperature data without having to restart home-assistant.
This commit is contained in:
parent
1e6c660f59
commit
d8c4af9c81
@ -24,27 +24,42 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.Optional(CONF_OFFSET, default=0): vol.Coerce(float)
|
vol.Optional(CONF_OFFSET, default=0): vol.Coerce(float)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
TEMPER_SENSORS = []
|
||||||
|
|
||||||
|
|
||||||
|
def get_temper_devices():
|
||||||
|
"""Scan the Temper devices from temperusb."""
|
||||||
|
from temperusb.temper import TemperHandler
|
||||||
|
return TemperHandler().get_devices()
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the Temper sensors."""
|
"""Setup the Temper sensors."""
|
||||||
from temperusb.temper import TemperHandler
|
|
||||||
|
|
||||||
temp_unit = hass.config.units.temperature_unit
|
temp_unit = hass.config.units.temperature_unit
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
scaling = {
|
scaling = {
|
||||||
'scale': config.get(CONF_SCALE),
|
'scale': config.get(CONF_SCALE),
|
||||||
'offset': config.get(CONF_OFFSET)
|
'offset': config.get(CONF_OFFSET)
|
||||||
}
|
}
|
||||||
temper_devices = TemperHandler().get_devices()
|
temper_devices = get_temper_devices()
|
||||||
devices = []
|
|
||||||
|
|
||||||
for idx, dev in enumerate(temper_devices):
|
for idx, dev in enumerate(temper_devices):
|
||||||
if idx != 0:
|
if idx != 0:
|
||||||
name = name + '_' + str(idx)
|
name = name + '_' + str(idx)
|
||||||
devices.append(TemperSensor(dev, temp_unit, name, scaling))
|
TEMPER_SENSORS.append(TemperSensor(dev, temp_unit, name, scaling))
|
||||||
|
add_devices(TEMPER_SENSORS)
|
||||||
|
|
||||||
add_devices(devices)
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
class TemperSensor(Entity):
|
class TemperSensor(Entity):
|
||||||
@ -52,18 +67,12 @@ class TemperSensor(Entity):
|
|||||||
|
|
||||||
def __init__(self, temper_device, temp_unit, name, scaling):
|
def __init__(self, temper_device, temp_unit, name, scaling):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.temper_device = temper_device
|
|
||||||
self.temp_unit = temp_unit
|
self.temp_unit = temp_unit
|
||||||
self.scale = scaling['scale']
|
self.scale = scaling['scale']
|
||||||
self.offset = scaling['offset']
|
self.offset = scaling['offset']
|
||||||
self.current_value = None
|
self.current_value = None
|
||||||
self._name = name
|
self._name = name
|
||||||
|
self.set_temper_device(temper_device)
|
||||||
# set calibration data
|
|
||||||
self.temper_device.set_calibration_data(
|
|
||||||
scale=self.scale,
|
|
||||||
offset=self.offset
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -80,6 +89,16 @@ class TemperSensor(Entity):
|
|||||||
"""Return the unit of measurement of this entity, if any."""
|
"""Return the unit of measurement of this entity, if any."""
|
||||||
return self.temp_unit
|
return self.temp_unit
|
||||||
|
|
||||||
|
def set_temper_device(self, temper_device):
|
||||||
|
"""Assign the underlying device for this sensor."""
|
||||||
|
self.temper_device = temper_device
|
||||||
|
|
||||||
|
# set calibration data
|
||||||
|
self.temper_device.set_calibration_data(
|
||||||
|
scale=self.scale,
|
||||||
|
offset=self.offset
|
||||||
|
)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
try:
|
try:
|
||||||
@ -88,5 +107,6 @@ class TemperSensor(Entity):
|
|||||||
sensor_value = self.temper_device.get_temperature(format_str)
|
sensor_value = self.temper_device.get_temperature(format_str)
|
||||||
self.current_value = round(sensor_value, 1)
|
self.current_value = round(sensor_value, 1)
|
||||||
except IOError:
|
except IOError:
|
||||||
_LOGGER.error('Failed to get temperature due to insufficient '
|
_LOGGER.error('Failed to get temperature. The device address may'
|
||||||
'permissions. Try running with "sudo"')
|
'have changed - attempting to reset device')
|
||||||
|
reset_devices()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user