File: aiolifx.py

package info (click to toggle)
python-aiolifx 1.0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: python: 7,289; makefile: 4
file content (2353 lines) | stat: -rw-r--r-- 92,476 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#
# This application is simply a bridge application for Lifx bulbs.
#
# Copyright (c) 2016, 2024 François Wautier
# Copyright (c) 2018, 2024 Avi Miller <me@dje.li>
# Copyright (c) 2022 Michael Farrell <micolous+git@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies
# or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
import asyncio as aio
import logging
from typing import Any, Coroutine, Set
from .message import BROADCAST_MAC, BROADCAST_SOURCE_ID
from .msgtypes import *
from .products import *
from .unpack import unpack_lifx_message
from functools import partial
from math import floor
import time, random, datetime, socket, ifaddr

# prevent tasks from being garbage collected
_BACKGROUND_TASKS: Set[aio.Task] = set()

if sys.version_info[:2] < (3, 11):
    from async_timeout import timeout as asyncio_timeout
else:
    from asyncio import timeout as asyncio_timeout

# A couple of constants
LISTEN_IP = "0.0.0.0"
UDP_BROADCAST_IP = "255.255.255.255"
UDP_BROADCAST_PORT = 56700
DEFAULT_TIMEOUT = 0.5  # How long to wait for an ack or response
DEFAULT_ATTEMPTS = 3  # How many time shou;d we try to send to the bulb`
DISCOVERY_INTERVAL = 180
DISCOVERY_STEP = 5
MAX_UNSIGNED_16_BIT_INTEGER_VALUE = int("0xFFFF", 16)
_LOGGER = logging.getLogger(__name__)


def _create_background_task(coro: Coroutine) -> None:
    """Create a background task that will not be garbage collected."""
    global _BACKGROUND_TASKS
    task = aio.create_task(coro)
    _BACKGROUND_TASKS.add(task)
    task.add_done_callback(_BACKGROUND_TASKS.discard)


def mac_to_ipv6_linklocal(mac, prefix="fe80::"):
    """Translate a MAC address into an IPv6 address in the prefixed network.

    This function calculates the EUI (Extended Unique Identifier) from the given
    MAC address and prepend the needed prefix to come up with a valid IPv6 address.
    The default prefix is the link local prefix defined by RFC 4291 .

        :param mac: the mac address of the device
        :type mac: str
        :param prefix: the IPv6 network prefix
        :type prefix: str
        :returns: IPv6 address
        :rtype: str

    """

    # Remove the most common delimiters; dots, dashes, etc.
    mac_value = int(
        mac.translate(str.maketrans(dict([(x, None) for x in [" ", ".", ":", "-"]]))),
        16,
    )
    # Split out the bytes that slot into the IPv6 address
    # XOR the most significant byte with 0x02, inverting the
    # Universal / Local bit
    high2 = mac_value >> 32 & 0xFFFF ^ 0x0200
    high1 = mac_value >> 24 & 0xFF
    low1 = mac_value >> 16 & 0xFF
    low2 = mac_value & 0xFFFF
    return prefix + ":{:04x}:{:02x}ff:fe{:02x}:{:04x}".format(high2, high1, low1, low2)


def nanosec_to_hours(ns):
    """Convert nanoseconds to hours

    :param ns: Number of nanoseconds
    :type ns: into
    :returns: ns/(1000000000.0*60*60)
    :rtype: int
    """
    return ns / (1000000000.0 * 60 * 60)


