Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def async_add_device_callback(device_type, device):
hass.data[DATA_DECONZ_EVENT] = []
hass.data[DATA_DECONZ_UNSUB] = []

for component in ['binary_sensor', 'light', 'scene', 'sensor']:
for component in ['binary_sensor', 'light', 'scene', 'sensor', 'switch']:
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
config_entry, component))

Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/light/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ATTR_TRANSITION, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT,
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT,
SUPPORT_FLASH, SUPPORT_TRANSITION, Light)
from homeassistant.components.switch.deconz import SWITCH_TYPES

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.

A platform shouldn't depend on another platform. Better move this constant to the component package.

from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.util.color as color_util
Expand All @@ -32,7 +33,8 @@ def async_add_light(lights):
"""Add light from deCONZ."""
entities = []
for light in lights:
entities.append(DeconzLight(light))
if light.type not in SWITCH_TYPES:
entities.append(DeconzLight(light))
async_add_devices(entities, True)

hass.data[DATA_DECONZ_UNSUB].append(
Expand Down
94 changes: 94 additions & 0 deletions homeassistant/components/switch/deconz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
Support for deCONZ switches.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.deconz/
"""
from homeassistant.components.deconz.const import (
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB)
from homeassistant.components.switch import SwitchDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect

DEPENDENCIES = ['deconz']

SWITCH_TYPES = ["On/Off plug-in unit", "Smart plug"]


async def async_setup_platform(hass, config, async_add_devices,
discovery_info=None):
"""Old way of setting up deCONZ switches."""
pass


async def async_setup_entry(hass, config_entry, async_add_devices):
"""Set up switches for deCONZ component.

Switches are based same device class as lights in deCONZ.
"""
@callback
def async_add_switch(lights):
"""Add switch from deCONZ."""
entities = []
for light in lights:
if light.type in SWITCH_TYPES:
entities.append(DeconzSwitch(light))
async_add_devices(entities, True)

hass.data[DATA_DECONZ_UNSUB].append(
async_dispatcher_connect(hass, 'deconz_new_light', async_add_switch))

async_add_switch(hass.data[DATA_DECONZ].lights.values())


class DeconzSwitch(SwitchDevice):
"""Representation of a deCONZ switch."""

def __init__(self, switch):
"""Set up switch and add update callback to get data from websocket."""
self._switch = switch

async def async_added_to_hass(self):
"""Subscribe to switches events."""
self._switch.register_async_callback(self.async_update_callback)
self.hass.data[DATA_DECONZ_ID][self.entity_id] = self._switch.deconz_id

@callback
def async_update_callback(self, reason):
"""Update the switch's state."""
self.async_schedule_update_ha_state()

@property
def state(self):
"""Return the state of the switch."""
return self._switch.state

@property
def is_on(self):
"""Return true if switch is on."""
return self._switch.state

@property
def is_standby(self):
"""Return true if switch is in standby."""
return self._switch.state == False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'


@property
def name(self):
"""Return the name of the switch."""
return self._switch.name

@property
def unique_id(self):
"""Return a unique identifier for this switch."""
return self._switch.uniqueid

async def async_turn_on(self, **kwargs):
"""Turn on switch."""
data = {'on': True}
await self._switch.async_set_state(data)

async def async_turn_off(self, **kwargs):
"""Turn off switch."""
data = {'on': False}
await self._switch.async_set_state(data)