This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -2,7 +2,11 @@
from pydeconz.sensor import Consumption, Daylight, LightLevel, Power, Switch
from homeassistant.const import (
ATTR_BATTERY_LEVEL, ATTR_TEMPERATURE, ATTR_VOLTAGE, DEVICE_CLASS_BATTERY)
ATTR_BATTERY_LEVEL,
ATTR_TEMPERATURE,
ATTR_VOLTAGE,
DEVICE_CLASS_BATTERY,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util import slugify
@ -11,14 +15,13 @@ from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
from .deconz_device import DeconzDevice
from .gateway import get_gateway_from_config_entry
ATTR_CURRENT = 'current'
ATTR_POWER = 'power'
ATTR_DAYLIGHT = 'daylight'
ATTR_EVENT_ID = 'event_id'
ATTR_CURRENT = "current"
ATTR_POWER = "power"
ATTR_DAYLIGHT = "daylight"
ATTR_EVENT_ID = "event_id"
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Old way of setting up deCONZ platforms."""
pass
@ -34,9 +37,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
for sensor in sensors:
if not sensor.BINARY and \
not (not gateway.allow_clip_sensor and
sensor.type.startswith('CLIP')):
if not sensor.BINARY and not (
not gateway.allow_clip_sensor and sensor.type.startswith("CLIP")
):
if sensor.type in Switch.ZHATYPE:
if sensor.battery:
@ -47,8 +50,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(entities, True)
gateway.listeners.append(async_dispatcher_connect(
hass, gateway.async_event_new_device(NEW_SENSOR), async_add_sensor))
gateway.listeners.append(
async_dispatcher_connect(
hass, gateway.async_event_new_device(NEW_SENSOR), async_add_sensor
)
)
async_add_sensor(gateway.api.sensors.values())
@ -60,7 +66,7 @@ class DeconzSensor(DeconzDevice):
def async_update_callback(self, force_update=False):
"""Update the sensor's state."""
changed = set(self._device.changed_keys)
keys = {'battery', 'on', 'reachable', 'state'}
keys = {"battery", "on", "reachable", "state"}
if force_update or any(key in changed for key in keys):
self.async_schedule_update_ha_state()
@ -103,8 +109,7 @@ class DeconzSensor(DeconzDevice):
elif self._device.type in Daylight.ZHATYPE:
attr[ATTR_DAYLIGHT] = self._device.daylight
elif self._device.type in LightLevel.ZHATYPE and \
self._device.dark is not None:
elif self._device.type in LightLevel.ZHATYPE and self._device.dark is not None:
attr[ATTR_DARK] = self._device.dark
elif self._device.type in Power.ZHATYPE:
@ -121,14 +126,14 @@ class DeconzBattery(DeconzDevice):
"""Register dispatcher callback for update of battery state."""
super().__init__(device, gateway)
self._name = '{} {}'.format(self._device.name, 'Battery Level')
self._name = "{} {}".format(self._device.name, "Battery Level")
self._unit_of_measurement = "%"
@callback
def async_update_callback(self, force_update=False):
"""Update the battery's state, if needed."""
changed = set(self._device.changed_keys)
keys = {'battery', 'reachable'}
keys = {"battery", "reachable"}
if force_update or any(key in changed for key in keys):
self.async_schedule_update_ha_state()
@ -155,7 +160,5 @@ class DeconzBattery(DeconzDevice):
@property
def device_state_attributes(self):
"""Return the state attributes of the battery."""
attr = {
ATTR_EVENT_ID: slugify(self._device.name),
}
attr = {ATTR_EVENT_ID: slugify(self._device.name)}
return attr