class Device(aio.DatagramProtocol):
    """Connection to a given Lifx device.

    :param loop: The asyncio loop being used
    :type loop: asyncio.AbstractEventLoop
    :param: mac_addr: The device MAC address aa:bb:cc:dd:ee:ff
    :type mac_addr: string
    :param ip_addr: The devie IP address (either IPv4 or IPv6)
    :type ip_addr: string
    :param port: The port used by the unicast connection
    :type port: into
    :param parent: Parent object with register/unregister methods
    :type parent: object
    :returns: an asyncio DatagramProtocol to handle communication with the device
    :rtype: DatagramProtocol
    """

    def __init__(self, loop, mac_addr, ip_addr, port, parent=None):
        self.loop = loop
        self.mac_addr = mac_addr
        self.ip_addr = ip_addr
        self.port = port
        self.parent = parent
        self.registered = False
        self.retry_count = DEFAULT_ATTEMPTS
        self.timeout = DEFAULT_TIMEOUT
        self.unregister_timeout = DEFAULT_TIMEOUT
        self.transport = None
        self.task = None
        self.seq = 0
        # Key is the message sequence, value is (Response, Event, callb )
        self.message = {}
        self.source_id = random.randint(0, (2**32) - 1)
        # Default callback for unexpected messages
        self.default_callb = None
        # And the rest
        self.label = None
        self.location = None
        self.group = None
        self.power_level = None
        self.vendor = None
        self.product = None
        self.version = None
        self.host_firmware_version = None
        self.host_firmware_build_timestamp = None
        self.wifi_firmware_version = None
        self.wifi_firmware_build_timestamp = None
        self.lastmsg = datetime.datetime.now()

    def seq_next(self):
        """Method to return the next sequence value to use in messages.

        :returns: next number in sequensce (modulo 128)
        :rtype: int
        """
        self.seq = (self.seq + 1) % 128
        return self.seq

    #
    #                            Protocol Methods
    #

    def connection_made(self, transport):
        """Method run when the connection to the lamp is established"""
        self.transport = transport
        self.register()

    def error_received(self, exc: Exception) -> None:
        """Method run when an error is received from the device.

        This method is called in rare conditions, when the transport (e.g. UDP)
        detects that a datagram could not be delivered to its recipient.
        In many conditions though, undeliverable datagrams will be silently dropped.
        """
        _LOGGER.debug("%s: Error received: %s", self.ip_addr, exc)
        # Clear the message queue since we know they are not going to be answered
        # and there is no point in waiting for them
        for entry in self.message.values():
            response_type, myevent, callb = entry
            if response_type != Acknowledgement:
                if callb:
                    callb(self, None)
                if myevent:
                    myevent.set()
        self.message.clear()

    def datagram_received(self, data, addr):
        """Method run when data is received from the device

        This method will unpack the data according to the LIFX protocol.
        If the message represents some state information, it will update
        the device state. Following that it will execute the callback corresponding
        to the message sequence number. If there is no sequence number, the
        default callback will be called.

            :param data: raw data
            :type data: bytestring
            :param addr: sender IP address 2-tuple for IPv4, 4-tuple for IPv6
            :type addr: tuple
        """
        self.register()
        response = unpack_lifx_message(data)
        self.lastmsg = datetime.datetime.now()
        if response.seq_num in self.message:
            response_type, myevent, callb = self.message[response.seq_num]
            if type(response) == response_type:
                if response.source_id == self.source_id:
                    if "State" in response.__class__.__name__:
                        setmethod = (
                            "resp_set_"
                            + response.__class__.__name__.replace("State", "").lower()
                        )
                        method = getattr(self, setmethod, None)
                        if method:
                            method(response)
                    if callb:
                        callb(self, response)
                    myevent.set()
                del self.message[response.seq_num]
            elif type(response) == Acknowledgement:
                pass
            else:
                del self.message[response.seq_num]
        elif self.default_callb:
            self.default_callb(response)

    def register(self):
        """Proxy method to register the device with the parent."""
        if not self.registered:
            self.registered = True
            if self.parent:
                self.parent.register(self)

    def unregister(self):
        """Proxy method to unregister the device with the parent."""
        if self.registered:
            # Only if we have not received any message recently.
            if (
                datetime.datetime.now()
                - datetime.timedelta(seconds=self.unregister_timeout)
                > self.lastmsg
            ):
                self.registered = False
                if self.parent:
                    self.parent.unregister(self)

    def cleanup(self):
        """Method to call to cleanly terminate the connection to the device."""
        if self.transport:
            self.transport.close()
            self.transport = None
        if self.task:
            self.task.cancel()
            self.task = None

    #
    #                            Workflow Methods
    #

    async def fire_sending(self, msg, num_repeats):
        """Coroutine used to send message to the device when no response is needed.

        :param msg: Message to send
        :type msg: aiolifx.
        :param num_repeats: number of times the message is to be sent.
        :returns: The coroutine that can be scheduled to run
        :rtype: coroutine
        """
        if num_repeats is None:
            num_repeats = self.retry_count
        sent_msg_count = 0
        sleep_interval = 0.05
        while sent_msg_count < num_repeats:
            if self.transport:
                self.transport.sendto(msg.packed_message)
            sent_msg_count += 1
            await aio.sleep(
                sleep_interval
            )  # Max num of messages device can handle is 20 per second.

    # Don't wait for Acks or Responses, just send the same message repeatedly as fast as possible
    def fire_and_forget(
        self, msg_type, payload={}, timeout_secs=None, num_repeats=None
    ):
        """Method used to send message to the device when no response/ack is needed.

        :param msg_type: The type of the message to send, a subclass of aiolifx.Message
        :type msg_type: class
        :param payload: value to use when instantiating msg_type
        :type payload: dict
        :param timeout_secs: Not used. Present here only for consistency with other methods
        :type timeout_secs: None
        :param num_repeats: Number of times the message is to be sent.
        :type num_repeats: int
        :returns: Always True
        :rtype: bool
        """
        msg = msg_type(
            self.mac_addr,
            self.source_id,
            seq_num=0,
            payload=payload,
            ack_requested=False,
            response_requested=False,
        )
        _create_background_task(self.fire_sending(msg, num_repeats))
        return True

    async def try_sending(self, msg, timeout_secs, max_attempts):
        """Coroutine used to send message to the device when a response or ack is needed.

        This coroutine will try to send up to max_attempts time the message, waiting timeout_secs
        for an answer. If no answer is received, it will consider that the device is no longer
        accessible and will unregister it.

            :param msg: The message to send
            :type msg: aiolifx.Message
            :param timeout_secs: Number of seconds to wait for a response or ack
            :type timeout_secs: int
            :param max_attempts: .
            :type max_attempts: int
            :returns: a coroutine to be scheduled
            :rtype: coroutine
        """
        if timeout_secs is None:
            timeout_secs = self.timeout
        if max_attempts is None:
            max_attempts = self.retry_count

        attempts = 0
        while attempts < max_attempts:
            if msg.seq_num not in self.message:
                return
            event = aio.Event()
            self.message[msg.seq_num][1] = event
            attempts += 1
            if self.transport:
                self.transport.sendto(msg.packed_message)
            try:
                async with asyncio_timeout(timeout_secs):
                    await event.wait()
                break
            except Exception as inst:
                if attempts >= max_attempts:
                    if msg.seq_num in self.message:
                        callb = self.message[msg.seq_num][2]
                        if callb:
                            callb(self, None)
                        del self.message[msg.seq_num]
                    # It's dead Jim
                    self.unregister()

    # Usually used for Set messages
    def req_with_ack(
        self, msg_type, payload, callb=None, timeout_secs=None, max_attempts=None
    ):
        """Method to send a message expecting to receive an ACK.

        :param msg_type: The type of the message to send, a subclass of aiolifx.Message
        :type msg_type: class
        :param payload: value to use when instantiating msg_type
        :type payload: dict
        :param callb: A callback that will be executed when the ACK is received in datagram_received
        :type callb: callable
        :param timeout_secs: Number of seconds to wait for an ack
        :type timeout_secs: int
        :param max_attempts: .
        :type max_attempts: int
        :returns: True
        :rtype: bool
        """
        msg = msg_type(
            self.mac_addr,
            self.source_id,
            seq_num=self.seq_next(),
            payload=payload,
            ack_requested=True,
            response_requested=False,
        )
        self.message[msg.seq_num] = [Acknowledgement, None, callb]
        _create_background_task(self.try_sending(msg, timeout_secs, max_attempts))
        return True

    # Usually used for Get messages, or for state confirmation after Set (hence the optional payload)
    def req_with_resp(
        self,
        msg_type,
        response_type,
        payload={},
        callb=None,
        timeout_secs=None,
        max_attempts=None,
    ):
        """Method to send a message expecting to receive a response.

        :param msg_type: The type of the message to send, a subclass of aiolifx.Message
        :type msg_type: class
        :param response_type: The type of the response to expect, a subclass of aiolifx.Message
        :type response_type: class
        :param payload: value to use when instantiating msg_type
        :type payload: dict
        :param callb: A callback that will be executed when the response is received in datagram_received
        :type callb: callable
        :param timeout_secs: Number of seconds to wait for a response
        :type timeout_secs: int
        :param max_attempts: .
        :type max_attempts: int
        :returns: True
        :rtype: bool
        """
        msg = msg_type(
            self.mac_addr,
            self.source_id,
            seq_num=self.seq_next(),
            payload=payload,
            ack_requested=False,
            response_requested=True,
        )
        self.message[msg.seq_num] = [response_type, None, callb]
        _create_background_task(self.try_sending(msg, timeout_secs, max_attempts))
        return True

    # Not currently implemented, although the LIFX LAN protocol supports this kind of workflow natively
    def req_with_ack_resp(
        self,
        msg_type,
        response_type,
        payload,
        callb=None,
        timeout_secs=None,
        max_attempts=None,
    ):
        """Method to send a message expecting to receive both a response and an ack.

        :param msg_type: The type of the message to send, a subclass of aiolifx.Message
        :type msg_type: class
        :param payload: value to use when instantiating msg_type
        :type payload: dict
        :param callb: A callback that will be executed when the response is received in datagram_received
        :type callb: callable
        :param timeout_secs: Number of seconds to wait for a response
        :type timeout_secs: int
        :param max_attempts: .
        :type max_attempts: int
        :returns: True
        :rtype: bool
        """
        msg = msg_type(
            self.mac_addr,
            self.source_id,
            seq_num=self.seq_next(),
            payload=payload,
            ack_requested=True,
            response_requested=True,
        )
        self.message[msg.seq_num] = [response_type, None, callb]
        _create_background_task(self.try_sending(msg, timeout_secs, max_attempts))
        return True

    #
    #                            Attribute Methods
    #
    def get_label(self, callb=None):
        """Convenience method to request the label from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: The cached value
            :rtype: str
        """
        if self.label is None:
            mypartial = partial(self.resp_set_label)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            response = self.req_with_resp(GetLabel, StateLabel, callb=mycallb)
        return self.label

    def set_label(self, value, callb=None):
        """Convenience method to set the label of the device

        This method will send a SetLabel message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param value: The new label
            :type value: str
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: None
            :rtype: None
        """
        if len(value) > 32:
            value = value[:32]
        mypartial = partial(self.resp_set_label, label=value)
        if callb:
            self.req_with_ack(
                SetLabel, {"label": value}, lambda x, y: (mypartial(y), callb(x, y))
            )
        else:
            self.req_with_ack(SetLabel, {"label": value}, lambda x, y: mypartial(y))

    def resp_set_label(self, resp, label=None):
        """Default callback for get_label/set_label"""
        if label:
            self.label = label
        elif resp:
            self.label = resp.label.decode().replace("\x00", "")

    def get_location(self, callb=None):
        """Convenience method to request the location from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: The cached value
            :rtype: str
        """
        if self.location is None:
            mypartial = partial(self.resp_set_location)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            response = self.req_with_resp(GetLocation, StateLocation, callb=mycallb)
        return self.location

    # def set_location(self, value,callb=None):
    # mypartial=partial(self.resp_set_location,location=value)
    # if callb:
    # self.req_with_ack(SetLocation, {"location": value},lambda x,y:(mypartial(y),callb(x,y)) )
    # else:
    # self.req_with_ack(SetLocation, {"location": value},lambda x,y:mypartial(y) )

    def resp_set_location(self, resp, location=None):
        """Default callback for get_location/set_location"""
        if location:
            self.location = location
        elif resp:
            self.location = resp.label.decode().replace("\x00", "")
            # self.resp_set_label(resp)

    def get_group(self, callb=None):
        """Convenience method to request the group from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: The cached value
            :rtype: str
        """
        if self.group is None:
            mypartial = partial(self.resp_set_group)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            response = self.req_with_resp(GetGroup, StateGroup, callb=callb)
        return self.group

    # Not implemented. Why?
    # def set_group(self, value,callb=None):
    # if callb:
    # self.req_with_ack(SetGroup, {"group": value},lambda x,y:(partial(self.resp_set_group,group=value)(y),callb(x,y)) )
    # else:
    # self.req_with_ack(SetGroup, {"group": value},lambda x,y:partial(self.resp_set_group,group=value)(y) )

    def resp_set_group(self, resp, group=None):
        """Default callback for get_group/set_group"""
        if group:
            self.group = group
        elif resp:
            self.group = resp.label.decode().replace("\x00", "")

    def get_power(self, callb=None):
        """Convenience method to request the power status from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        if self.power_level is None:
            response = self.req_with_resp(GetPower, StatePower, callb=callb)
        return self.power_level

    def set_power(self, value, callb=None, rapid=False):
        """Convenience method to set the power status of the device

        This method will send a SetPower message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param value: The new state
            :type value: str/bool/int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        on = [True, 1, "on"]
        off = [False, 0, "off"]
        mypartial = partial(self.resp_set_power, power_level=value)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)
        if value in on and not rapid:
            response = self.req_with_ack(
                SetPower,
                {"power_level": MAX_UNSIGNED_16_BIT_INTEGER_VALUE},
                callb=mycallb,
            )
        elif value in off and not rapid:
            response = self.req_with_ack(SetPower, {"power_level": 0}, callb=mycallb)
        elif value in on and rapid:
            response = self.fire_and_forget(
                SetPower, {"power_level": MAX_UNSIGNED_16_BIT_INTEGER_VALUE}
            )
            self.power_level = MAX_UNSIGNED_16_BIT_INTEGER_VALUE
        elif value in off and rapid:
            response = self.fire_and_forget(SetPower, {"power_level": 0})
            self.power_level = 0

    def resp_set_power(self, resp, power_level=None):
        """Default callback for get_power/set_power"""
        if power_level is not None:
            self.power_level = power_level
        elif resp:
            self.power_level = resp.power_level

    def get_wififirmware(self, callb=None):
        """Convenience method to request the wifi firmware info from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: The cached value (version, timestamp)
            :rtype: 2-tuple
        """
        if self.wifi_firmware_version is None:
            mypartial = partial(self.resp_set_wififirmware)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            response = self.req_with_resp(
                GetWifiFirmware, StateWifiFirmware, callb=mycallb
            )
        return (self.wifi_firmware_version, self.wifi_firmware_build_timestamp)

    def resp_set_wififirmware(self, resp):
        """Default callback for get_wififirmware"""
        if resp:
            self.wifi_firmware_version = float(
                str(str(resp.version >> 16) + "." + str(resp.version & 0xFF))
            )
            self.wifi_firmware_build_timestamp = resp.build

    # Too volatile to be saved
    def get_wifiinfo(self, callb=None):
        """Convenience method to request the wifi info from the device

        This will request the information from the device and request that callb be executed
        when a response is received. The is no  default callback

            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: None
            :rtype: None
        """
        response = self.req_with_resp(GetWifiInfo, StateWifiInfo, callb=callb)
        return None

    def get_hostfirmware(self, callb=None):
        """Convenience method to request the device firmware info from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: The cached value
        :rtype: str
        """
        if self.host_firmware_version is None:
            mypartial = partial(self.resp_set_hostfirmware)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            response = self.req_with_resp(
                GetHostFirmware, StateHostFirmware, callb=mycallb
            )

        return (self.host_firmware_version, self.host_firmware_build_timestamp)

    def resp_set_hostfirmware(self, resp):
        """Default callback for get_hostfirmware"""
        if resp:
            self.host_firmware_version = (
                str(resp.version >> 16) + "." + str(resp.version & 0xFFFF)
            )
            self.host_firmware_build_timestamp = resp.build

    # Too volatile to be saved
    def get_hostinfo(self, callb=None):
        """Convenience method to request the device info from the device

        This will request the information from the device and request that callb be executed
        when a response is received. The is no  default callback

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        response = self.req_with_resp(GetInfo, StateInfo, callb=callb)
        return None

    def set_reboot(self):
        """Convenience method to reboot the device

        This will send a magic reboot packet to the device which has the same effect
        as physically turning the device off and on again. Its uptime value will be
        reset and it will be rediscovered.

        There are no parameters or callbacks as the device immediately restarts with
        any response so it just returns True to indicate the packet was sent.
        """
        return self.fire_and_forget(SetReboot)

    def get_version(self, callb=None):
        """Convenience method to request the version from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: The cached value
            :rtype: str
        """
        if self.vendor is None:
            mypartial = partial(self.resp_set_version)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            response = self.req_with_resp(GetVersion, StateVersion, callb=mycallb)
        return (self.host_firmware_version, self.host_firmware_build_timestamp)

    def resp_set_version(self, resp):
        """Default callback for get_version"""
        if resp:
            self.vendor = resp.vendor
            self.product = resp.product
            self.version = resp.version

    #
    #                            Formating
    #
    def device_characteristics_str(self, indent):
        """Convenience to string method."""
        s = "{}\n".format(self.label)
        s += indent + "MAC Address: {}\n".format(self.mac_addr)
        s += indent + "IP Address: {}\n".format(self.ip_addr)
        s += indent + "Port: {}\n".format(self.port)
        s += indent + "Power: {}\n".format(str_map(self.power_level))
        s += indent + "Location: {}\n".format(self.location)
        s += indent + "Group: {}\n".format(self.group)
        return s

    def device_firmware_str(self, indent):
        """Convenience to string method."""
        host_build_ns = self.host_firmware_build_timestamp
        host_build_s = (
            datetime.datetime.utcfromtimestamp(host_build_ns / 1000000000)
            if host_build_ns != None
            else None
        )
        wifi_build_ns = self.wifi_firmware_build_timestamp
        wifi_build_s = (
            datetime.datetime.utcfromtimestamp(wifi_build_ns / 1000000000)
            if wifi_build_ns != None
            else None
        )
        s = "Host Firmware Build Timestamp: {} ({} UTC)\n".format(
            host_build_ns, host_build_s
        )
        s += indent + "Host Firmware Build Version: {}\n".format(
            self.host_firmware_version
        )
        s += indent + "Wifi Firmware Build Timestamp: {} ({} UTC)\n".format(
            wifi_build_ns, wifi_build_s
        )
        s += indent + "Wifi Firmware Build Version: {}\n".format(
            self.wifi_firmware_version
        )
        return s

    def device_product_str(self, indent):
        """Convenience to string method."""
        s = "Vendor: {}\n".format(self.vendor)
        s += indent + "Product: {}\n".format(
            (self.product and products_dict[self.product]) or "Unknown"
        )
        s += indent + "Version: {}\n".format(self.version)
        return s

    def device_time_str(self, resp, indent="  "):
        """Convenience to string method."""
        time = resp.time
        uptime = resp.uptime
        downtime = resp.downtime
        time_s = (
            datetime.datetime.utcfromtimestamp(time / 1000000000)
            if time != None
            else None
        )
        uptime_s = round(nanosec_to_hours(uptime), 2) if uptime != None else None
        downtime_s = round(nanosec_to_hours(downtime), 2) if downtime != None else None
        s = "Current Time: {} ({} UTC)\n".format(time, time_s)
        s += indent + "Uptime (ns): {} ({} hours)\n".format(uptime, uptime_s)
        s += indent + "Last Downtime Duration +/-5s (ns): {} ({} hours)\n".format(
            downtime, downtime_s
        )
        return s

    def device_radio_str(self, resp, indent="  "):
        """Convenience to string method."""
        signal = resp.signal
        tx = resp.tx
        rx = resp.rx
        s = "Wifi Signal Strength (mW): {}\n".format(signal)
        s += indent + "Wifi TX (bytes): {}\n".format(tx)
        s += indent + "Wifi RX (bytes): {}\n".format(rx)
        return s

    def register_callback(self, callb):
        """Method used to register a default call back to be called when data is received

        :param callb: The calllback to be executed.
        :type callb: callable

        """
        self.default_callb = callb


class Light(Device):
    """Connection to a given Lifx light device.

    :param loop: The asyncio loop being used
    :type loop: asyncio.AbstractEventLoop
    :param: mac_addr: The device MAC address aa:bb:cc:dd:ee:ff
    :type mac_addr: string
    :param ip_addr: The devie IP address (either IPv4 or IPv6)
    :type ip_addr: string
    :param port: The port used by the unicast connection
    :type port: into
    :param parent: Parent object with register/unregister methods
    :type parent: object
    :returns: an asyncio DatagramProtocol to handle communication with the device
    :rtype: DatagramProtocol
    """

    def __init__(self, loop, mac_addr, ip_addr, port=UDP_BROADCAST_PORT, parent=None):
        mac_addr = mac_addr.lower()
        super(Light, self).__init__(loop, mac_addr, ip_addr, port, parent)
        self.color = None
        self.color_zones = None
        self.zones_count = 1
        self.infrared_brightness = None
        self.hev_cycle = None
        self.hev_cycle_configuration = None
        self.last_hev_cycle_result = None
        self.effect = {"effect": None}
        # matrix devices: Tile, Candle, Path, Spot, Ceiling
        self.chain = {}
        self.chain_length = 0
        self.tile_devices = []
        self.tile_devices_count = 0
        self.tile_device_width = 0
        # Only used by a Lifx Switch. Will be populated with either True or False for each relay index if `get_rpower` called.
        # At the moment we assume the switch to be 4 relays. This will likely work with the 2 relays switch as well, but only the first two values
        # in this array will contain useful data.
        self.relays_power = [None, None, None, None]
        # Only used by a Lifx switch. Will be populated with an object containing the `haptic_duration_ms`, `backlight_on_color` and `backlight_off_color`
        self.button_config = None
        # Only used by a Lifx switch. Will be populated with an object containing `count`, `index`, `buttons_count` and `buttons`
        self.button = None

    def get_power(self, callb=None):
        """Convenience method to request the power status from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        if self.power_level is None:
            response = self.req_with_resp(LightGetPower, LightStatePower, callb=callb)
        return self.power_level

    def set_power(self, value, callb=None, duration=0, rapid=False):
        """Convenience method to set the power status of the device

        This method will send a SetPower message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param value: The new state
            :type value: str/bool/int
            :param duration: The duration, in seconds, of the power state transition.
            :type duration: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        on = [True, 1, "on"]
        off = [False, 0, "off"]
        if value in on:
            myvalue = MAX_UNSIGNED_16_BIT_INTEGER_VALUE
        else:
            myvalue = 0
        mypartial = partial(self.resp_set_lightpower, power_level=myvalue)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)
        if not rapid:
            response = self.req_with_ack(
                LightSetPower,
                {"power_level": myvalue, "duration": duration},
                callb=mycallb,
            )
        else:
            response = self.fire_and_forget(
                LightSetPower,
                {"power_level": myvalue, "duration": duration},
                num_repeats=1,
            )
            self.power_level = myvalue
            if callb:
                callb(self, None)

    # Here lightpower because LightStatePower message will give lightpower
    def resp_set_lightpower(self, resp, power_level=None):
        """Default callback for set_power"""
        if power_level is not None:
            self.power_level = power_level
        elif resp:
            self.power_level = resp.power_level

    # LightGet, color, power_level, label
    def get_color(self, callb=None):
        """Convenience method to request the colour status from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        response = self.req_with_resp(LightGet, LightState, callb=callb)
        return self.color

    # color is [Hue, Saturation, Brightness, Kelvin], duration in ms
    def set_color(self, value, callb=None, duration=0, rapid=False):
        """Convenience method to set the colour status of the device

        This method will send a LightSetColor message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param value: The new state, a dictionary onf int with 4 keys Hue, Saturation, Brightness, Kelvin
            :type value: dict
            :param duration: The duration, in seconds, of the power state transition.
            :type duration: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        if len(value) == 4:
            mypartial = partial(self.resp_set_light, color=value)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)
            # try:
            if rapid:
                self.fire_and_forget(
                    LightSetColor, {"color": value, "duration": duration}, num_repeats=1
                )
                self.resp_set_light(None, color=value)
                if callb:
                    callb(self, None)
            else:
                self.req_with_ack(
                    LightSetColor, {"color": value, "duration": duration}, callb=mycallb
                )
            # except WorkflowException as e:
            # print(e)

    # Here light because LightState message will give light
    def resp_set_light(self, resp, color=None):
        """Default callback for set_color"""
        if color:
            self.color = color
        elif resp:
            self.power_level = resp.power_level
            self.color = resp.color
            self.label = resp.label.decode().replace("\x00", "")

    # Multizone
    def get_color_zones(self, start_index, end_index=None, callb=None):
        """Convenience method to request the state of colour by zones from the device

        This method will request the information from the device and request that callb
        be executed when a response is received.

            :param start_index: Index of the start of the zone of interest
            :type start_index: int
            :param end_index: Index of the end of the zone of interest. By default start_index+7
            :type end_index: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :returns: None
            :rtype: None
        """
        if end_index is None:
            end_index = start_index + 7
        args = {
            "start_index": start_index,
            "end_index": end_index,
        }
        self.req_with_resp(
            MultiZoneGetColorZones, MultiZoneStateMultiZone, payload=args, callb=callb
        )

    def set_color_zones(
        self,
        start_index,
        end_index,
        color,
        duration=0,
        apply=1,
        callb=None,
        rapid=False,
    ):
        """Convenience method to set the colour status zone of the device

        This method will send a MultiZoneSetColorZones message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param start_index: Index of the start of the zone of interest
            :type start_index: int
            :param end_index: Index of the end of the zone of interest. By default start_index+7
            :type end_index: int
            :param apply: Indicates if the colour change is to be applied or memorized. Default: 1
            :type apply: int
            :param value: The new state, a dictionary onf int with 4 keys Hue, Saturation, Brightness, Kelvin
            :type value: dict
            :param duration: The duration, in seconds, of the power state transition.
            :type duration: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        if len(color) == 4:
            args = {
                "start_index": start_index,
                "end_index": end_index,
                "color": color,
                "duration": duration,
                "apply": apply,
            }

            mypartial = partial(self.resp_set_multizonemultizone, args=args)
            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)

            if rapid:
                self.fire_and_forget(MultiZoneSetColorZones, args, num_repeats=1)
                mycallb(self, None)
            else:
                self.req_with_ack(MultiZoneSetColorZones, args, callb=mycallb)

    # A multi-zone MultiZoneGetColorZones returns MultiZoneStateMultiZone -> multizonemultizone
    def resp_set_multizonemultizone(self, resp, args=None):
        """Default callback for get-color_zones/set_color_zones"""
        if args:
            if self.color_zones:
                for i in range(args["start_index"], args["end_index"] + 1):
                    self.color_zones[i] = args["color"]
        elif resp:
            if self.color_zones is None:
                self.color_zones = [None] * resp.count
            try:
                for i in range(resp.index, min(resp.index + 8, resp.count)):
                    if i > len(self.color_zones) - 1:
                        self.color_zones += [resp.color[i - resp.index]] * (
                            i - len(self.color_zones)
                        )
                        self.color_zones.append(resp.color[i - resp.index])
                    else:
                        self.color_zones[i] = resp.color[i - resp.index]
            except:
                # I guess this should not happen but...
                pass

    def get_multizone_effect(self, callb=None):
        """Convenience method to get the currently running firmware effect on the device.

        The value returned is the previously known state of the device. Use a callback
        to get the current state of the device.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_multizonemultizoneeffect will be used.
        :type callb: callable
        :returns: current effect details as a dictionary
        :rtype: dict
        """
        response = self.req_with_resp(
            MultiZoneGetMultiZoneEffect, MultiZoneStateMultiZoneEffect, callb=callb
        )
        return self.effect

    def set_multizone_effect(
        self, effect=0, speed=3, direction=0, callb=None, rapid=False
    ):
        """Convenience method to start or stop the Move firmware effect on multizone devices.

        Compatible devices include LIFX Z, Lightstrip and Beam and can be identified by
        checking if products_dict[device.product].multizone is True. Multizone devices
        only have one firmware effect named "MOVE". The effect can be started and stopped
        without the device being powered on. The effect will not be visible if the
        device is a single uniform color.

        Sending a set_power(0) to the device while the effect is running does not stop the effect.
        Physically powering off the device will stop the effect. And the device.


        :param effect: 0/Off, 1/Move
        :type effect: int
        :param speed: time in seconds for one cycle of the effect to travel the length of the device
        :type speed: float
        :param direction: 0/Right, 1/Left
        :type direction: int
        """

        typ = effect
        if type(effect) == str:
            typ = MultiZoneEffectType[effect.upper()].value
        elif type(effect) == int:
            typ = effect if effect in [e.value for e in MultiZoneEffectType] else 0

        speed = floor(speed * 1000) if 0 < speed <= 60 else 3000

        if type(direction) == str:
            direction = MultiZoneDirection[direction.upper()].value
        elif type(direction) == int:
            direction = (
                direction if direction in [d.value for d in MultiZoneDirection] else 0
            )

        payload = {
            "type": typ,
            "speed": speed,
            "duration": 0,
            "direction": direction,
        }

        if rapid:
            self.fire_and_forget(MultiZoneSetMultiZoneEffect, payload)
        else:
            self.req_with_ack(MultiZoneSetMultiZoneEffect, payload, callb=callb)

    def resp_set_multizonemultizoneeffect(self, resp):
        """Default callback for get_multizone_effect"""

        if resp:
            self.effect = {"effect": MultiZoneEffectType(resp.effect).name.upper()}

            if resp.effect != 0:
                self.effect["speed"] = resp.speed / 1000
                self.effect["duration"] = (
                    0.0
                    if resp.duration == 0
                    else float(f"{self.effect['duration'] / 1000000000:4f}")
                )
                self.effect["direction"] = MultiZoneDirection(
                    resp.direction
                ).name.capitalize()

    def get_extended_color_zones(self, callb=None):
        """
        Convenience method to request the state of all zones of a multizone device
        in a single request.

        The device must have the extended_multizone feature to use this method.

        This method will request the information from the device and request that callb
        be executed when a response is received.

        :param callb: Callable to be used when the response is received. If not set,
                    self.resp_set_multizonemultizoneextendedcolorzones will be used.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        self.req_with_resp(
            MultiZoneGetExtendedColorZones,
            MultiZoneStateExtendedColorZones,
            callb=callb,
        )

    def set_extended_color_zones(
        self,
        colors,
        colors_count,
        zone_index=0,
        duration=0,
        apply=1,
        callb=None,
        rapid=False,
    ):
        """
        Convenience method to set the state of all zones on a multizone device in
        a single request.

        The device must have the extended_multizone feature to use this method.
        There must be 82 color tuples in the colors list regardless of how many
        zones the device has. Use the colors_count parameter to specify the number
        of colors from the colors list that should be applied to the device and
        use the zone_index parameter to specify the starting zone.

        :param colors List of color dictionaries with HSBK keys
        :type colors List[dict[str, int]]
        :param colors_count How many color values in the color list to apply to the device
        :type colors_count int
        :param zone_index Which zone to start applying the colors from (default 0)
        :type zone_index int
        :param duration duration in seconds to apply the colors (default 0)
        :type duration int
        :param apply whether to apply the colors or buffer the new value (default 1 or apply)
        :type apply int
        :param callb Callback function to invoke when the response is received
        :type callb Callable
        :returns None
        :rtype None
        """
        if len(colors) == 82:
            args = {
                "duration": duration,
                "apply": apply,
                "zone_index": zone_index,
                "colors_count": colors_count,
                "colors": colors,
            }
            mypartial = partial(self.resp_set_multizoneextendedcolorzones, args=args)

            if callb:
                mycallb = lambda x, y: (mypartial(y), callb(x, y))
            else:
                mycallb = lambda x, y: mypartial(y)

            if rapid:
                self.fire_and_forget(
                    MultiZoneSetExtendedColorZones, args, num_repeats=1
                )
                mycallb(self, None)
            else:
                self.req_with_ack(MultiZoneSetExtendedColorZones, args, callb=mycallb)

    def resp_set_multizoneextendedcolorzones(self, resp, args=None):
        """Default callback for get_extended_color_zones"""
        if args:
            if self.color_zones:
                for i in range(args["zone_index"], args["colors_count"]):
                    self.color_zones[i] = args["colors"][i]

        elif resp:
            self.zones_count = resp.zones_count
            self.color_zones = resp.colors[resp.zone_index : resp.colors_count]

    # value should be a dictionary with the the following keys: transient, color, period, cycles, skew_ratio, waveform
    def set_waveform(self, value, callb=None, rapid=False):
        """Convenience method to animate the light, a dictionary with the the following
        keys: transient, color, period, cycles, skew_ratio, waveform

        This method will send a SetPower message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param value: The animation parameter.
            :type value:
            :param duration: The duration, in seconds, of the power state transition.
            :type duration: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        if "color" in value and len(value["color"]) == 4:
            if rapid:
                self.fire_and_forget(LightSetWaveform, value, num_repeats=1)
            else:
                self.req_with_ack(LightSetWaveform, value, callb=callb)

    # value should be a dictionary with the the following keys:
    # transient, color, period, cycles, skew_ratio, waveform, set_hue, set_saturation, set_brightness, set_kelvin
    def set_waveform_optional(self, value, callb=None, rapid=False):
        """Convenience method to animate the light, a dictionary with the the following
        keys: transient, color, period, cycles, skew_ratio, waveform, set_hue, set_saturation, set_brightness, set_kelvin

        This method will send a SetPower message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param value: The animation parameter.
            :type value:
            :param duration: The duration, in seconds, of the power state transition.
            :type duration: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        if "color" in value and len(value["color"]) == 4:
            if rapid:
                self.fire_and_forget(LightSetWaveformOptional, value, num_repeats=1)
            else:
                self.req_with_ack(LightSetWaveformOptional, value, callb=callb)

    # Infrared get maximum brightness, infrared_brightness
    def get_infrared(self, callb=None):
        """Convenience method to request the infrared brightness from the device

        This method will check whether the value has already been retrieved from the device,
        if so, it will simply return it. If no, it will request the information from the device
        and request that callb be executed when a response is received. The default callback
        will simply cache the value.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        response = self.req_with_resp(LightGetInfrared, LightStateInfrared, callb=callb)
        return self.infrared_brightness

    # Infrared set maximum brightness, infrared_brightness
    def set_infrared(self, infrared_brightness, callb=None, rapid=False):
        """Convenience method to set the infrared status of the device

        This method will send a SetPower message to the device, and request callb be executed
        when an ACK is received. The default callback will simply cache the value.

            :param infrared_brightness: The new state
            :type infrared_brightness: int
            :param duration: The duration, in seconds, of the power state transition.
            :type duration: int
            :param callb: Callable to be used when the response is received. If not set,
                        self.resp_set_label will be used.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """
        mypartial = partial(
            self.resp_set_lightinfrared, infrared_brightness=infrared_brightness
        )
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)
        if rapid:
            self.fire_and_forget(
                LightSetInfrared,
                {"infrared_brightness": infrared_brightness},
                num_repeats=1,
            )
            self.resp_set_lightinfrared(None, infrared_brightness=infrared_brightness)
            if callb:
                callb(self, None)
        else:
            self.req_with_ack(
                LightSetInfrared,
                {"infrared_brightness": infrared_brightness},
                callb=mycallb,
            )

    # Here lightinfrared because LightStateInfrared message will give lightinfrared
    def resp_set_lightinfrared(self, resp, infrared_brightness=None):
        """Default callback for set_infrared/get_infrared"""
        if infrared_brightness is not None:
            self.infrared_brightness = infrared_brightness
        elif resp:
            self.infrared_brightness = resp.infrared_brightness

    def get_hev_cycle(self, callb=None):
        """Request the state of any running HEV cycle of the device.

        This method only works with LIFX Clean bulbs.

        This method will do nothing unless a call back is passed to it.

        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        if products_dict[self.product].hev is True:
            self.req_with_resp(GetHevCycle, StateHevCycle, callb=callb)

    def resp_set_hevcycle(self, resp):
        """Default callback for get_hev_cycle/set_hev_cycle"""
        if resp:
            self.hev_cycle = {
                "duration": resp.duration,
                "remaining": resp.remaining,
                "last_power": resp.last_power,
            }

    def set_hev_cycle(self, enable=True, duration=0, callb=None, rapid=False):
        """Immediately starts a HEV cycle on the device.

        This method only works with LIFX Clean bulbs.

        This method will send a SetHevCycle message to the device, and request
        callb be executed when an ACK is received.

        :param enable: If True, start the HEV cycle, otherwise abort.
        :type enable: bool
        :param duration: The duration, in seconds, of the HEV cycle. If 0,
                         use the default configuration on the bulb.
        :type duration: int
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :param rapid: Whether to ask for ack (False) or not (True). Default False
        :type rapid: bool
        :returns: None
        :rtype: None
        """
        if products_dict[self.product].hev is True:
            if rapid:
                self.fire_and_forget(
                    SetHevCycle,
                    {"enable": int(enable), "duration": duration},
                    num_repeats=1,
                )
                if callb:
                    callb(self, None)
            else:
                self.req_with_resp(
                    SetHevCycle,
                    StateHevCycle,
                    {"enable": int(enable), "duration": duration},
                    callb=callb,
                )

    def get_hev_configuration(self, callb=None):
        """Requests the default HEV configuration of the device.

        This method only works with LIFX Clean bulbs.

        This method will do nothing unless a call back is passed to it.

        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        if products_dict[self.product].hev is True:
            self.req_with_resp(
                GetHevCycleConfiguration, StateHevCycleConfiguration, callb=callb
            )

    def resp_set_hevcycleconfiguration(self, resp):
        """Default callback for get_hev_cycle_configuration/set_hev_cycle_configuration"""
        if resp:
            self.hev_cycle_configuration = {
                "duration": resp.duration,
                "indication": resp.indication,
            }

    def set_hev_configuration(self, indication, duration, callb=None, rapid=False):
        """Sets the default HEV configuration of the device.

        This method only works with LIFX Clean bulbs.

        This method will send a SetHevCycleConfiguration message to the device,
        and request callb be executed when an ACK is received.

        :param indication: If True, show a short flashing indication when the
                           HEV cycle finishes.
        :type indication: bool
        :param duration: The duration, in seconds, of the HEV cycle.
        :type duration: int
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :param rapid: Whether to ask for ack (False) or not (True). Default False
        :type rapid: bool
        :returns: None
        :rtype: None
        """
        if products_dict[self.product].hev is True:
            if rapid:
                self.fire_and_forget(
                    SetHevCycleConfiguration,
                    {"indication": int(indication), "duration": duration},
                    num_repeats=1,
                )
                if callb:
                    callb(self, None)
            else:
                self.req_with_resp(
                    SetHevCycleConfiguration,
                    StateHevCycleConfiguration,
                    {"indication": int(indication), "duration": duration},
                    callb=callb,
                )

    # Get last HEV cycle result
    def get_last_hev_cycle_result(self, callb=None):
        """Requests the result of the last HEV cycle of the device.

        This method only works with LIFX Clean bulbs.

        This method will do nothing unless a call back is passed to it.

        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        if products_dict[self.product].hev is True:
            self.req_with_resp(
                GetLastHevCycleResult, StateLastHevCycleResult, callb=callb
            )

    def resp_set_lasthevcycleresult(self, resp):
        if resp:
            self.last_hev_cycle_result = LAST_HEV_CYCLE_RESULT.get(resp.result)

    def get_device_chain(self, callb=None):
        """Convenience method to get the devices on a matrix chain.

        This method only works on LIFX matrix devices which include the Tile,
        Candle, Path, Spot and Ceiling.

        The LIFX protocol definition uses the terms tile and chain, even
        though the actual Tile product has been discontinued and is/was the
        only one to have more than one tile on the chain.

        This method populates the tile_devices, tile_devices_count and
        tile_device_width attributes of the corresponding Light object.

        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        if products_dict[self.product].matrix is True:
            self.req_with_resp(TileGetDeviceChain, TileStateDeviceChain, callb=callb)

    def resp_set_tiledevicechain(self, resp):
        if resp:
            self.tile_devices = [tile_device for tile_device in resp.tile_devices]
            self.tile_devices_count = resp.tile_devices_count
            self.tile_device_width = self.tile_devices[0]["width"]

    def get64(self, tile_index=0, length=1, width=None, callb=None):
        """Convenience method to get the state of zones on tiles in a chain.

        This method populates returns the state of at least one but up to
        five tiles worth of zones, with up to 64 zones per tile. This is stored
        in the chain attribute of the Light which is an array that has the
        tile_index as the key and a list of 64 HSBK tuples as the value.

        :param tile_index: starting tile on the target chain
        :type tile_index: int
        :param length: how many tiles to target including the starting tile
        :type length: int
        :param width: how many zones per row on the target tile
        :type width: int
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :rtype: None
        """
        if width is None:
            if self.tile_device_width == 0:
                return
            width = self.tile_device_width

        if products_dict[self.product].chain is True:
            length = 5

        for i in range(tile_index, length):
            args = {
                "tile_index": i,
                "length": 1,
                "x": 0,
                "y": 0,
                "width": width,
            }

            self.req_with_resp(
                msg_type=TileGet64, response_type=TileState64, payload=args, callb=callb
            )

    def resp_set_tile64(self, resp):
        if resp and isinstance(resp, TileState64):
            self.chain[resp.tile_index] = resp.colors
            self.chain_length = len(self.chain)

    def set64(
        self, tile_index=0, x=0, y=0, width=None, duration=0, colors=None, callb=None
    ):
        """Convenience method to set 64 colors on a tile.

        You can either provide the width of the target tile or
        use the get_device_chain method to retrieve the value
        from the target light. If the width is not provided,
        this method will return without sending a packet.

        The x and y parameters specify the row and column
        starting point from which to change the zones and
        the amount of colors provided will determine how many zones
        are changed.

        To change all zones to the same color, use the set_color
        method.

        Note this method does not return a response even if requested.

        :param tile_index: the starting tile in a chain to target
        :type tile_index: int
        :param x: the starting column to target on the target tile
        :type x: int
        :param y: the starting row to target on the target tile
        :type y: int
        :param width: how many zones per row on the target tile
        :type width: int
        :param duration: how long in seconds to transition to the new colors
        :type duration: int
        :param colors: up to 64 color tuples to apply to the target zones
        :type colors: list[tuple[int, float, float, int]]
        :rtype: None
        """

        if width is None:
            if self.tile_device_width == 0:
                return
            width = self.tile_device_width

        if len(colors) < 64:
            for _ in range(64 - len(colors)):
                colors.append((0, 0, 0, 3500))

        if len(colors) > 64:
            colors = colors[:64]

        payload = {
            "tile_index": tile_index,
            "length": 1,
            "x": x,
            "y": y,
            "width": width,
            "duration": duration * 1000,
            "colors": colors,
        }

        self.fire_and_forget(TileSet64, payload)

    def get_tile_effect(self, callb=None):
        """Convenience method to get the currently running effect on a Tile or Candle.

        The value returned is the previously known state of the effect. Use a callback
        to get the actual current state.

        :param callb: callable to be used when a response is received. If not set,
                      self.resp_set_tileeffect will be used.
        :type callb: callable
        :returns: current effect details as a dictionary
        :rtype: dict
        """
        response = self.req_with_resp(
            TileGetTileEffect, TileStateTileEffect, callb=callb
        )
        return self.effect

    def set_tile_effect(
        self,
        effect=0,
        speed=None,
        sky_type=None,
        cloud_saturation_min=None,
        cloud_saturation_max=None,
        palette=[],
        callb=None,
        rapid=False,
    ):
        """Convenience method to start or stop a firmware effect on matrix devices.

        A palette of up to 16 HSBK tuples can be provided for the MORPH effect, otherwise
        it will use the same Exciting theme used by the LIFX smart phone app.

        :param effect: 0/Off, 2/Morph, 3/Flame, 5/Sky (LIFX Ceiling only)
        :type effect: int/str
        :param speed: time in seconds for one cycle of the effect to travel the length of the device
        :type speed: int
        :param sky_type: only used by Sky effect on LIFX ceiling
        :type sky_type: int/str
        :param cloud_saturation_min: only used by Sky effect on LIFX ceiling
        :type cloud_saturation_min: int
        :param cloud_saturation_max: only used by Sky effect on LIFX ceiling
        :type cloud_saturation_max: int
        :param palette: a list of up to 16 HSBK tuples to use for the Morph effect
        :type palette: list[tuple(hue, saturation, brightness, kelvin)]
        :param callb: a callback to use when the response is received
        :type callb: callable
        :param rapid: whether to request an acknowledgement or not
        :type rapid: bool
        :returns: None
        :rtype: None
        """

        # Exciting theme
        default_morph_palette = [
            (0, 65535, 65535, 3500),
            (7282, 65535, 65535, 3500),
            (10923, 65535, 65535, 3500),
            (22209, 65535, 65535, 3500),
            (43509, 65535, 65535, 3500),
            (49334, 65535, 65535, 3500),
            (53521, 65535, 65535, 3500),
        ]

        typ = effect
        if type(effect) == str:
            typ = TileEffectType[effect.upper()].value
        elif type(effect) == int:
            typ = effect if effect in [e.value for e in TileEffectType] else 0

        if typ is TileEffectType.SKY.value:
            speed = floor(speed * 1000) if speed is not None else 50000

            if sky_type is None:
                sky_type = TileEffectSkyType.CLOUDS.value
            elif type(sky_type) == str:
                sky_type = TileEffectSkyType[sky_type.upper()].value
            elif type(sky_type) == int:
                sky_type = (
                    sky_type if sky_type in [e.value for e in TileEffectSkyType] else 2
                )

            if cloud_saturation_min is None:
                cloud_saturation_min = 50
            if cloud_saturation_max is None:
                cloud_saturation_max = 180

        else:
            sky_type = 0
            cloud_saturation_min = 0
            cloud_saturation_max = 0

            if speed is None:
                speed = 3
            if len(palette) == 0 and typ is TileEffectType.MORPH.value:
                palette = default_morph_palette
            if len(palette) > 16:
                palette = palette[:16]

            speed = floor(speed * 1000) if 0 < speed <= 60 else 3000

        palette_count = len(palette)
        payload = {
            "type": typ,
            "speed": speed,
            "duration": 0,
            "sky_type": sky_type,
            "cloud_saturation_min": cloud_saturation_min,
            "cloud_saturation_max": cloud_saturation_max,
            "palette_count": palette_count,
            "palette": palette,
        }

        if rapid:
            self.fire_and_forget(TileSetTileEffect, payload)
        else:
            self.req_with_ack(TileSetTileEffect, payload, callb=callb)

    def resp_set_tiletileeffect(self, resp):
        """Default callback for get_tile_effect and set_tile_effect"""
        if resp:
            self.effect = {"effect": TileEffectType(resp.effect).name.upper()}

            if resp.effect != 0:
                self.effect["speed"] = resp.speed / 1000
                self.effect["duration"] = (
                    0.0
                    if resp.duration == 0
                    else float(f"{resp.duration/1000000000:4f}")
                )
                if resp.effect == TileEffectType.SKY.value:
                    self.effect["sky_type"] = TileEffectSkyType(
                        resp.sky_type
                    ).name.upper()
                    self.effect["cloud_saturation_min"] = resp.cloud_saturation_min
                    self.effect["cloud_saturation_max"] = resp.cloud_saturation_max

                self.effect["palette_count"] = resp.palette_count
                self.effect["palette"] = resp.palette

    def get_rpower(self, relay_index=None, callb=None):
        """Method will get the power state of all relays; or a single relay if value provided.

        :param relay_index: The index of the relay to check power state for. If not provided, will loop through 4 relays
        :type relay_index: int
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        mypartial = partial(self.resp_set_rpower)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)

        if relay_index is not None:
            payload = {"relay_index": relay_index}
            response = self.req_with_resp(
                GetRPower, StateRPower, payload, callb=mycallb
            )
        else:
            for relay_index in range(4):
                payload = {"relay_index": relay_index}
                response = self.req_with_resp(
                    GetRPower, StateRPower, payload, callb=mycallb
                )
        return self.relays_power

    def set_rpower(self, relay_index, is_on, callb=None, rapid=False):
        """Sets relay power for a given relay index

        :param relay_index: The relay on the switch starting from 0.
        :type relay_index: int
        :param on: Whether the relay is on or not
        :type is_on: bool
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :param rapid: Whether to ask for ack (False) or not (True). Default False
        :type rapid: bool
        :returns: None
        :rtype: None
        """
        level = 0
        if is_on:
            level = MAX_UNSIGNED_16_BIT_INTEGER_VALUE

        payload = {"relay_index": relay_index, "level": level}
        mypartial = partial(self.resp_set_rpower, relay_index=relay_index, level=level)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)

        if not rapid:
            self.req_with_resp(SetRPower, StateRPower, payload, callb=mycallb)
        else:
            self.fire_and_forget(SetRPower, payload)

    def resp_set_rpower(self, resp, relay_index=None, level=None):
        """Default callback for get_rpower/set_rpower"""
        if relay_index != None and level != None:
            self.relays_power[relay_index] = level == MAX_UNSIGNED_16_BIT_INTEGER_VALUE
        elif resp:
            # Current models of the LIFX switch do not have dimming capability, so the two valid values are 0 for off (False) and 65535 for on (True).
            self.relays_power[resp.relay_index] = (
                resp.level == MAX_UNSIGNED_16_BIT_INTEGER_VALUE
            )

    def get_button(self, callb=None):
        """Method will get the state of all buttons

        :param relay_index: The index of the relay to check power state for. If not provided, will loop through 4 relays
        :type relay_index: int
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        mypartial = partial(self.resp_get_button)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)

        payload = {}
        response = self.req_with_resp(GetButton, StateButton, payload, callb=mycallb)

    def set_button(self, callb=None, rapid=False):
        raise Exception(
            "SetButton isn't yet implemented as you can only set button actions to the same values as the LIFX app (ie you can't add custom callbacks), making it not that useful. Feel free to implement if you need this :)"
        )
        """ Sets button

            :param callb: Callable to be used when the response is received.
            :type callb: callable
            :param rapid: Whether to ask for ack (False) or not (True). Default False
            :type rapid: bool
            :returns: None
            :rtype: None
        """

        payload = {}
        mypartial = partial(self.resp_get_button)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)

        if not rapid:
            self.req_with_resp(SetButton, StateButton, payload, callb=mycallb)
        else:
            self.fire_and_forget(SetButton, payload)

    def resp_get_button(self, resp):
        """Default callback for get_button/set_button"""
        self.button = {
            "count": resp.count,
            "index": resp.index,
            "buttons_count": resp.buttons_count,
            "buttons": resp.buttons,
        }

    def get_button_config(self, callb=None):
        """Method will get the button config

        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :returns: The cached value
        :rtype: int
        """
        mypartial = partial(self.resp_get_button_config)
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)

        response = self.req_with_resp(
            GetButtonConfig, StateButtonConfig, {}, callb=mycallb
        )

    def set_button_config(
        self,
        haptic_duration_ms: int,
        backlight_on_color,
        backlight_off_color,
        callb=None,
        rapid=False,
    ):
        """Sets button config

        :param haptic_duration_ms: How many milliseconds the haptic vibration when the button is pressed should last
        :type haptic_duration_ms: int
        :param backlight_on_color: The color the backlight should be when a button is on
        :type backlight_on_color: { "hue": int, "saturation": int, "brightness": int, "kelvin": int }
        :param backlight_off_color: The color the backlight should be when a button is off
        :type backlight_off_color: { "hue": int, "saturation": int, "brightness": int, "kelvin": int }
        :param callb: Callable to be used when the response is received.
        :type callb: callable
        :param rapid: Whether to ask for ack (False) or not (True). Default False
        :type rapid: bool
        :returns: None
        :rtype: None
        """

        payload = {
            "haptic_duration_ms": haptic_duration_ms,
            "backlight_on_color": backlight_on_color,
            "backlight_off_color": backlight_off_color,
        }
        mypartial = partial(
            self.resp_get_button_config,
            haptic_duration_ms=haptic_duration_ms,
            backlight_on_color=backlight_on_color,
            backlight_off_color=backlight_off_color,
        )
        if callb:
            mycallb = lambda x, y: (mypartial(y), callb(x, y))
        else:
            mycallb = lambda x, y: mypartial(y)

        if not rapid:
            self.req_with_resp(
                SetButtonConfig, StateButtonConfig, payload, callb=mycallb
            )
        else:
            self.fire_and_forget(SetButtonConfig, payload)

    def resp_get_button_config(
        self,
        resp,
        haptic_duration_ms=None,
        backlight_on_color=None,
        backlight_off_color=None,
    ):
        """Default callback for get_button_config/set_button_config"""
        if (
            haptic_duration_ms != None
            and backlight_on_color != None
            and backlight_off_color != None
        ):
            self.button_config = {
                "haptic_duration_ms": haptic_duration_ms,
                "backlight_on_color": backlight_on_color,
                "backlight_off_color": backlight_off_color,
            }
        elif resp:
            self.button_config = {
                "haptic_duration_ms": resp.haptic_duration_ms,
                "backlight_on_color": resp.backlight_on_color,
                "backlight_off_color": resp.backlight_off_color,
            }

    def get_accesspoint(self, callb=None):
        """Convenience method to request the access point available

        This method will do nothing unless a call back is passed to it.

        :param callb: Callable to be used when the response is received. If not set,
                      self.resp_set_label will be used.
        :type callb: callable
        :returns: None
        :rtype: None
        """
        response = self.req_with_resp(GetAccessPoint, StateAccessPoint, callb=callb)
        return None

    def __str__(self):
        indent = "  "
        s = self.device_characteristics_str(indent)
        s += indent + "Color (HSBK): {}\n".format(self.color)
        s += indent + self.device_firmware_str(indent)
        s += indent + self.device_product_str(indent)
        # s += indent + self.device_time_str(indent)
        # s += indent + self.device_radio_str(indent)
        return s


