2018-02-19 22:46:22 +00:00
|
|
|
"""Class to hold all sensor accessories."""
|
|
|
|
import logging
|
|
|
|
|
2018-02-26 03:27:40 +00:00
|
|
|
from homeassistant.const import (
|
2018-02-26 21:29:52 +00:00
|
|
|
ATTR_UNIT_OF_MEASUREMENT, TEMP_FAHRENHEIT, TEMP_CELSIUS)
|
2018-02-19 22:46:22 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
|
|
|
|
|
|
|
from . import TYPES
|
2018-02-26 03:27:40 +00:00
|
|
|
from .accessories import (
|
|
|
|
HomeAccessory, add_preload_service, override_properties)
|
2018-02-19 22:46:22 +00:00
|
|
|
from .const import (
|
2018-02-26 03:27:40 +00:00
|
|
|
SERV_TEMPERATURE_SENSOR, CHAR_CURRENT_TEMPERATURE, PROP_CELSIUS)
|
2018-02-19 22:46:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-02-26 03:27:40 +00:00
|
|
|
def calc_temperature(state, unit=TEMP_CELSIUS):
|
|
|
|
"""Calculate temperature from state and unit.
|
|
|
|
|
|
|
|
Always return temperature as Celsius value.
|
|
|
|
Conversion is handled on the device.
|
|
|
|
"""
|
2018-02-26 21:29:52 +00:00
|
|
|
try:
|
|
|
|
value = float(state)
|
|
|
|
except ValueError:
|
2018-02-26 03:27:40 +00:00
|
|
|
return None
|
|
|
|
|
2018-02-26 21:29:52 +00:00
|
|
|
return round((value - 32) / 1.8, 2) if unit == TEMP_FAHRENHEIT else value
|
2018-02-26 03:27:40 +00:00
|
|
|
|
|
|
|
|
2018-02-19 22:46:22 +00:00
|
|
|
@TYPES.register('TemperatureSensor')
|
|
|
|
class TemperatureSensor(HomeAccessory):
|
|
|
|
"""Generate a TemperatureSensor accessory for a temperature sensor.
|
|
|
|
|
2018-02-26 21:29:52 +00:00
|
|
|
Sensor entity must return temperature in °C, °F.
|
2018-02-19 22:46:22 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hass, entity_id, display_name):
|
|
|
|
"""Initialize a TemperatureSensor accessory object."""
|
2018-02-26 03:27:40 +00:00
|
|
|
super().__init__(display_name, entity_id, 'SENSOR')
|
2018-02-19 22:46:22 +00:00
|
|
|
|
|
|
|
self._hass = hass
|
|
|
|
self._entity_id = entity_id
|
|
|
|
|
2018-02-26 03:27:40 +00:00
|
|
|
self.serv_temp = add_preload_service(self, SERV_TEMPERATURE_SENSOR)
|
|
|
|
self.char_temp = self.serv_temp. \
|
2018-02-19 22:46:22 +00:00
|
|
|
get_characteristic(CHAR_CURRENT_TEMPERATURE)
|
2018-02-26 03:27:40 +00:00
|
|
|
override_properties(self.char_temp, PROP_CELSIUS)
|
2018-03-01 23:20:02 +00:00
|
|
|
self.char_temp.value = 0
|
2018-02-26 03:27:40 +00:00
|
|
|
self.unit = None
|
2018-02-19 22:46:22 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Method called be object after driver is started."""
|
|
|
|
state = self._hass.states.get(self._entity_id)
|
|
|
|
self.update_temperature(new_state=state)
|
|
|
|
|
|
|
|
async_track_state_change(
|
|
|
|
self._hass, self._entity_id, self.update_temperature)
|
|
|
|
|
|
|
|
def update_temperature(self, entity_id=None, old_state=None,
|
|
|
|
new_state=None):
|
|
|
|
"""Update temperature after state changed."""
|
2018-02-20 00:40:56 +00:00
|
|
|
if new_state is None:
|
|
|
|
return
|
|
|
|
|
2018-02-26 03:27:40 +00:00
|
|
|
unit = new_state.attributes[ATTR_UNIT_OF_MEASUREMENT]
|
|
|
|
temperature = calc_temperature(new_state.state, unit)
|
|
|
|
if temperature is not None:
|
|
|
|
self.char_temp.set_value(temperature)
|
|
|
|
_LOGGER.debug("%s: Current temperature set to %d°C",
|
|
|
|
self._entity_id, temperature)
|