4
0

controller.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import asyncio
  2. from base64 import b64encode
  3. import binascii
  4. import logging
  5. from homeassistant.const import ATTR_ENTITY_ID
  6. from homeassistant.core import split_entity_id
  7. from . import Helper
  8. _LOGGER = logging.getLogger(__name__)
  9. BROADLINK_CONTROLLER = 'Broadlink'
  10. XIAOMI_CONTROLLER = 'Xiaomi'
  11. MQTT_CONTROLLER = 'MQTT'
  12. ENC_BASE64 = 'Base64'
  13. ENC_HEX = 'Hex'
  14. ENC_PRONTO = 'Pronto'
  15. ENC_RAW = 'Raw'
  16. BROADLINK_COMMANDS_ENCODING = [
  17. ENC_BASE64, ENC_HEX, ENC_PRONTO]
  18. XIAOMI_COMMANDS_ENCODING = [
  19. ENC_PRONTO, ENC_RAW]
  20. MQTT_COMMANDS_ENCODING = [ENC_RAW]
  21. class Controller():
  22. def __init__(self, hass, controller, encoding, controller_data):
  23. if controller not in [
  24. BROADLINK_CONTROLLER, XIAOMI_CONTROLLER, MQTT_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. self.hass = hass
  39. self._controller = controller
  40. self._encoding = encoding
  41. self._controller_data = controller_data
  42. async def send(self, command):
  43. if self._controller == BROADLINK_CONTROLLER:
  44. if self._encoding == ENC_HEX:
  45. try:
  46. command = binascii.unhexlify(command)
  47. command = b64encode(command).decode('utf-8')
  48. except:
  49. raise Exception("Error while converting "
  50. "Hex to Base64 encoding")
  51. if self._encoding == ENC_PRONTO:
  52. try:
  53. command = command.replace(' ', '')
  54. command = bytearray.fromhex(command)
  55. command = Helper.pronto2lirc(command)
  56. command = Helper.lirc2broadlink(command)
  57. command = b64encode(command).decode('utf-8')
  58. except:
  59. raise Exception("Error while converting "
  60. "Pronto to Base64 encoding")
  61. service_data = {
  62. 'host': self._controller_data,
  63. 'packet': command
  64. }
  65. await self.hass.services.async_call(
  66. 'broadlink', 'send', service_data)
  67. if self._controller == XIAOMI_CONTROLLER:
  68. service_data = {
  69. ATTR_ENTITY_ID: self._controller_data,
  70. 'command': self._encoding.lower() + ':' + command
  71. }
  72. await self.hass.services.async_call(
  73. 'remote', 'send_command', service_data)
  74. if self._controller == MQTT_CONTROLLER:
  75. service_data = {
  76. 'topic': self._controller_data,
  77. 'payload': command
  78. }
  79. await self.hass.services.async_call(
  80. 'mqtt', 'publish', service_data)