Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changelog: Add support for groups
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,18 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
try: | ||
from setuptools import setup | ||
except ImportError: | ||
from distutils.core import setup | ||
|
||
setup( | ||
name='Group support', | ||
version='0.1', | ||
packages=['group'], | ||
package_dir = {'':'src'}, | ||
entry_points={ \ | ||
'telldus.startup': ['c = group:Group [cREQ]'] | ||
}, | ||
extras_require = dict(cREQ = 'Base>=0.1\nTelldus>=0.1'), | ||
) |
This file contains 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,116 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from base import implements, Plugin | ||
from telldus import DeviceManager, Device | ||
from tellduslive.base import TelldusLive, ITelldusLiveObserver | ||
|
||
class GroupDevice(Device): | ||
def __init__(self): | ||
super(GroupDevice,self).__init__() | ||
self._nodeId = 0 | ||
self.devices = [] | ||
|
||
def _command(self, action, value, success, failure, ignore, **kwargs): | ||
for deviceId in self.devices: | ||
device = self.manager().device(deviceId) | ||
if not device: | ||
continue | ||
device.command(action, value, origin='Group %s' % self.name(), success=None, failure=None, ignore=ignore) | ||
success() | ||
|
||
def containingDevices(self): | ||
return self.devices | ||
|
||
def isDevice(self): | ||
return True | ||
|
||
def isSensor(self): | ||
return False | ||
|
||
def localId(self): | ||
return self._nodeId | ||
|
||
def params(self): | ||
return { | ||
'devices': self.devices, | ||
} | ||
|
||
def setId(self, newId): | ||
self._nodeId = newId | ||
super(GroupDevice,self).setId(newId) | ||
|
||
def setNodeId(self, nodeId): | ||
self._nodeId = nodeId | ||
|
||
def setParams(self, params): | ||
self.devices = params.setdefault('devices', []) | ||
|
||
def typeString(self): | ||
return 'group' | ||
|
||
def methods(self): | ||
m = 0 | ||
for device in self.flattenContainingDevices(): | ||
m = m | device.methods() | ||
return m | ||
|
||
class Group(Plugin): | ||
implements(ITelldusLiveObserver) | ||
|
||
def __init__(self): | ||
self.devices = [] | ||
self.deviceManager = DeviceManager(self.context) | ||
for d in self.deviceManager.retrieveDevices('group'): | ||
p = d.params() | ||
device = GroupDevice() | ||
self.devices.append(device) | ||
device.setNodeId(d.id()) | ||
device.setParams(p) | ||
self.deviceManager.addDevice(device) | ||
self.deviceManager.finishedLoading('group') | ||
self.live = TelldusLive(self.context) | ||
|
||
def addDevice(self, name, devices): | ||
if type(devices) != list: | ||
return | ||
device = GroupDevice() | ||
device.setName(name) | ||
device.setParams({ | ||
'devices': devices | ||
}) | ||
self.devices.append(device) | ||
self.deviceManager.addDevice(device) | ||
|
||
@TelldusLive.handler('group') | ||
def __handleCommand(self, msg): | ||
data = msg.argument(0).toNative() | ||
action = data['action'] | ||
if action == 'addGroup': | ||
self.addDevice(data['name'], data['devices']) | ||
|
||
elif action == 'editGroup': | ||
deviceId = data['device'] | ||
for device in self.devices: | ||
if device.id() == deviceId: | ||
device.setParams({ | ||
'devices': data['devices'], | ||
}) | ||
device.paramUpdated('') | ||
break | ||
|
||
elif action == 'groupInfo': | ||
deviceId = data['device'] | ||
for device in self.devices: | ||
if device.id() == deviceId: | ||
params = device.params() | ||
params['deviceId'] = deviceId | ||
self.live.pushToWeb('group', 'groupInfo', params) | ||
return | ||
|
||
elif action == 'remove': | ||
deviceId = data['device'] | ||
for device in self.devices: | ||
if device.id() == deviceId: | ||
self.deviceManager.removeDevice(deviceId) | ||
self.devices.remove(device) | ||
return |
This file contains 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,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from Group import Group |