2019-02-13 20:21:14 +00:00
|
|
|
"""Support for IKEA Tradfri sensors."""
|
2017-10-05 16:05:38 +00:00
|
|
|
from datetime import timedelta
|
2019-03-21 05:56:46 +00:00
|
|
|
import logging
|
2017-10-05 16:05:38 +00:00
|
|
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import KEY_API, KEY_GATEWAY
|
|
|
|
|
2017-10-05 16:05:38 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=5)
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
api = hass.data[KEY_API][config_entry.entry_id]
|
|
|
|
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]
|
|
|
|
|
|
|
|
devices_commands = await api(gateway.get_devices())
|
2018-03-28 22:50:09 +00:00
|
|
|
all_devices = await api(devices_commands)
|
2018-09-30 20:22:07 +00:00
|
|
|
devices = (dev for dev in all_devices if not dev.has_light_control and
|
|
|
|
not dev.has_socket_control)
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(TradfriDevice(device, api) for device in devices)
|
2017-10-05 16:05:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TradfriDevice(Entity):
|
|
|
|
"""The platform class required by Home Assistant."""
|
|
|
|
|
|
|
|
def __init__(self, device, api):
|
|
|
|
"""Initialize the device."""
|
|
|
|
self._api = api
|
|
|
|
self._device = None
|
|
|
|
self._name = None
|
|
|
|
|
|
|
|
self._refresh(device)
|
|
|
|
|
2018-03-28 22:50:09 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-10-05 16:05:38 +00:00
|
|
|
"""Start thread when added to hass."""
|
|
|
|
self._async_start_observe()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed for tradfri."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit_of_measurement of the device."""
|
|
|
|
return '%'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the devices' state attributes."""
|
|
|
|
info = self._device.device_info
|
|
|
|
attrs = {
|
|
|
|
'manufacturer': info.manufacturer,
|
|
|
|
'model_number': info.model_number,
|
|
|
|
'serial': info.serial,
|
|
|
|
'firmware_version': info.firmware_version,
|
|
|
|
'power_source': info.power_source_str,
|
|
|
|
'battery_level': info.battery_level
|
|
|
|
}
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the current state of the device."""
|
|
|
|
return self._device.device_info.battery_level
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_start_observe(self, exc=None):
|
|
|
|
"""Start observation of light."""
|
2017-11-22 09:37:20 +00:00
|
|
|
# pylint: disable=import-error
|
2018-03-28 22:50:09 +00:00
|
|
|
from pytradfri.error import PytradfriError
|
2017-10-05 16:05:38 +00:00
|
|
|
if exc:
|
|
|
|
_LOGGER.warning("Observation failed for %s", self._name,
|
|
|
|
exc_info=exc)
|
|
|
|
|
|
|
|
try:
|
|
|
|
cmd = self._device.observe(callback=self._observe_update,
|
|
|
|
err_callback=self._async_start_observe,
|
|
|
|
duration=0)
|
2018-10-02 09:03:09 +00:00
|
|
|
self.hass.async_create_task(self._api(cmd))
|
2018-03-28 22:50:09 +00:00
|
|
|
except PytradfriError as err:
|
2017-10-05 16:05:38 +00:00
|
|
|
_LOGGER.warning("Observation failed, trying again", exc_info=err)
|
|
|
|
self._async_start_observe()
|
|
|
|
|
|
|
|
def _refresh(self, device):
|
|
|
|
"""Refresh the device data."""
|
|
|
|
self._device = device
|
|
|
|
self._name = device.name
|
|
|
|
|
|
|
|
def _observe_update(self, tradfri_device):
|
|
|
|
"""Receive new state data for this device."""
|
|
|
|
self._refresh(tradfri_device)
|
|
|
|
|
2018-10-02 09:03:09 +00:00
|
|
|
self.hass.async_create_task(self.async_update_ha_state())
|