controller.py 3.9 KB

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