controller.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from base64 import b64encode
  2. import binascii
  3. import requests
  4. import logging
  5. from homeassistant.const import ATTR_ENTITY_ID
  6. from . import Helper
  7. _LOGGER = logging.getLogger(__name__)
  8. BROADLINK_CONTROLLER = 'Broadlink'
  9. XIAOMI_CONTROLLER = 'Xiaomi'
  10. MQTT_CONTROLLER = 'MQTT'
  11. LOOKIN_CONTROLLER = 'LOOKin'
  12. ENC_BASE64 = 'Base64'
  13. ENC_HEX = 'Hex'
  14. ENC_PRONTO = 'Pronto'
  15. ENC_RAW = 'Raw'
  16. BROADLINK_COMMANDS_ENCODING = [ENC_BASE64, ENC_HEX, ENC_PRONTO]
  17. XIAOMI_COMMANDS_ENCODING = [ENC_PRONTO, ENC_RAW]
  18. MQTT_COMMANDS_ENCODING = [ENC_RAW]
  19. LOOKIN_COMMANDS_ENCODING = [ENC_PRONTO, ENC_RAW]
  20. class Controller():
  21. def __init__(self, hass, controller, encoding, controller_data):
  22. if controller not in [
  23. BROADLINK_CONTROLLER, XIAOMI_CONTROLLER,
  24. MQTT_CONTROLLER, LOOKIN_CONTROLLER]:
  25. raise Exception("The controller is not supported.")
  26. if controller == BROADLINK_CONTROLLER:
  27. if encoding not in BROADLINK_COMMANDS_ENCODING:
  28. raise Exception("The encoding is not supported "
  29. "by the Broadlink controller.")
  30. if controller == XIAOMI_CONTROLLER:
  31. if encoding not in XIAOMI_COMMANDS_ENCODING:
  32. raise Exception("The encoding is not supported "
  33. "by the Xiaomi controller.")
  34. if controller == MQTT_CONTROLLER:
  35. if encoding not in MQTT_COMMANDS_ENCODING:
  36. raise Exception("The encoding is not supported "
  37. "by the mqtt controller.")
  38. if controller == LOOKIN_CONTROLLER:
  39. if encoding not in LOOKIN_COMMANDS_ENCODING:
  40. raise Exception("The encoding is not supported "
  41. "by the LOOKin controller.")
  42. self.hass = hass
  43. self._controller = controller
  44. self._encoding = encoding
  45. self._controller_data = controller_data
  46. async def send(self, command):
  47. if self._controller == BROADLINK_CONTROLLER:
  48. if self._encoding == ENC_HEX:
  49. try:
  50. command = binascii.unhexlify(command)
  51. command = b64encode(command).decode('utf-8')
  52. except:
  53. raise Exception("Error while converting "
  54. "Hex to Base64 encoding")
  55. if self._encoding == ENC_PRONTO:
  56. try:
  57. command = command.replace(' ', '')
  58. command = bytearray.fromhex(command)
  59. command = Helper.pronto2lirc(command)
  60. command = Helper.lirc2broadlink(command)
  61. command = b64encode(command).decode('utf-8')
  62. except:
  63. raise Exception("Error while converting "
  64. "Pronto to Base64 encoding")
  65. service_data = {
  66. 'host': self._controller_data,
  67. 'packet': command
  68. }
  69. await self.hass.services.async_call(
  70. 'broadlink', 'send', service_data)
  71. if self._controller == XIAOMI_CONTROLLER:
  72. service_data = {
  73. ATTR_ENTITY_ID: self._controller_data,
  74. 'command': self._encoding.lower() + ':' + command
  75. }
  76. await self.hass.services.async_call(
  77. 'remote', 'send_command', service_data)
  78. if self._controller == MQTT_CONTROLLER:
  79. service_data = {
  80. 'topic': self._controller_data,
  81. 'payload': command
  82. }
  83. await self.hass.services.async_call(
  84. 'mqtt', 'publish', service_data)
  85. if self._controller == LOOKIN_CONTROLLER:
  86. encoding = self._encoding.lower().replace('pronto', 'prontohex')
  87. url = f"http://{self._controller_data}/commands/ir/" \
  88. f"{encoding}/{command}"
  89. await self.hass.async_add_executor_job(
  90. requests.get, url
  91. )