Parcourir la source

Fix MQTT support

Vassilis Panos il y a 7 ans
Parent
commit
e7a37168be
6 fichiers modifiés avec 59 ajouts et 49 suppressions
  1. 1 1
      smartir/__init__.py
  2. 2 2
      smartir/climate.py
  3. 50 40
      smartir/controller.py
  4. 2 2
      smartir/fan.py
  5. 2 2
      smartir/media_player.py
  6. 2 2
      version.json

+ 1 - 1
smartir/__init__.py

@@ -16,7 +16,7 @@ from homeassistant.helpers.typing import ConfigType
 _LOGGER = logging.getLogger(__name__)
 _LOGGER = logging.getLogger(__name__)
 
 
 DOMAIN = 'smartir'
 DOMAIN = 'smartir'
-VERSION = '1.3.8'
+VERSION = '1.3.9'
 VERSION_URL = (
 VERSION_URL = (
     "https://raw.githubusercontent.com/"
     "https://raw.githubusercontent.com/"
     "smartHomeHub/SmartIR/{}/version.json")
     "smartHomeHub/SmartIR/{}/version.json")

+ 2 - 2
smartir/climate.py

@@ -44,7 +44,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
     vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
     vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
     vol.Required(CONF_DEVICE_CODE): cv.positive_int,
     vol.Required(CONF_DEVICE_CODE): cv.positive_int,
     vol.Required(CONF_CONTROLLER_SEND_SERVICE): cv.entity_id,
     vol.Required(CONF_CONTROLLER_SEND_SERVICE): cv.entity_id,
-    vol.Required(CONF_CONTROLLER_COMMAND_TOPIC): cv.string,
+    vol.Optional(CONF_CONTROLLER_COMMAND_TOPIC): cv.string,
     vol.Optional(CONF_TEMPERATURE_SENSOR): cv.entity_id,
     vol.Optional(CONF_TEMPERATURE_SENSOR): cv.entity_id,
     vol.Optional(CONF_HUMIDITY_SENSOR): cv.entity_id,
     vol.Optional(CONF_HUMIDITY_SENSOR): cv.entity_id,
     vol.Optional(CONF_POWER_SENSOR): cv.entity_id
     vol.Optional(CONF_POWER_SENSOR): cv.entity_id
@@ -132,7 +132,7 @@ class SmartIRClimate(ClimateDevice, RestoreEntity):
             self.hass,
             self.hass,
             self._supported_controller, 
             self._supported_controller, 
             self._commands_encoding,
             self._commands_encoding,
-            self._controller_send_service
+            self._controller_send_service,
             self._controller_command_topic)
             self._controller_command_topic)
             
             
     async def async_added_to_hass(self):
     async def async_added_to_hass(self):

+ 50 - 40
smartir/controller.py

@@ -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)

+ 2 - 2
smartir/fan.py

@@ -34,7 +34,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
     vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
     vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
     vol.Required(CONF_DEVICE_CODE): cv.positive_int,
     vol.Required(CONF_DEVICE_CODE): cv.positive_int,
     vol.Required(CONF_CONTROLLER_SEND_SERVICE): cv.entity_id,
     vol.Required(CONF_CONTROLLER_SEND_SERVICE): cv.entity_id,
-    vol.Required(CONF_CONTROLLER_COMMAND_TOPIC): cv.string,
+    vol.Optional(CONF_CONTROLLER_COMMAND_TOPIC): cv.string,
     vol.Optional(CONF_POWER_SENSOR): cv.entity_id
     vol.Optional(CONF_POWER_SENSOR): cv.entity_id
 })
 })
 
 
@@ -115,7 +115,7 @@ class SmartIRFan(FanEntity, RestoreEntity):
             self.hass,
             self.hass,
             self._supported_controller, 
             self._supported_controller, 
             self._commands_encoding,
             self._commands_encoding,
-            self._controller_send_service
+            self._controller_send_service,
             self._controller_command_topic)
             self._controller_command_topic)
 
 
     async def async_added_to_hass(self):
     async def async_added_to_hass(self):

+ 2 - 2
smartir/media_player.py

@@ -34,7 +34,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
     vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
     vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
     vol.Required(CONF_DEVICE_CODE): cv.positive_int,
     vol.Required(CONF_DEVICE_CODE): cv.positive_int,
     vol.Required(CONF_CONTROLLER_SEND_SERVICE): cv.entity_id,
     vol.Required(CONF_CONTROLLER_SEND_SERVICE): cv.entity_id,
-    vol.Required(CONF_CONTROLLER_COMMAND_TOPIC): cv.string,
+    vol.Optional(CONF_CONTROLLER_COMMAND_TOPIC): cv.string,
     vol.Optional(CONF_POWER_SENSOR): cv.entity_id
     vol.Optional(CONF_POWER_SENSOR): cv.entity_id
 })
 })
 
 
@@ -133,7 +133,7 @@ class SmartIRMediaPlayer(MediaPlayerDevice, RestoreEntity):
             self.hass,
             self.hass,
             self._supported_controller, 
             self._supported_controller, 
             self._commands_encoding,
             self._commands_encoding,
-            self._controller_send_service
+            self._controller_send_service,
             self._controller_command_topic)
             self._controller_command_topic)
 
 
     async def async_added_to_hass(self):
     async def async_added_to_hass(self):

+ 2 - 2
version.json

@@ -1,7 +1,7 @@
 {            
 {            
-    "version": "1.3.8",
+    "version": "1.3.9",
     "minHAVersion": "0.89.0b0",
     "minHAVersion": "0.89.0b0",
-    "releaseNotes": "A new version (1.3.8) is available that is compatible with your system. Call the ``smartir.update_component`` service to update the component.",
+    "releaseNotes": "A new version (1.3.9) is available that is compatible with your system. Call the ``smartir.update_component`` service to update the component.",
     "files": [
     "files": [
         "__init__.py",
         "__init__.py",
         "climate.py",
         "climate.py",