class LifxDiscovery(aio.DatagramProtocol):
    """UDP broadcast discovery for  Lifx device.

    The discovery object will bradcast a discovery message every discovery_interval second. Sometimes it
    may be necessary to speed up this process. So discovery uses self.discovery_countdown, initially
    set to discovery_interval. It will then sleep for discovery_step seconds and decrease discovery_countdown
    by that amount. When discovery_countdown is <= 0, discovery is triggered. To hasten the process, one can set
    discovery_countdown = 0.

        :param parent: Parent object to register/unregister discovered device
        :type parent: object
        :param loop: The asyncio loop being used
        :type loop: asyncio.AbstractEventLoop
        :param: ipv6prefix: ipv6 network prefix to use
        :type mipv6prefix: string
        :param discovery_interval: How often, in seconds, to broadcast a discovery messages
        :type discovery_interval: int
        :param discovery_step: How often, in seconds, will the discovery process check if it is time to broadcast
        :type discovery_step: int
        :returns: an asyncio DatagramProtocol to handle communication with the device
        :rtype: DatagramProtocol
    """

    def __init__(
        self,
        loop,
        parent=None,
        ipv6prefix=None,
        discovery_interval=DISCOVERY_INTERVAL,
        discovery_step=DISCOVERY_STEP,
        broadcast_ip=UDP_BROADCAST_IP,
    ):
        self.lights = {}  # Known devices indexed by mac addresses
        self.parent = parent  # Where to register new devices
        self.transport = None
        self.loop = loop
        self.task = None
        self.source_id = random.randint(0, (2**32) - 1)
        self.ipv6prefix = ipv6prefix
        self.discovery_interval = discovery_interval
        self.discovery_step = discovery_step
        self.discovery_countdown = 0
        self.broadcast_ip = broadcast_ip

    def start(self, listen_ip=LISTEN_IP, listen_port=0):
        """Start discovery task."""
        coro = self.loop.create_datagram_endpoint(
            lambda: self, local_addr=(listen_ip, listen_port)
        )

        self.task = aio.create_task(coro)
        return self.task

    def connection_made(self, transport):
        """Method run when the UDP broadcast server is started"""
        # print('started')
        self.transport = transport
        sock = self.transport.get_extra_info("socket")
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.loop.call_soon(self.discover)

    def datagram_received(self, data, addr):
        """Method run when data is received from the devices

        This method will unpack the data according to the LIFX protocol.
        If a new device is found, the Light device will be created and started aa
        a DatagramProtocol and will be registered with the parent.

            :param data: raw data
            :type data: bytestring
            :param addr: sender IP address 2-tuple for IPv4, 4-tuple for IPv6
            :type addr: tuple
        """
        response = unpack_lifx_message(data)
        response.ip_addr = addr[0]

        mac_addr = response.target_addr
        if mac_addr == BROADCAST_MAC:
            return

        if (
            type(response) == StateService and response.service == 1
        ):  # only look for UDP services
            # discovered
            remote_port = response.port
        elif type(response) == LightState:
            # looks like the lights are volunteering LigthState after booting
            remote_port = UDP_BROADCAST_PORT
        else:
            return

        if self.ipv6prefix:
            family = socket.AF_INET6
            remote_ip = mac_to_ipv6_linklocal(mac_addr, self.ipv6prefix)
        else:
            family = socket.AF_INET
            remote_ip = response.ip_addr

        if mac_addr in self.lights:
            # rediscovered
            light = self.lights[mac_addr]

            # nothing to do
            if light.registered:
                return

            light.cleanup()
            light.ip_addr = remote_ip
            light.port = remote_port
        else:
            # newly discovered
            light = Light(self.loop, mac_addr, remote_ip, remote_port, parent=self)
            self.lights[mac_addr] = light

        coro = self.loop.create_datagram_endpoint(
            lambda: light, family=family, remote_addr=(remote_ip, remote_port)
        )

        light.task = aio.create_task(coro)

    def discover(self):
        """Method to send a discovery message"""
        if self.transport:
            if self.discovery_countdown <= 0:
                self.discovery_countdown = self.discovery_interval
                msg = GetService(
                    BROADCAST_MAC,
                    self.source_id,
                    seq_num=0,
                    payload={},
                    ack_requested=False,
                    response_requested=True,
                )
                self.transport.sendto(
                    msg.generate_packed_message(),
                    (self.broadcast_ip, UDP_BROADCAST_PORT),
                )
            else:
                self.discovery_countdown -= self.discovery_step
            self.loop.call_later(self.discovery_step, self.discover)

    def register(self, alight):
        """Proxy method to register the device with the parent."""
        if self.parent:
            self.parent.register(alight)

    def unregister(self, alight):
        """Proxy method to unregister the device with the parent."""
        if self.parent:
            self.parent.unregister(alight)

    def cleanup(self):
        """Method to call to cleanly terminate the connection to the device."""
        if self.transport:
            self.transport.close()
            self.transport = None
        if self.task:
            self.task.cancel()
            self.task = None
        for light in self.lights.values():
            light.cleanup()
        self.lights = {}


