|
@@ -18,20 +18,31 @@ ENC_RAW = 'Raw'
|
|
|
BROADLINK_COMMANDS_ENCODING = [
|
|
BROADLINK_COMMANDS_ENCODING = [
|
|
|
ENC_BASE64, ENC_HEX, ENC_PRONTO]
|
|
ENC_BASE64, ENC_HEX, ENC_PRONTO]
|
|
|
|
|
|
|
|
|
|
+MQTT_COMMANDS_ENCODING = [ENC_RAW]
|
|
|
|
|
+
|
|
|
class Controller():
|
|
class Controller():
|
|
|
- def __init__(self, hass, controller, encoding, service, topic):
|
|
|
|
|
|
|
+ def __init__(self, hass, controller, encoding, service, topic=None):
|
|
|
if controller not in [
|
|
if controller not in [
|
|
|
- BROADLINK_CONTROLLER, MQTT_CONTROLLER, XIAOMI_CONTROLLER]:
|
|
|
|
|
|
|
+ BROADLINK_CONTROLLER, XIAOMI_CONTROLLER, MQTT_CONTROLLER]:
|
|
|
raise Exception("The controller is not supported.")
|
|
raise Exception("The controller is not supported.")
|
|
|
|
|
|
|
|
|
|
+ if controller == BROADLINK_CONTROLLER:
|
|
|
|
|
+ if encoding not in BROADLINK_COMMANDS_ENCODING:
|
|
|
|
|
+ raise Exception("The encoding is not supported "
|
|
|
|
|
+ "by the Broadlink controller.")
|
|
|
|
|
+
|
|
|
if controller == XIAOMI_CONTROLLER:
|
|
if controller == XIAOMI_CONTROLLER:
|
|
|
raise Exception("The Xiaomi IR controller "
|
|
raise Exception("The Xiaomi IR controller "
|
|
|
"is not yet supported.")
|
|
"is not yet supported.")
|
|
|
|
|
|
|
|
- if controller == BROADLINK_CONTROLLER:
|
|
|
|
|
- if encoding not in BROADLINK_COMMANDS_ENCODING:
|
|
|
|
|
|
|
+ if controller == MQTT_CONTROLLER:
|
|
|
|
|
+ if encoding not in MQTT_COMMANDS_ENCODING:
|
|
|
raise Exception("The encoding is not supported "
|
|
raise Exception("The encoding is not supported "
|
|
|
- "by the Broadlink controller.")
|
|
|
|
|
|
|
+ "by the mqtt controller.")
|
|
|
|
|
+
|
|
|
|
|
+ if not topic:
|
|
|
|
|
+ raise Exception("controller_command_topic must be "
|
|
|
|
|
+ "specified for mqtt controllers.")
|
|
|
|
|
|
|
|
self.hass = hass
|
|
self.hass = hass
|
|
|
self._service_domain = split_entity_id(service)[0]
|
|
self._service_domain = split_entity_id(service)[0]
|
|
@@ -40,43 +51,42 @@ class Controller():
|
|
|
self._controller = controller
|
|
self._controller = controller
|
|
|
self._encoding = encoding
|
|
self._encoding = encoding
|
|
|
|
|
|
|
|
- if self._service_domain == 'mqtt':
|
|
|
|
|
- if self._command_topic == '':
|
|
|
|
|
- raise Exception("controller_command_topic must be "
|
|
|
|
|
- "specified for mqtt controllers.")
|
|
|
|
|
-
|
|
|
|
|
async def send(self, command):
|
|
async def send(self, command):
|
|
|
- if self._controller not in [BROADLINK_CONTROLLER, MQTT_CONTROLLER]:
|
|
|
|
|
- raise Exception("Unsupported controller")
|
|
|
|
|
-
|
|
|
|
|
- if self._encoding == ENC_HEX:
|
|
|
|
|
- try:
|
|
|
|
|
- command = binascii.unhexlify(command)
|
|
|
|
|
- command = b64encode(command).decode('utf-8')
|
|
|
|
|
- except:
|
|
|
|
|
- raise Exception("Error while converting "
|
|
|
|
|
- "Hex to Base64 encoding")
|
|
|
|
|
-
|
|
|
|
|
- if self._encoding == ENC_PRONTO:
|
|
|
|
|
- try:
|
|
|
|
|
- command = command.replace(' ', '')
|
|
|
|
|
- command = bytearray.fromhex(command)
|
|
|
|
|
- command = Helper.pronto2lirc(command)
|
|
|
|
|
- command = Helper.lirc2broadlink(command)
|
|
|
|
|
- command = b64encode(command).decode('utf-8')
|
|
|
|
|
- except:
|
|
|
|
|
- raise Exception("Error while converting "
|
|
|
|
|
- "Pronto to Base64 encoding")
|
|
|
|
|
-
|
|
|
|
|
if self._controller == BROADLINK_CONTROLLER:
|
|
if self._controller == BROADLINK_CONTROLLER:
|
|
|
- service_data = {'packet': command}
|
|
|
|
|
|
|
+ if self._encoding == ENC_HEX:
|
|
|
|
|
+ try:
|
|
|
|
|
+ command = binascii.unhexlify(command)
|
|
|
|
|
+ command = b64encode(command).decode('utf-8')
|
|
|
|
|
+ except:
|
|
|
|
|
+ raise Exception("Error while converting "
|
|
|
|
|
+ "Hex to Base64 encoding")
|
|
|
|
|
+
|
|
|
|
|
+ if self._encoding == ENC_PRONTO:
|
|
|
|
|
+ try:
|
|
|
|
|
+ command = command.replace(' ', '')
|
|
|
|
|
+ command = bytearray.fromhex(command)
|
|
|
|
|
+ command = Helper.pronto2lirc(command)
|
|
|
|
|
+ command = Helper.lirc2broadlink(command)
|
|
|
|
|
+ command = b64encode(command).decode('utf-8')
|
|
|
|
|
+ except:
|
|
|
|
|
+ raise Exception("Error while converting "
|
|
|
|
|
+ "Pronto to Base64 encoding")
|
|
|
|
|
+
|
|
|
|
|
+ service_data = {
|
|
|
|
|
+ 'packet': command
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ await self.hass.services.async_call(
|
|
|
|
|
+ self._service_domain, self._service_name,
|
|
|
|
|
+ service_data)
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- if self._controller = MQTT_CONTROLLER:
|
|
|
|
|
|
|
+ if self._controller == MQTT_CONTROLLER:
|
|
|
service_data = {
|
|
service_data = {
|
|
|
- 'topic': self._command_topic,
|
|
|
|
|
- 'payload': command
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ 'topic': self._command_topic,
|
|
|
|
|
+ 'payload': command
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- await self.hass.services.async_call(
|
|
|
|
|
- self._service_domain, self._service_name,
|
|
|
|
|
- service_data)
|
|
|
|
|
|
|
+ await self.hass.services.async_call(
|
|
|
|
|
+ self._service_domain, self._service_name,
|
|
|
|
|
+ service_data)
|