controller.py 4.0 KB

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