Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
193 additions
and
2 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
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 |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
[ | ||
'philips_hue', | ||
'razer_chroma', | ||
'thingm_blink' | ||
'thingm_blink', | ||
'xiaomi_yeelight' | ||
] |
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
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 @@ | ||
from .core import init, run, process |
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,104 @@ | ||
import socket | ||
from chroma_feedback import wording | ||
from .factory import bulb_factory | ||
|
||
args = None | ||
|
||
|
||
def init(program): | ||
global args | ||
|
||
if not args: | ||
ip = discover_ips() | ||
|
||
if ip: | ||
program.add_argument('--xiaomi-yeelight-ip', default = ip) | ||
else: | ||
print(wording.get('discovery_no').format('IP') + wording.get('exclamation_mark')) | ||
print() | ||
program.add_argument('--xiaomi-yeelight-ip', required = True) | ||
args = program.parse_known_args()[0] | ||
|
||
|
||
def run(status): | ||
devices = [] | ||
|
||
for ip in args.xiaomi_yeelight_ip: | ||
devices.append(bulb_factory(ip)) | ||
if not devices: | ||
exit(wording.get('device_no') + wording.get('exclamation_mark')) | ||
return process(status, devices) | ||
|
||
|
||
def process(status, devices): | ||
result = [] | ||
|
||
# process devices | ||
|
||
for device in devices: | ||
device_name = device.get_properties()['name'] | ||
|
||
if status == 'passed': | ||
result.append( | ||
{ | ||
'consumer': 'xiaomi_yeelight', | ||
'name': device_name, | ||
'active': static(device, [0, 255, 0]), | ||
'status': status | ||
}) | ||
if status == 'process': | ||
result.append( | ||
{ | ||
'consumer': 'xiaomi_yeelight', | ||
'name': device_name, | ||
'active': static(device, [255, 255, 0]), | ||
'status': status | ||
}) | ||
if status == 'errored': | ||
result.append( | ||
{ | ||
'consumer': 'xiaomi_yeelight', | ||
'name': device_name, | ||
'active': pulsate(device, [255, 255, 255]), | ||
'status': status | ||
}) | ||
if status == 'failed': | ||
result.append( | ||
{ | ||
'consumer': 'xiaomi_yeelight', | ||
'name': device_name, | ||
'active': pulsate(device, [255, 0, 0]), | ||
'status': status | ||
}) | ||
return result | ||
|
||
|
||
def static(device, rgb): | ||
return device.set_rgb(rgb[0], rgb[1], rgb[2]) == 'ok' | ||
|
||
|
||
def pulsate(device, rgb): | ||
return device.set_rgb(rgb[0], rgb[1], rgb[2]) == 'ok' | ||
|
||
|
||
def discover_ips(): | ||
message =\ | ||
[ | ||
'M-SEARCH * HTTP/1.1', | ||
'HOST: 239.255.255.250:1982', | ||
'MAN: "ssdp:discover"', | ||
'ST: wifi_bulb' | ||
] | ||
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | ||
SOCKET.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32) | ||
SOCKET.settimeout(1) | ||
SOCKET.sendto('\r\n'.join(message).encode(), ('239.255.255.250', 1982)) | ||
ips = [] | ||
|
||
while True: | ||
try: | ||
ip = SOCKET.recvfrom(65507)[1][0] | ||
except socket.timeout: | ||
break | ||
ips.append(ip) | ||
return list(dict.fromkeys(ips)) |
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,21 @@ | ||
from chroma_feedback import wording | ||
|
||
|
||
def bulb_factory(ip): | ||
bulb = None | ||
|
||
# handle import | ||
|
||
try: | ||
from yeelight import Bulb, BulbException | ||
except ImportError: | ||
exit(wording.get('package_no').format('YEELIGHT') + wording.get('exclamation_mark')) | ||
|
||
# create instance | ||
|
||
try: | ||
bulb = Bulb(ip) | ||
bulb.turn_on() | ||
except BulbException: | ||
exit(wording.get('connection_no').format('YEELIGHT') + wording.get('exclamation_mark')) | ||
return bulb |
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
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
Empty file.
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,40 @@ | ||
from mock import MagicMock | ||
from chroma_feedback.consumer import xiaomi_yeelight | ||
|
||
mock = MagicMock() | ||
devices =\ | ||
[ | ||
mock | ||
] | ||
|
||
|
||
def test_process_passed(): | ||
result = xiaomi_yeelight.process('passed', devices) | ||
|
||
assert result[0]['consumer'] == 'xiaomi_yeelight' | ||
assert result[0]['name'] | ||
assert result[0]['status'] == 'passed' | ||
|
||
|
||
def test_process_process(): | ||
result = xiaomi_yeelight.process('process', devices) | ||
|
||
assert result[0]['consumer'] == 'xiaomi_yeelight' | ||
assert result[0]['name'] | ||
assert result[0]['status'] == 'process' | ||
|
||
|
||
def test_process_errored(): | ||
result = xiaomi_yeelight.process('errored', devices) | ||
|
||
assert result[0]['consumer'] == 'xiaomi_yeelight' | ||
assert result[0]['name'] | ||
assert result[0]['status'] == 'errored' | ||
|
||
|
||
def test_process_failed(): | ||
result = xiaomi_yeelight.process('failed', devices) | ||
|
||
assert result[0]['consumer'] == 'xiaomi_yeelight' | ||
assert result[0]['name'] | ||
assert result[0]['status'] == 'failed' |