-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhroneTV.py
123 lines (94 loc) · 3.79 KB
/
dhroneTV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
# Copyright 2018 dhrone. All Rights Reserved.
#
# pyASH imports
from pyASH.endpoint import Endpoint
from pyASH.iot import Iot
from pyASH.interface import PowerController, Speaker
from pyASH.utility import LOGLEVEL
# Setup logger
import logging
logger = logging.getLogger(__name__)
logger.setLevel(LOGLEVEL)
class iotTV(Iot):
@Iot.transformFromProperty('powerState', 'apower')
def fromPowerState(self, value):
return { 'ON': True, 'OFF': False }.get(value, value)
@Iot.transformToProperty('powerState', 'apower')
def toPowerState(self, value):
return { True: 'ON', False: 'OFF'}.get(value, value)
@Iot.transformFromProperty('input', 'asource')
def fromInput(self, value):
return value
@Iot.transformToProperty('input', 'asource')
def toInput(self, value):
return value
@Iot.transformFromProperty('muted', 'mute')
def fromMuted(self, value):
return value
@Iot.transformToProperty('muted', 'mute')
def toMuted(self, value):
return value
@Iot.transformFromProperty('volume')
def fromVolume(self, value):
return int(value / 10)
@Iot.transformToProperty('volume')
def toVolume(self, value):
return value * 10
@Endpoint.addInterface(PowerController, proactivelyReported=True, retrievable=True, uncertaintyInMilliseconds=0) ### Need to think through how to specify uncertainty for a property
@Endpoint.addInterface(Speaker)
class dhroneTV(Endpoint):
manufacturerName = 'dhrone'
description = 'iotTV controller by dhrone'
displayCategories = 'OTHER'
proactivelyReported = False
retrievable=False
uncertaintyInMilliseconds=0
@Endpoint.addDirective(['TurnOn'])
def TurnOn(self, request):
self.iot['powerState'] = True
@Endpoint.addDirective
def TurnOff(self, request):
self.iot['powerState'] = False
@Endpoint.addDirective(['AdjustVolume','SetVolume'])
def Volume(self, request):
if request.name == 'AdjustVolume':
v = self.iot['volume'] + request.payload['volume']
self.iot['volume'] = 0 if v < 0 else 100 if v > 100 else v
else:
self.iot['volume'] = request.payload['volume']
class dhroneTVScene(Endpoint):
manufacturerName = 'dhrone'
description = 'iotTV controller by dhrone'
displayCategories = 'SCENE_TRIGGER'
class Iot(iotTV):
def getThingName(self):
return self.endpointId.split(':')[0]
@Endpoint.addDirective
def Activate(self, request):
(endpointId, sceneId) = request.endpointId.split(':')
ds = { 'epower':True, 'esource': 'HDMI1', 'input': 'SAT', 'powerState':True }
self.iot.batchSet(ds)
@Endpoint.addDirective
def Deactivate(self, request):
(endpointId, sceneId) = request.endpointId.split(':')
ds = { 'epower': False, 'input': 'CD' }
self.iot.batchSet(ds)
#user = StaticUser()
#user.addEndpoint(endpointClass=dhroneTV, things='avmctrl_den', friendlyName='Sound', description='Sound by dhrone')
#user.addEndpoint(endpointClass=dhroneTVScene, things='avmctrl_den', friendlyName='TV', description='TV by dhrone'))
#ash = pyASH(user)
#lambda_handler = ash.lambda_handler
if __name__ == u'lambda_function':
def lambda_handler(request, context):
token = Request(request).token
user = DbUser(token=Request(request).token)
pyASH(user).lambda_handler(request)
if __name__ == u'__main__':
from user import DbUser
user = DbUser()
user.createTables()
user.createUser('[email protected]')
user.addEndpoint(endpointClass=dhroneTV, things='avmctrl_den', friendlyName='Sound', description='Sound by dhrone')
user.addEndpoint(endpointClass=dhroneTVScene, things='avmctrl_den', friendlyName='TV', description='TV by dhrone')
user.commit()