-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_percentage.py
executable file
·67 lines (52 loc) · 2.44 KB
/
actions_percentage.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
import os
import sys
from .action import alexa
DEFAULT_RANGE = (0, 100)
def what_percentage(value, range):
_min, _max = range
return ( (value - _min) / (_max - _min) ) * 100
def calc_percentage(percent, range):
_min, _max = range
return (_max - _min) * (percent / 100) + _min
def clamp_percentage(percent, range):
_min, _max = range
return min(_max, max(_min, percent))
@alexa('setPercentage', 'SetPercentageRequest', 'SetPercentageConfirmation','',[],"2")
def set_percentage(self, payload):
device_id = payload['appliance']['applianceId']
items = self.items(device_id)
new_percentage = float( payload['percentageState']['value'] )
for item in items:
item_range = self.item_range(item, DEFAULT_RANGE)
item_new = calc_percentage(new_percentage, item_range)
self.logger.info("Alexa: setPercentage({}, {:.1f})".format(item.id(), item_new))
item( item_new )
return self.respond()
@alexa('incrementPercentage', 'IncrementPercentageRequest', 'IncrementPercentageConfirmation','',[],"2")
def incr_percentage(self, payload):
device_id = payload['appliance']['applianceId']
items = self.items(device_id)
percentage_delta = float( payload['deltaPercentage']['value'] )
for item in items:
item_range = self.item_range(item, DEFAULT_RANGE)
item_now = item()
percentage_now = what_percentage(item_now, item_range)
percentage_new = clamp_percentage(percentage_now + percentage_delta, item_range)
item_new = calc_percentage(percentage_new, item_range)
self.logger.info("Alexa: incrementPercentage({}, {:.1f})".format(item.id(), item_new))
item( item_new )
return self.respond()
@alexa('decrementPercentage', 'DecrementPercentageRequest', 'DecrementPercentageConfirmation','',[],"2")
def decr_percentage(self, payload):
device_id = payload['appliance']['applianceId']
items = self.items(device_id)
percentage_delta = float( payload['deltaPercentage']['value'] )
for item in items:
item_range = self.item_range(item, DEFAULT_RANGE)
item_now = item()
percentage_now = what_percentage(item_now, item_range)
percentage_new = clamp_percentage(percentage_now - percentage_delta, item_range)
item_new = calc_percentage(percentage_new, item_range)
self.logger.info("Alexa: decrementPercentage({}, {:.1f})".format(item.id(), item_new))
item( item_new )
return self.respond()