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
|
import socket
from typing import List
from airtouch4pyapi import helper
from airtouch4pyapi import packetmap
from airtouch4pyapi import communicate
from enum import Enum
# from hexdump import hexdump
#API
# class Airtouch
#Init - takes IP Address, queries group names and group infos
#GetInfo - takes nothing, returns Groups list
#SetGroupToTemperature takes group number + temperature
#TurnGroupOn
#TurnGroupOff
#SetCoolingModeByGroup
#SetFanSpeedByGroup
#GetSupportedCoolingModesByGroup
#GetSupportedFanSpeedsByGroup
#TurnAcOn
#TurnAcOff
#SetFanSpeedForAc
#SetCoolingModeForAc
#GetSupportedCoolingModesForAc
#GetSupportedFanSpeedsForAc
#GetAcs
class AirTouchStatus(Enum):
NOT_CONNECTED = 0
OK = 1
CONNECTION_INTERRUPTED = 2
CONNECTION_LOST = 3
ERROR = 4
class AirTouchVersion(Enum):
AIRTOUCH4 = 4
AIRTOUCH5 = 5
class AirTouchGroup:
def __init__(self):
self.GroupName = ""
self.GroupNumber = 0
self.OpenPercent = 0
self.Temperature = 0
self.TargetSetpoint = 0
self.BelongsToAc = -1
@property
def IsOn(self):
return self.PowerState
class AirTouchError:
def __init__(self):
self.Message = ""
self.Status = AirTouchStatus.OK
class AirTouchAc:
def __init__(self):
self.AcName = ""
self.AcNumber = 0
self.StartGroupNumber = 0
self.GroupCount = 0
@property
def IsOn(self):
return self.PowerState
class AirTouch:
IpAddress = "";
SettingValueTranslator = packetmap.SettingValueTranslator();
def __init__(self, ipAddress, atVersion = None, port = None):
self.IpAddress = ipAddress;
self.Status = AirTouchStatus.NOT_CONNECTED;
self.Messages = dict();
self.atVersion = atVersion;
self.atPort = port;
self.acs = dict();
self.groups = dict();
self.Messages:List[AirTouchError] = [];
async def UpdateInfo(self):
if(self.atPort != None and self.atVersion == None):
self.Status = AirTouchStatus.ERROR
errorMessage = AirTouchError()
errorMessage.Message = "If you specify a port, you must specify a version"
self.Messages.append(errorMessage)
print(self.Status)
for msg in self.Messages:
print(msg.Message);
return;
if(self.atVersion == None):
await self.findVersion()
if(self.atVersion == None):
print(self.Status)
for msg in self.Messages:
print(msg.Message);
return;
#get the group infos
await self.UpdateGroupInfo()
#if the first call means we still have an error status, not worth doing the subsequent ones
if(self.Status != AirTouchStatus.OK):
print(self.Status)
for msg in self.Messages:
print(msg.Message);
return;
#get the group nicknames
await self.UpdateGroupNames()
#get ac infos
await self.UpdateAcInfo()
#get ac abilities
await self.UpdateAcAbility()
#sort out which AC belongs to which zone/group
self.AssignAcsToGroups()
async def findVersion(self):
if(await self.isOpen(self.IpAddress, 9004)):
self.atVersion = AirTouchVersion.AIRTOUCH4
self.atPort = 9004
return
elif(await self.isOpen(self.IpAddress, 9005)):
self.atVersion = AirTouchVersion.AIRTOUCH5
self.atPort = 9005
return
else:
self.Status = AirTouchStatus.ERROR
errorMessage = AirTouchError()
errorMessage.Message = "Could not find open port 9004 (v4) or 9005 (v5)"
self.Messages.append(errorMessage)
async def isOpen(self, ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
s.shutdown(2)
return True
except:
return False
## Initial Update Calls
async def UpdateAcAbility(self):
acAbilityMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("AcAbility", self.atVersion);
await self.SendMessageToAirtouch(acAbilityMessage)
async def UpdateAcInfo(self):
message = packetmap.MessageFactory.CreateEmptyMessageOfType("AcStatus", self.atVersion);
await self.SendMessageToAirtouch(message)
async def UpdateGroupInfo(self):
message = packetmap.MessageFactory.CreateEmptyMessageOfType("GroupStatus", self.atVersion);
await self.SendMessageToAirtouch(message)
async def UpdateGroupNames(self):
nameMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("GroupName", self.atVersion);
await self.SendMessageToAirtouch(nameMessage)
def AssignAcsToGroups(self):
## Assign ACs to groups (zones) based on startgroupnumber, count
for group in self.groups.values():
#find out which ac this group belongs to
for ac in self.acs.values():
if(ac.StartGroupNumber == 0 and ac.GroupCount == 0):
#assuming this means theres only one ac? so every group belongs to this ac?
group.BelongsToAc = ac.AcNumber
if(ac.StartGroupNumber <= group.GroupNumber and group.GroupNumber <= ac.StartGroupNumber + ac.GroupCount):
group.BelongsToAc = ac.AcNumber
## END Initial Update Calls
## Turn things on/off/temp by name (just finds the number, calls the right function)
async def TurnGroupOnByName(self, groupName):
targetGroup = self._getTargetGroup(groupName)
await self.TurnGroupOn(targetGroup.GroupNumber);
async def TurnGroupOffByName(self, groupName):
targetGroup = self._getTargetGroup(groupName)
await self.TurnGroupOff(targetGroup.GroupNumber);
async def SetGroupToTemperatureByGroupName(self, groupName, temperature):
targetGroup = self._getTargetGroup(groupName)
await self.SetGroupToTemperature(targetGroup.GroupNumber, temperature);
async def SetGroupToPercentByGroupName(self, groupName, percent):
targetGroup = self._getTargetGroup(groupName)
await self.SetGroupToPercentage(targetGroup.GroupNumber, percent);
## END Turn things on/off/temp by name (just finds the number, calls the right function)
## Group/zone modes
async def SetCoolingModeByGroup(self, groupNumber, coolingMode):
await self.SetCoolingModeForAc(self.groups[groupNumber].BelongsToAc, coolingMode);
return self.groups[groupNumber];
async def SetFanSpeedByGroup(self, groupNumber, fanSpeed):
await self.SetFanSpeedForAc(self.groups[groupNumber].BelongsToAc, fanSpeed);
return self.groups[groupNumber];
def GetSupportedCoolingModesByGroup(self, groupNumber):
return self.GetSupportedCoolingModesForAc(self.groups[groupNumber].BelongsToAc);
def GetSupportedFanSpeedsByGroup(self, groupNumber):
return self.GetSupportedFanSpeedsForAc(self.groups[groupNumber].BelongsToAc);
## END Group/zone modes
# Main control functions
async def TurnGroupOn(self, groupNumber):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("GroupControl", self.atVersion);
controlMessage.SetMessageValue("Power", 3)
controlMessage.SetMessageValue("GroupNumber", groupNumber)
await self.SendMessageToAirtouch(controlMessage)
return self.groups[groupNumber];
async def TurnGroupOff(self, groupNumber):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("GroupControl", self.atVersion);
controlMessage.SetMessageValue("Power", 2)
controlMessage.SetMessageValue("GroupNumber", groupNumber)
await self.SendMessageToAirtouch(controlMessage)
return self.groups[groupNumber];
async def TurnAcOn(self, acNumber):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("AcControl", self.atVersion);
#these are required to leave these settings unchanged
controlMessage.SetMessageValue("AcMode", 0x0f);
controlMessage.SetMessageValue("AcFanSpeed", 0x0f);
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
controlMessage.SetMessageValue("TargetSetpoint", 0x3f);
if(self.atVersion == AirTouchVersion.AIRTOUCH5):
controlMessage.SetMessageValue("SetpointControlType", 0x00);
controlMessage.SetMessageValue("Power", 3)
controlMessage.SetMessageValue("AcNumber", acNumber)
await self.SendMessageToAirtouch(controlMessage)
async def TurnAcOff(self, acNumber):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("AcControl", self.atVersion);
#these are required to leave these settings unchanged
controlMessage.SetMessageValue("AcMode", 0x0f);
controlMessage.SetMessageValue("AcFanSpeed", 0x0f);
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
controlMessage.SetMessageValue("TargetSetpoint", 0x3f);
if(self.atVersion == AirTouchVersion.AIRTOUCH5):
controlMessage.SetMessageValue("SetpointControlType", 0x00);
controlMessage.SetMessageValue("Power", 2)
controlMessage.SetMessageValue("AcNumber", acNumber)
await self.SendMessageToAirtouch(controlMessage)
# END Main control functions
#use a fanspeed reported from GetSupportedFanSpeedsForAc
async def SetFanSpeedForAc(self, acNumber, fanSpeed):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("AcControl", self.atVersion);
#these are required to leave these settings unchanged
controlMessage.SetMessageValue("AcMode", 0x0f);
controlMessage.SetMessageValue("AcFanSpeed", packetmap.SettingValueTranslator.NamedValueToRawValue("AcFanSpeed", fanSpeed));
controlMessage.SetMessageValue("TargetSetpoint", 0x3f);
controlMessage.SetMessageValue("AcNumber", acNumber)
await self.SendMessageToAirtouch(controlMessage)
#use a mode reported from GetSupportedCoolingModesForAc
async def SetCoolingModeForAc(self, acNumber, acMode):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("AcControl", self.atVersion);
#these are required to leave these settings unchanged
controlMessage.SetMessageValue("AcMode", packetmap.SettingValueTranslator.NamedValueToRawValue("AcMode", acMode));
controlMessage.SetMessageValue("AcFanSpeed", 0x0f);
controlMessage.SetMessageValue("TargetSetpoint", 0x3f);
controlMessage.SetMessageValue("AcNumber", acNumber)
await self.SendMessageToAirtouch(controlMessage)
#GetSupportedCoolingModesForAc
def GetSupportedCoolingModesForAc(self, acNumber):
return self.acs[acNumber].ModeSupported;
#GetSupportedFanSpeedsForAc
def GetSupportedFanSpeedsForAc(self, acNumber):
return self.acs[acNumber].FanSpeedSupported;
## Group/Zone temperatures
async def SetGroupToTemperature(self, groupNumber, temperature):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("GroupControl", self.atVersion);
controlMessage.SetMessageValue("Power", 3)
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
controlMessage.SetMessageValue("HaveTemperatureControl", 3)
controlMessage.SetMessageValue("GroupSettingValue", 5)
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
controlMessage.SetMessageValue("TargetSetpoint", temperature)
elif(self.atVersion == AirTouchVersion.AIRTOUCH5):
controlMessage.SetMessageValue("TargetSetpoint", temperature*10-100)
controlMessage.SetMessageValue("GroupNumber", groupNumber)
await self.SendMessageToAirtouch(controlMessage)
return self.groups[groupNumber];
#should this turn the group on?
async def SetGroupToPercentage(self, groupNumber, percent):
controlMessage = packetmap.MessageFactory.CreateEmptyMessageOfType("GroupControl", self.atVersion);
controlMessage.SetMessageValue("Power", 3)
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
controlMessage.SetMessageValue("HaveTemperatureControl", 3)
controlMessage.SetMessageValue("GroupSettingValue", 4)
controlMessage.SetMessageValue("TargetSetpoint", percent)
controlMessage.SetMessageValue("GroupNumber", groupNumber)
await self.SendMessageToAirtouch(controlMessage)
return self.groups[groupNumber];
## END Group/Zone temperatures
## Helper functions
def GetAcs(self):
acs = [];
for acNumber in self.acs.keys():
ac = self.acs[acNumber]
acs.append(ac);
return acs;
def GetGroupByGroupNumber(self, groupNumber):
return self.groups[groupNumber];
def GetGroups(self):
groups = [];
for groupNumber in self.groups.keys():
groupInfo = self.groups[groupNumber]
groups.append(groupInfo);
return groups;
#returns a list of groups, each group has a name, a number, on or off, current damper opening, current temp and target temp
def GetVersion(self):
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
return "4"
elif(self.atVersion == AirTouchVersion.AIRTOUCH5):
return "5"
return ""
## END Helper functions
## Major AT comms. Set up a message, send it, get the result, translate packet to message at the end.
async def SendMessageToAirtouch(self, messageObject):
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
if(messageObject.MessageType == "GroupStatus"):
MESSAGE = "80b0012b0000"
if(messageObject.MessageType == "GroupName"):
MESSAGE = "90b0011f0002ff12"
if(messageObject.MessageType == "AcAbility"):
MESSAGE = "90b0011f0002ff11"
if(messageObject.MessageType == "AcStatus"):
MESSAGE = "80b0012d0000f4cf"
if(messageObject.MessageType == "GroupControl" or messageObject.MessageType == "AcControl"):
MESSAGE = communicate.MessageObjectToMessagePacket(messageObject, messageObject.MessageType, self.atVersion);
elif(self.atVersion == AirTouchVersion.AIRTOUCH5):
if(messageObject.MessageType == "GroupStatus"):
MESSAGE = "80b001c000082100000000000000"
if(messageObject.MessageType == "GroupName"):
MESSAGE = "90b0011f0002ff13"
if(messageObject.MessageType == "AcAbility"):
MESSAGE = "90b0011f0002ff11"
if(messageObject.MessageType == "AcStatus"):
MESSAGE = "80b001c000082300000000000000"
if(messageObject.MessageType == "GroupControl" or messageObject.MessageType == "AcControl"):
MESSAGE = communicate.MessageObjectToMessagePacket(messageObject, messageObject.MessageType, self.atVersion);
try:
dataResult = await communicate.SendMessagePacketToAirtouch(MESSAGE, self.IpAddress, self.atVersion, self.atPort)
self.Status = AirTouchStatus.OK
except Exception as e:
if(self.Status == AirTouchStatus.OK):
self.Status = AirTouchStatus.CONNECTION_INTERRUPTED
else:
self.Status = AirTouchStatus.CONNECTION_LOST
errorMessage = AirTouchError()
errorMessage.Message = "Could not send message to airtouch: " + str(e)
self.Messages.append(errorMessage)
return
return self.TranslatePacketToMessage(dataResult)
## Interpret response object
def TranslatePacketToMessage(self, dataResult):
#If the request hasn't gone well, we don't want to update any of the data we have with bad/no data
if(self.Status != AirTouchStatus.OK):
return;
if(self.atVersion == AirTouchVersion.AIRTOUCH4):
## AT4 is easy
address = dataResult[2:4]
messageId = dataResult[4:5]
messageType = dataResult[5:6]
dataLength = dataResult[6:8]
if(messageType == b'\x2b'):
self.DecodeAirtouchGroupStatusMessage(dataResult[8::]);
if(messageType == b'\x1f'):
self.DecodeAirtouchExtendedMessage(dataResult[8::]);
if(messageType == b'\x2d'):
self.DecodeAirtouchAcStatusMessage(dataResult[8::]);
else:
## AT5 requires a bit more knowledge if it's an extended message.
messageType = dataResult[17:18]
if(messageType == b'\xc0'):
messageSubType = dataResult[20:21]
### We got a control message
if(messageSubType == b'\x21'):
### Zone Status Message
self.DecodeAirtouch5ZoneStatusMessage(dataResult[22::]);
if(messageSubType == b'\x23'):
### AC Status Message
self.DecodeAirtouch5AcStatusMessage(dataResult[22::]);
if(messageType == b'\x1f'):
messageSubType = dataResult[21:22]
if(messageSubType == b'\x13'):
self.DecodeAirtouch5GroupNames(dataResult[17::]);
if(messageSubType == b'\x11'):
self.DecodeAirtouch5AcAbility(dataResult[17::]);
## Only for AT4, decode extended.
def DecodeAirtouchExtendedMessage(self, payload):
groups = self.groups;
if(payload[0:2] == b'\xff\x12'):
for groupChunk in helper.chunks(payload[2:], 9):
if(len(groupChunk) < 9):
continue
groupNumber = groupChunk[0]
groupInfo = AirTouchGroup()
if groupNumber not in groups:
groups[groupNumber] = groupInfo;
else:
groupInfo = groups[groupNumber];
groupName = groupChunk[1:9].decode("utf-8").rstrip('\0')
groups[groupNumber].GroupName = groupName
if(payload[0:2] == b'\xff\x11'):
chunkSize = communicate.TranslateMapValueToValue(payload[2:], packetmap.DataLocationTranslator.map[self.atVersion.value]["AcAbility"]["ChunkSize"]);
self.DecodeAirtouchMessage(payload[2:], packetmap.DataLocationTranslator.map[self.atVersion.value]["AcAbility"], False, chunkSize + 2)
## Only for AT4, decode a basic message.
def DecodeAirtouchMessage(self, payload, map, isGroupBased, chunkSize):
for chunk in helper.chunks(payload, chunkSize):
if(len(chunk) < chunkSize):
continue
packetInfoLocationMap = map
resultList = self.groups
resultObject = AirTouchGroup()
if(isGroupBased):
groupNumber = communicate.TranslateMapValueToValue(chunk, packetInfoLocationMap["GroupNumber"]);
if groupNumber not in resultList:
resultList[groupNumber] = resultObject;
else:
resultObject = resultList[groupNumber];
else:
resultList = self.acs
resultObject = AirTouchAc()
acNumber = communicate.TranslateMapValueToValue(chunk, packetInfoLocationMap["AcNumber"]);
if acNumber not in resultList:
resultObject.AcName = "AC " + str(acNumber)
resultList[acNumber] = resultObject;
else:
resultObject = resultList[acNumber];
packetInfoAttributes = [attr for attr in packetInfoLocationMap.keys()]
for attribute in packetInfoAttributes:
mapValue = communicate.TranslateMapValueToValue(chunk, packetInfoLocationMap[attribute])
translatedValue = packetmap.SettingValueTranslator.RawValueToNamedValue(attribute, mapValue);
#a bit dodgy, to get the mode and fanspeed as lists
if(attribute.endswith("ModeSupported") and translatedValue != 0):
modeSupported = [];
if(hasattr(resultObject, "ModeSupported")):
modeSupported = resultObject.ModeSupported
modeSupported.append(attribute.replace("ModeSupported", ""));
setattr(resultObject, "ModeSupported", modeSupported)
elif(attribute.endswith("FanSpeedSupported") and translatedValue != 0):
modeSupported = [];
if(hasattr(resultObject, "FanSpeedSupported")):
modeSupported = resultObject.FanSpeedSupported
modeSupported.append(attribute.replace("FanSpeedSupported", ""));
setattr(resultObject, "FanSpeedSupported", modeSupported)
else:
setattr(resultObject, attribute, translatedValue)
#read the chunk as a set of bytes concatenated together.
#use the map of attribute locations
#for each entry in the map
#read out entry value from map
#run translate on class matching entry name with entry value
#set property of entry name on the group response
## Only for AT5, get additional details
def DecodeAirtouch5Message(self, payload, map, isGroupBased):
normalDataLength = int.from_bytes(payload[0:2], byteorder='big')
repeatDataLength = int.from_bytes(payload[2:4], byteorder='big')
repeatCount = int.from_bytes(payload[4:6], byteorder='big')
packetInfoLocationMap = map
for i in range(repeatCount):
resultList = self.groups
resultObject = AirTouchGroup()
chunkStart = 6+(i*repeatDataLength)
chunk = payload[chunkStart:chunkStart+repeatDataLength]
if(isGroupBased):
groupNumber = communicate.TranslateMapValueToValue(chunk, packetInfoLocationMap["GroupNumber"]);
if groupNumber not in resultList:
resultList[groupNumber] = resultObject;
else:
resultObject = resultList[groupNumber];
else:
resultList = self.acs
resultObject = AirTouchAc()
acNumber = communicate.TranslateMapValueToValue(chunk, packetInfoLocationMap["AcNumber"]);
if acNumber not in resultList:
resultList[acNumber] = resultObject;
else:
resultObject = resultList[acNumber];
self.DecodeAttributes(chunk, packetInfoLocationMap, resultObject)
## Specific for AC Ability message
def DecodeAirtouch5AcAbility(self, payload):
#decodes AC Abilities based on page 12 of the comms protocol.
dataLength = int.from_bytes(payload[1:3], byteorder='big')-2 #get the data length, subtract the CRC bytes and "header". This will allow us to track size.
AcCount = 0
if( dataLength % 26 != 0):
self.Status = AirTouchStatus.ERROR
errorMessage = AirTouchError()
errorMessage.Message = "Got a response to ACAbility without correct field details"
self.Messages.append(errorMessage)
return
else:
AcCount = dataLength / 26
payload = payload[5::]
packetInfoLocationMap = packetmap.DataLocationTranslator.map[self.atVersion.value]["AcAbility"]
for i in range(int(AcCount)):
AcPayload = payload[i*26:i*26+26]
resultList = self.acs
resultObject = AirTouchAc()
acNumber = communicate.TranslateMapValueToValue(AcPayload, packetInfoLocationMap["AcNumber"]);
if acNumber not in resultList:
resultList[acNumber] = resultObject;
else:
resultObject = resultList[acNumber];
self.DecodeAttributes(AcPayload, packetInfoLocationMap, resultObject)
zoneName = AcPayload[2:18].decode("utf-8").rstrip('\0')
resultObject.AcName = zoneName
def DecodeAirtouch5GroupNames(self, payload):
#decodes group names based on page 14 of the comms protocol.
groups = self.groups;
dataLength = int.from_bytes(payload[1:3], byteorder='big')-2 #get the data length, subtract the CRC bytes. This will allow us to track size.
tracker = 3
if(payload[tracker:tracker+2] == b'\xff\x13'):
tracker = 5
while(tracker < dataLength):
zoneNumber = int.from_bytes(payload[tracker:tracker+1], byteorder='big')
nameLength = int.from_bytes(payload[tracker+1:tracker+2], byteorder='big')
zoneName = payload[tracker+2:tracker+2+nameLength].decode("utf-8").rstrip('\0')
tracker += (2+nameLength)
groups[zoneNumber].GroupName = zoneName
else:
self.Status = AirTouchStatus.ERROR
errorMessage = AirTouchError()
errorMessage.Message = "Got a response to GroupNames without correct field details"
self.Messages.append(errorMessage)
for group in self.groups.values():
group.GroupName = "Zone "+str(group.GroupNumber);
def DecodeAttributes(self, chunk, packetInfoLocationMap, resultObject):
packetInfoAttributes = [attr for attr in packetInfoLocationMap.keys()]
for attribute in packetInfoAttributes:
mapValue = communicate.TranslateMapValueToValue(chunk, packetInfoLocationMap[attribute])
translatedValue = packetmap.SettingValueTranslator.RawValueToNamedValue(attribute, mapValue, AirTouchVersion.AIRTOUCH5.value);
if(attribute.endswith("ModeSupported") and translatedValue != 0):
modeSupported = [];
if(hasattr(resultObject, "ModeSupported")):
modeSupported = resultObject.ModeSupported
if attribute.replace("ModeSupported", "") not in modeSupported:
modeSupported.append(attribute.replace("ModeSupported", ""));
setattr(resultObject, "ModeSupported", modeSupported)
elif(attribute.endswith("FanSpeedSupported") and translatedValue != 0):
modeSupported = [];
if(hasattr(resultObject, "FanSpeedSupported")):
modeSupported = resultObject.FanSpeedSupported
if attribute.replace("FanSpeedSupported", "") not in modeSupported:
modeSupported.append(attribute.replace("FanSpeedSupported", ""));
setattr(resultObject, "FanSpeedSupported", modeSupported)
else:
setattr(resultObject, attribute, translatedValue)
def DecodeAirtouchGroupStatusMessage(self, payload):
self.DecodeAirtouchMessage(payload, packetmap.DataLocationTranslator.map[self.atVersion.value]["GroupStatus"], True, 6);
def DecodeAirtouch5ZoneStatusMessage(self, payload):
self.DecodeAirtouch5Message(payload, packetmap.DataLocationTranslator.map[self.atVersion.value]["GroupStatus"], True);
def DecodeAirtouchAcStatusMessage(self, payload):
self.DecodeAirtouchMessage(payload, packetmap.DataLocationTranslator.map[self.atVersion.value]["AcStatus"], False, 8);
#read the chunk as a set of bytes concatenated together.
#use the map of attribute locations
#for each entry in the map
#read out entry value from map
#run translate on class matching entry name with entry value
#set property of entry name on the group response
def DecodeAirtouch5AcStatusMessage(self, payload):
self.DecodeAirtouch5Message(payload, packetmap.DataLocationTranslator.map[self.atVersion.value]["AcStatus"], False);
def _getTargetGroup(self, groupName):
return [group for group in self.groups.values() if group.GroupName == groupName][0]
|