Skip to content

Commit

Permalink
Refactor IKEA Tradfri, part 2 (home-assistant#27245)
Browse files Browse the repository at this point in the history
* Add more device info data

* Add attributes to device_info

* Refactor sensor

* Filter
devices

* Update following review

* Update following review

* Add device_Class
  • Loading branch information
ggravlingen authored and MartinHjelmare committed Oct 6, 2019
1 parent 7a15605 commit 1059cea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 74 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/tradfri/base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def device_info(self):

return {
"identifiers": {(TRADFRI_DOMAIN, self._device.id)},
"name": self._name,
"manufacturer": info.manufacturer,
"model": info.model_number,
"name": self._name,
"sw_version": info.firmware_version,
"via_device": (TRADFRI_DOMAIN, self._gateway_id),
}
Expand Down
92 changes: 19 additions & 73 deletions homeassistant/components/tradfri/sensor.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
"""Support for IKEA Tradfri sensors."""
import logging
from datetime import timedelta

from pytradfri.error import PytradfriError

from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from homeassistant.components.tradfri.base_class import TradfriBaseDevice
from homeassistant.const import DEVICE_CLASS_BATTERY
from . import KEY_API, KEY_GATEWAY
from .const import CONF_GATEWAY_ID

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(minutes=5)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up a Tradfri config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
api = hass.data[KEY_API][config_entry.entry_id]
gateway = hass.data[KEY_GATEWAY][config_entry.entry_id]

Expand All @@ -23,84 +20,33 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
devices = (
dev
for dev in all_devices
if not dev.has_light_control and not dev.has_socket_control
if not dev.has_light_control
and not dev.has_socket_control
and not dev.has_blind_control
)
async_add_entities(TradfriDevice(device, api) for device in devices)
if devices:
async_add_entities(TradfriSensor(device, api, gateway_id) for device in devices)


class TradfriDevice(Entity):
class TradfriSensor(TradfriBaseDevice):
"""The platform class required by Home Assistant."""

def __init__(self, device, api):
def __init__(self, device, api, gateway_id):
"""Initialize the device."""
self._api = api
self._device = None
self._name = None

self._refresh(device)

async def async_added_to_hass(self):
"""Start thread when added to hass."""
self._async_start_observe()

@property
def should_poll(self):
"""No polling needed for tradfri."""
return False
super().__init__(device, api, gateway_id)
self._unique_id = f"{gateway_id}-{device.id}"

@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):
def device_class(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
return DEVICE_CLASS_BATTERY

@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."""
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,
)
self.hass.async_create_task(self._api(cmd))
except PytradfriError as err:
_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)

self.hass.async_create_task(self.async_update_ha_state())
@property
def unit_of_measurement(self):
"""Return the unit_of_measurement of the device."""
return "%"

0 comments on commit 1059cea

Please sign in to comment.