deCONZ - Add support for consumption and power sensors (#13218)

* Add support for consumption and power sensors

* Keep attr_current inside component
This commit is contained in:
Kane610 2018-03-15 04:07:37 +01:00 committed by Paulus Schoutsen
parent 76874e1cbc
commit e122692b46
4 changed files with 17 additions and 14 deletions

View file

@ -23,8 +23,7 @@ async def async_setup_platform(hass, config, async_add_devices,
sensors = hass.data[DATA_DECONZ].sensors sensors = hass.data[DATA_DECONZ].sensors
entities = [] entities = []
for key in sorted(sensors.keys(), key=int): for sensor in sensors.values():
sensor = sensors[key]
if sensor and sensor.type in DECONZ_BINARY_SENSOR: if sensor and sensor.type in DECONZ_BINARY_SENSOR:
entities.append(DeconzBinarySensor(sensor)) entities.append(DeconzBinarySensor(sensor))
async_add_devices(entities, True) async_add_devices(entities, True)
@ -93,9 +92,9 @@ class DeconzBinarySensor(BinarySensorDevice):
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
from pydeconz.sensor import PRESENCE from pydeconz.sensor import PRESENCE
attr = { attr = {}
ATTR_BATTERY_LEVEL: self._sensor.battery, if self._sensor.battery:
} attr[ATTR_BATTERY_LEVEL] = self._sensor.battery
if self._sensor.type in PRESENCE: if self._sensor.type in PRESENCE and self._sensor.dark:
attr['dark'] = self._sensor.dark attr['dark'] = self._sensor.dark
return attr return attr

View file

@ -17,7 +17,7 @@ from homeassistant.helpers import discovery
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util.json import load_json, save_json from homeassistant.util.json import load_json, save_json
REQUIREMENTS = ['pydeconz==31'] REQUIREMENTS = ['pydeconz==32']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View file

@ -6,7 +6,8 @@ https://home-assistant.io/components/sensor.deconz/
""" """
from homeassistant.components.deconz import ( from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID) DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.const import ATTR_BATTERY_LEVEL, CONF_EVENT, CONF_ID from homeassistant.const import (
ATTR_BATTERY_LEVEL, ATTR_VOLTAGE, CONF_EVENT, CONF_ID)
from homeassistant.core import EventOrigin, callback from homeassistant.core import EventOrigin, callback
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.icon import icon_for_battery_level from homeassistant.helpers.icon import icon_for_battery_level
@ -14,6 +15,7 @@ from homeassistant.util import slugify
DEPENDENCIES = ['deconz'] DEPENDENCIES = ['deconz']
ATTR_CURRENT = 'current'
ATTR_EVENT_ID = 'event_id' ATTR_EVENT_ID = 'event_id'
@ -27,8 +29,7 @@ async def async_setup_platform(hass, config, async_add_devices,
sensors = hass.data[DATA_DECONZ].sensors sensors = hass.data[DATA_DECONZ].sensors
entities = [] entities = []
for key in sorted(sensors.keys(), key=int): for sensor in sensors.values():
sensor = sensors[key]
if sensor and sensor.type in DECONZ_SENSOR: if sensor and sensor.type in DECONZ_SENSOR:
if sensor.type in DECONZ_REMOTE: if sensor.type in DECONZ_REMOTE:
DeconzEvent(hass, sensor) DeconzEvent(hass, sensor)
@ -106,9 +107,12 @@ class DeconzSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
attr = { attr = {}
ATTR_BATTERY_LEVEL: self._sensor.battery, if self._sensor.battery:
} attr[ATTR_BATTERY_LEVEL] = self._sensor.battery
if self.unit_of_measurement == 'Watts':
attr[ATTR_CURRENT] = self._sensor.current
attr[ATTR_VOLTAGE] = self._sensor.voltage
return attr return attr

View file

@ -701,7 +701,7 @@ pycsspeechtts==1.0.2
pydaikin==0.4 pydaikin==0.4
# homeassistant.components.deconz # homeassistant.components.deconz
pydeconz==31 pydeconz==32
# homeassistant.components.zwave # homeassistant.components.zwave
pydispatcher==2.0.5 pydispatcher==2.0.5