-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
deCONZ - support for power plugs #15752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinHjelmare
merged 8 commits into
home-assistant:dev
from
Kane610:deconz-add-switch-platform
Aug 1, 2018
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a69f9c
Initial commit for deCONZ switch support
Kane610 b1c2dcc
Fix hound comment
Kane610 d23271f
Fix martins comment; platforms shouldn't depend on another platform
Kane610 b3faee8
Fix existing tests
Kane610 b9932cf
New tests
Kane610 e2fbd69
Clean up unnecessary methods
Kane610 2b42c56
Bump requirement to v43
Kane610 9c181d9
Added device state attributes to light
Kane610 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.