2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Minut Point sensors."""
|
2018-11-19 11:52:21 +00:00
|
|
|
import logging
|
|
|
|
|
2018-12-13 15:40:56 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN
|
2018-11-19 11:52:21 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_PRESSURE,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
TEMP_CELSIUS,
|
2020-02-28 19:46:48 +00:00
|
|
|
UNIT_PERCENTAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-12-03 15:50:05 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-11-19 11:52:21 +00:00
|
|
|
from homeassistant.util.dt import parse_datetime
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import MinutPointEntity
|
|
|
|
from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW
|
|
|
|
|
2018-11-19 11:52:21 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_SOUND = "sound_level"
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
|
|
|
DEVICE_CLASS_TEMPERATURE: (None, 1, TEMP_CELSIUS),
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_PRESSURE: (None, 0, "hPa"),
|
2020-02-28 19:46:48 +00:00
|
|
|
DEVICE_CLASS_HUMIDITY: (None, 1, UNIT_PERCENTAGE),
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_SOUND: ("mdi:ear-hearing", 1, "dBa"),
|
2018-11-19 11:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up a Point's sensors based on a config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-12-03 15:50:05 +00:00
|
|
|
async def async_discover_sensor(device_id):
|
|
|
|
"""Discover and add a discovered sensor."""
|
|
|
|
client = hass.data[POINT_DOMAIN][config_entry.entry_id]
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(
|
|
|
|
(
|
|
|
|
MinutPointSensor(client, device_id, sensor_type)
|
|
|
|
for sensor_type in SENSOR_TYPES
|
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
2018-12-03 15:50:05 +00:00
|
|
|
|
|
|
|
async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, POINT_DISCOVERY_NEW.format(DOMAIN, POINT_DOMAIN), async_discover_sensor
|
|
|
|
)
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MinutPointSensor(MinutPointEntity):
|
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
|
|
|
def __init__(self, point_client, device_id, device_class):
|
2019-02-14 04:35:12 +00:00
|
|
|
"""Initialize the sensor."""
|
2018-11-19 11:52:21 +00:00
|
|
|
super().__init__(point_client, device_id, device_class)
|
|
|
|
self._device_prop = SENSOR_TYPES[device_class]
|
|
|
|
|
2019-02-12 16:15:02 +00:00
|
|
|
async def _update_callback(self):
|
2018-11-19 11:52:21 +00:00
|
|
|
"""Update the value of the sensor."""
|
|
|
|
if self.is_updated:
|
2019-02-14 04:35:12 +00:00
|
|
|
_LOGGER.debug("Update sensor value for %s", self)
|
2019-02-12 16:15:02 +00:00
|
|
|
self._value = await self.hass.async_add_executor_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.device.sensor, self.device_class
|
|
|
|
)
|
2018-11-19 11:52:21 +00:00
|
|
|
self._updated = parse_datetime(self.device.last_update)
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2018-11-19 11:52:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon representation."""
|
|
|
|
return self._device_prop[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2019-02-10 20:48:33 +00:00
|
|
|
if self.value is None:
|
|
|
|
return None
|
2018-11-19 11:52:21 +00:00
|
|
|
return round(self.value, self._device_prop[1])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._device_prop[2]
|