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