2019-02-13 21:21:14 +01:00
|
|
|
"""Support for IKEA Tradfri sensors."""
|
2019-09-22 23:01:32 +02:00
|
|
|
|
2021-03-22 19:47:44 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-09-05 21:09:14 +02:00
|
|
|
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
|
2019-12-01 06:23:39 +01:00
|
|
|
|
2019-10-09 21:56:16 +02:00
|
|
|
from .base_class import TradfriBaseDevice
|
2020-09-05 23:02:32 +02:00
|
|
|
from .const import CONF_GATEWAY_ID, DEVICES, DOMAIN, KEY_API
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-10-05 17:05:38 +01:00
|
|
|
|
2018-09-19 21:21:43 +02:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up a Tradfri config entry."""
|
2019-10-06 19:24:56 +02:00
|
|
|
gateway_id = config_entry.data[CONF_GATEWAY_ID]
|
2020-09-03 18:39:24 +02:00
|
|
|
tradfri_data = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
api = tradfri_data[KEY_API]
|
2020-09-05 23:02:32 +02:00
|
|
|
devices = tradfri_data[DEVICES]
|
2018-09-19 21:21:43 +02:00
|
|
|
|
2020-09-05 23:02:32 +02:00
|
|
|
sensors = (
|
2019-07-31 12:25:30 -07:00
|
|
|
dev
|
2020-09-05 23:02:32 +02:00
|
|
|
for dev in devices
|
2019-10-06 19:24:56 +02:00
|
|
|
if not dev.has_light_control
|
|
|
|
and not dev.has_socket_control
|
|
|
|
and not dev.has_blind_control
|
2019-10-24 23:25:47 +02:00
|
|
|
and not dev.has_signal_repeater_control
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2020-09-05 23:02:32 +02:00
|
|
|
if sensors:
|
|
|
|
async_add_entities(TradfriSensor(sensor, api, gateway_id) for sensor in sensors)
|
2017-10-05 17:05:38 +01:00
|
|
|
|
|
|
|
|
2021-03-22 19:47:44 +01:00
|
|
|
class TradfriSensor(TradfriBaseDevice, SensorEntity):
|
2017-10-05 17:05:38 +01:00
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
2019-10-06 19:24:56 +02:00
|
|
|
def __init__(self, device, api, gateway_id):
|
2017-10-05 17:05:38 +01:00
|
|
|
"""Initialize the device."""
|
2019-10-06 19:24:56 +02:00
|
|
|
super().__init__(device, api, gateway_id)
|
|
|
|
self._unique_id = f"{gateway_id}-{device.id}"
|
2017-10-05 17:05:38 +01:00
|
|
|
|
|
|
|
@property
|
2019-10-06 19:24:56 +02:00
|
|
|
def device_class(self):
|
2017-10-05 17:05:38 +01:00
|
|
|
"""Return the devices' state attributes."""
|
2019-10-06 19:24:56 +02:00
|
|
|
return DEVICE_CLASS_BATTERY
|
2017-10-05 17:05:38 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the current state of the device."""
|
|
|
|
return self._device.device_info.battery_level
|
|
|
|
|
2019-10-06 19:24:56 +02:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit_of_measurement of the device."""
|
2020-09-05 21:09:14 +02:00
|
|
|
return PERCENTAGE
|