class LifxScan:
    """Scan all network interfaces for any active bulb."""

    def __init__(self, loop):
        """Initialize the scanner."""
        self.loop = loop

    async def scan(self, timeout=1):
        """Return a list of local IP addresses on interfaces with LIFX bulbs."""
        adapters = await self.loop.run_in_executor(None, ifaddr.get_adapters)
        ips = [
            ip.ip
            for adapter in ifaddr.get_adapters()
            for ip in adapter.ips
            if ip.is_IPv4
        ]

        if not ips:
            return []

        tasks = []
        discoveries = []
        for ip in ips:
            manager = ScanManager(ip)
            lifx_discovery = LifxDiscovery(self.loop, manager)
            discoveries.append(lifx_discovery)
            lifx_discovery.start(listen_ip=ip)
            tasks.append(aio.create_task(manager.lifx_ip()))

        (done, pending) = await aio.wait(tasks, timeout=timeout)

        for discovery in discoveries:
            discovery.cleanup()

        for task in pending:
            task.cancel()

        return [task.result() for task in done]


class ScanManager:
    """Temporary manager for discovering any bulb."""

    def __init__(self, ip):
        """Initialize the manager."""
        self._event = aio.Event()
        self.ip = ip

    async def lifx_ip(self):
        """Return our IP address when any device is discovered."""
        await self._event.wait()
        return self.ip

    def register(self, bulb):
        """Handle detected bulb."""
        self._event.set()

    def unregister(self, bulb):
        """Handle disappearing bulb."""
        pass