Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions homeassistant/components/binary_sensor/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import asyncio

from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.deconz import DOMAIN as DECONZ_DATA
from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.const import ATTR_BATTERY_LEVEL
from homeassistant.core import callback

Expand All @@ -21,27 +22,29 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
return

from pydeconz.sensor import DECONZ_BINARY_SENSOR
sensors = hass.data[DECONZ_DATA].sensors
sensors = hass.data[DATA_DECONZ].sensors
entities = []

for key in sorted(sensors.keys(), key=int):
sensor = sensors[key]
if sensor and sensor.type in DECONZ_BINARY_SENSOR:
entities.append(DeconzBinarySensor(sensor))
entities.append(DeconzBinarySensor(hass, sensor))
async_add_devices(entities, True)


class DeconzBinarySensor(BinarySensorDevice):
"""Representation of a binary sensor."""

def __init__(self, sensor):
def __init__(self, hass, sensor):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't pass in hass. It will be set on the entity when the entity has been added to home assistant. This means you can access hass with self.hass, after the entity has been added to home assistant.

"""Set up sensor and add update callback to get data from websocket."""
self.hass = hass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this.

self._sensor = sensor

@asyncio.coroutine
def async_added_to_hass(self):
"""Subscribe sensors events."""
self._sensor.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id

@callback
def async_update_callback(self, reason):
Expand Down
22 changes: 19 additions & 3 deletions homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/deconz/
"""

import asyncio
import logging
from itertools import chain

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'itertools.chain' imported but unused

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'itertools.chain' imported but unused


import voluptuous as vol

Expand All @@ -17,11 +19,12 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util.json import load_json, save_json

REQUIREMENTS = ['pydeconz==27']
REQUIREMENTS = ['pydeconz==28']

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'deconz'
DATA_DECONZ_ID = 'deconz_entities'

CONFIG_FILE = 'deconz.conf'

Expand All @@ -34,13 +37,16 @@
}, extra=vol.ALLOW_EXTRA)

SERVICE_FIELD = 'field'
SERVICE_ENTITY = 'entity'
SERVICE_DATA = 'data'

SERVICE_SCHEMA = vol.Schema({
vol.Required(SERVICE_FIELD): cv.string,
vol.Exclusive(SERVICE_FIELD, 'deconz_id'): cv.string,
vol.Exclusive(SERVICE_ENTITY, 'deconz_id'): cv.entity_id,
vol.Required(SERVICE_DATA): dict,
})


CONFIG_INSTRUCTIONS = """
Unlock your deCONZ gateway to register with Home Assistant.

Expand Down Expand Up @@ -100,6 +106,7 @@ def async_setup_deconz(hass, config, deconz_config):
return False

hass.data[DOMAIN] = deconz
hass.data[DATA_DECONZ_ID] = {}

