2020-08-20 15:30:41 +00:00
|
|
|
"""Support for Broadlink sensors."""
|
2019-12-09 10:48:52 +00:00
|
|
|
import logging
|
2017-04-24 03:41:09 +00:00
|
|
|
|
2016-12-16 05:42:00 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_ILLUMINANCE,
|
2021-07-26 15:42:19 +00:00
|
|
|
DEVICE_CLASS_POWER,
|
2020-08-20 15:30:41 +00:00
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
PLATFORM_SCHEMA,
|
2021-05-21 09:44:34 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-03-22 11:37:16 +00:00
|
|
|
SensorEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-07-26 15:42:19 +00:00
|
|
|
from homeassistant.const import CONF_HOST, PERCENTAGE, POWER_WATT, TEMP_CELSIUS
|
2020-08-20 15:30:41 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2020-04-18 23:16:49 +00:00
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
from .const import DOMAIN
|
2021-06-25 05:39:21 +00:00
|
|
|
from .entity import BroadlinkEntity
|
2020-08-20 15:30:41 +00:00
|
|
|
from .helpers import import_device
|
2016-12-16 05:42:00 +00:00
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-12-16 05:42:00 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2021-05-21 09:44:34 +00:00
|
|
|
"temperature": (
|
|
|
|
"Temperature",
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
|
|
|
"air_quality": ("Air Quality", None, None, None),
|
2021-05-24 09:36:42 +00:00
|
|
|
"humidity": (
|
|
|
|
"Humidity",
|
|
|
|
PERCENTAGE,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
2021-05-21 09:44:34 +00:00
|
|
|
"light": ("Light", None, DEVICE_CLASS_ILLUMINANCE, None),
|
|
|
|
"noise": ("Noise", None, None, None),
|
2021-07-26 15:42:19 +00:00
|
|
|
"power": (
|
|
|
|
"Current power",
|
|
|
|
POWER_WATT,
|
|
|
|
DEVICE_CLASS_POWER,
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
),
|
2016-12-16 05:42:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
2020-08-20 15:30:41 +00:00
|
|
|
{vol.Required(CONF_HOST): cv.string}, extra=vol.ALLOW_EXTRA
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-12-16 05:42:00 +00:00
|
|
|
|
|
|
|
|
2020-05-13 08:36:32 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2020-08-20 15:30:41 +00:00
|
|
|
"""Import the device and discontinue platform.
|
|
|
|
|
|
|
|
This is for backward compatibility.
|
|
|
|
Do not use this method.
|
|
|
|
"""
|
|
|
|
import_device(hass, config[CONF_HOST])
|
|
|
|
_LOGGER.warning(
|
|
|
|
"The sensor platform is deprecated, please remove it from your configuration"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Broadlink sensor."""
|
|
|
|
device = hass.data[DOMAIN].devices[config_entry.entry_id]
|
|
|
|
sensor_data = device.update_manager.coordinator.data
|
2020-05-13 08:36:32 +00:00
|
|
|
sensors = [
|
2020-08-20 15:30:41 +00:00
|
|
|
BroadlinkSensor(device, monitored_condition)
|
|
|
|
for monitored_condition in sensor_data
|
2021-05-07 12:47:51 +00:00
|
|
|
if sensor_data[monitored_condition] != 0 or device.api.type == "A1"
|
2020-05-13 08:36:32 +00:00
|
|
|
]
|
2020-08-20 15:30:41 +00:00
|
|
|
async_add_entities(sensors)
|
2016-12-16 05:42:00 +00:00
|
|
|
|
|
|
|
|
2021-06-25 05:39:21 +00:00
|
|
|
class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
2020-08-20 15:30:41 +00:00
|
|
|
"""Representation of a Broadlink sensor."""
|
2016-12-16 05:42:00 +00:00
|
|
|
|
2020-08-20 15:30:41 +00:00
|
|
|
def __init__(self, device, monitored_condition):
|
2016-12-16 05:42:00 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-06-25 05:39:21 +00:00
|
|
|
super().__init__(device)
|
2020-08-20 15:30:41 +00:00
|
|
|
self._monitored_condition = monitored_condition
|
2021-06-25 21:31:17 +00:00
|
|
|
|
2021-07-21 13:27:52 +00:00
|
|
|
self._attr_device_class = SENSOR_TYPES[monitored_condition][2]
|
|
|
|
self._attr_name = f"{device.name} {SENSOR_TYPES[monitored_condition][0]}"
|
|
|
|
self._attr_state_class = SENSOR_TYPES[monitored_condition][3]
|
2021-06-25 21:31:17 +00:00
|
|
|
self._attr_state = self._coordinator.data[monitored_condition]
|
2021-07-21 13:27:52 +00:00
|
|
|
self._attr_unique_id = f"{device.unique_id}-{monitored_condition}"
|
|
|
|
self._attr_unit_of_measurement = SENSOR_TYPES[monitored_condition][1]
|
2021-05-21 09:44:34 +00:00
|
|
|
|
2021-07-27 18:53:29 +00:00
|
|
|
def _update_state(self, data):
|
|
|
|
"""Update the state of the entity."""
|
|
|
|
self._attr_state = data[self._monitored_condition]
|