controller.py 4.5 KB

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