for component in ['binary_sensor', 'light', 'scene', 'sensor']:
hass.async_add_job(discovery.async_load_platform(
Expand All @@ -112,6 +119,7 @@ def async_configure(call):

Field is a string representing a specific device in deCONZ
e.g. field='/lights/1/state'.
Entity_id can be used to retrieve the proper field.
Data is a json object with what data you want to alter
e.g. data={'on': true}.
{
Expand All @@ -121,9 +129,17 @@ def async_configure(call):
See Dresden Elektroniks REST API documentation for details:
http://dresden-elektronik.github.io/deconz-rest-doc/rest/
"""
deconz = hass.data[DOMAIN]
field = call.data.get(SERVICE_FIELD)
entity_id = call.data.get(SERVICE_ENTITY)
data = call.data.get(SERVICE_DATA)
deconz = hass.data[DOMAIN]
if entity_id:
entities = hass.data.get(DATA_DECONZ_ID)
if entities:
field = entities.get(entity_id)
if field is None:
_LOGGER.error('Could not find the entity %s', entity_id)
return
yield from deconz.async_put_state(field, data)
hass.services.async_register(
DOMAIN, 'configure', async_configure, schema=SERVICE_SCHEMA)
Expand Down
7 changes: 5 additions & 2 deletions homeassistant/components/deconz/services.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

configure:
description: Set attribute of device in Deconz. See Dresden Elektroniks REST API documentation for details http://dresden-elektronik.github.io/deconz-rest-doc/rest/
description: Set attribute of device in deCONZ. See https://home-assistant.io/components/deconz/#device-services for details.
fields:
field:
description: Field is a string representing a specific device in Deconz.
description: Field is a string representing a specific device in deCONZ.
example: '/lights/1/state'
entity:
description: Entity id representing a specific device in deCONZ.
example: 'light.rgb_light'
data:
description: Data is a json object with what data you want to alter.
example: '{"on": true}'
17 changes: 9 additions & 8 deletions homeassistant/components/light/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"""
import asyncio

from homeassistant.components.deconz import DOMAIN as DECONZ_DATA
from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_RGB_COLOR,
ATTR_TRANSITION, ATTR_XY_COLOR, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT,
Expand All @@ -17,33 +18,32 @@

DEPENDENCIES = ['deconz']

ATTR_LIGHT_GROUP = 'LightGroup'


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the deCONZ light."""
if discovery_info is None:
return

lights = hass.data[DECONZ_DATA].lights
groups = hass.data[DECONZ_DATA].groups
lights = hass.data[DATA_DECONZ].lights
groups = hass.data[DATA_DECONZ].groups
entities = []

for light in lights.values():
entities.append(DeconzLight(light))
entities.append(DeconzLight(hass, light))

for group in groups.values():
if group.lights: # Don't create entity for group not containing light
entities.append(DeconzLight(group))
entities.append(DeconzLight(hass, group))
async_add_devices(entities, True)


class DeconzLight(Light):
"""Representation of a deCONZ light."""

def __init__(self, light):
def __init__(self, hass, light):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't pass in hass. It will be set on the entity when the entity has been added to home assistant. This means you can access hass with self.hass, after the entity has been added to home assistant.

"""Set up light and add update callback to get data from websocket."""
self.hass = hass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self._light = light

self._features = SUPPORT_BRIGHTNESS
Expand All @@ -64,6 +64,7 @@ def __init__(self, light):
def async_added_to_hass(self):
"""Subscribe to lights events."""
self._light.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._light.deconz_id

@callback
def async_update_callback(self, reason):
Expand Down
15 changes: 11 additions & 4 deletions homeassistant/components/scene/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"""
import asyncio

from homeassistant.components.deconz import DOMAIN as DECONZ_DATA
from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.components.scene import Scene

DEPENDENCIES = ['deconz']
Expand All @@ -18,21 +19,27 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if discovery_info is None:
return

scenes = hass.data[DECONZ_DATA].scenes
scenes = hass.data[DATA_DECONZ].scenes
entities = []

for scene in scenes.values():
entities.append(DeconzScene(scene))
entities.append(DeconzScene(hass, scene))
async_add_devices(entities)


class DeconzScene(Scene):
"""Representation of a deCONZ scene."""

def __init__(self, scene):
def __init__(self, hass, scene):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

"""Set up a scene."""
self.hass = hass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

self._scene = scene

@asyncio.coroutine
def async_added_to_hass(self):
"""Subscribe to sensors events."""
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._scene.deconz_id

@asyncio.coroutine
def async_activate(self):
"""Activate the scene."""
Expand Down
17 changes: 11 additions & 6 deletions homeassistant/components/sensor/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"""
import asyncio

from homeassistant.components.deconz import DOMAIN as DECONZ_DATA
from homeassistant.components.deconz import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID)
from homeassistant.const import ATTR_BATTERY_LEVEL, CONF_EVENT, CONF_ID
from homeassistant.core import EventOrigin, callback
from homeassistant.helpers.entity import Entity
Expand All @@ -25,7 +26,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
return

from pydeconz.sensor import DECONZ_SENSOR, SWITCH as DECONZ_REMOTE
sensors = hass.data[DECONZ_DATA].sensors
sensors = hass.data[DATA_DECONZ].sensors
entities = []

for key in sorted(sensors.keys(), key=int):
Expand All @@ -34,23 +35,25 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if sensor.type in DECONZ_REMOTE:
DeconzEvent(hass, sensor)
if sensor.battery:
entities.append(DeconzBattery(sensor))
entities.append(DeconzBattery(hass, sensor))
else:
entities.append(DeconzSensor(sensor))
entities.append(DeconzSensor(hass, sensor))
async_add_devices(entities, True)


class DeconzSensor(Entity):
"""Representation of a sensor."""

def __init__(self, sensor):
def __init__(self, hass, sensor):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

"""Set up sensor and add update callback to get data from websocket."""
self.hass = hass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

self._sensor = sensor

@asyncio.coroutine
def async_added_to_hass(self):
"""Subscribe to sensors events."""
self._sensor.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._sensor.deconz_id

@callback
def async_update_callback(self, reason):
Expand Down Expand Up @@ -116,8 +119,9 @@ def device_state_attributes(self):
class DeconzBattery(Entity):
"""Battery class for when a device is only represented as an event."""

def __init__(self, device):
def __init__(self, hass, device):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

"""Register dispatcher callback for update of battery state."""
self.hass = hass

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

self._device = device
self._name = '{} {}'.format(self._device.name, 'Battery Level')
self._device_class = 'battery'
Expand All @@ -127,6 +131,7 @@ def __init__(self, device):
def async_added_to_hass(self):
"""Subscribe to sensors events."""
self._device.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._device.deconz_id

@callback
def async_update_callback(self, reason):
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ pycsspeechtts==1.0.2
pydaikin==0.4

# homeassistant.components.deconz
pydeconz==27
pydeconz==28

# homeassistant.components.zwave
pydispatcher==2.0.5
Expand Down