diff --git a/custom_components/infrarize/controller.py b/custom_components/infrarize/controller.py index 30758f6..b505322 100644 --- a/custom_components/infrarize/controller.py +++ b/custom_components/infrarize/controller.py @@ -1,3 +1,4 @@ +import asyncio from abc import ABC, abstractmethod from base64 import b64encode import binascii @@ -27,6 +28,8 @@ MQTT_COMMANDS_ENCODING = [ENC_RAW] LOOKIN_COMMANDS_ENCODING = [ENC_PRONTO, ENC_RAW] ESPHOME_COMMANDS_ENCODING = [ENC_RAW] +MQTT_SEND_STATES_KEY = 'infrarize_mqtt_send_states' + def get_controller(hass, controller, encoding, controller_data, delay): """Return a controller compatible with the specification provided.""" @@ -135,6 +138,12 @@ class XiaomiController(AbstractController): class MQTTController(AbstractController): """Controls a MQTT device.""" + def __init__(self, hass, controller, encoding, controller_data, delay): + super().__init__(hass, controller, encoding, controller_data, delay) + states = self.hass.data.setdefault(MQTT_SEND_STATES_KEY, {}) + self._send_state = states.setdefault(controller_data, MQTTTopicSendState()) + self._send_delay = float(delay) + def check_encoding(self, encoding): """Check if the encoding is supported by the controller.""" if encoding not in MQTT_COMMANDS_ENCODING: @@ -148,8 +157,42 @@ class MQTTController(AbstractController): 'payload': command } - await self.hass.services.async_call( - 'mqtt', 'publish', service_data) + async with self._send_state.lock: + loop = asyncio.get_running_loop() + now = loop.time() + + if ( + self._send_state.last_payload == command + and self._send_state.last_sent_at is not None + and now - self._send_state.last_sent_at < self._send_delay + ): + _LOGGER.debug( + "Skipping duplicate MQTT command for topic %s within %.3fs", + self._controller_data, + self._send_delay, + ) + return + + if self._send_state.last_sent_at is not None: + wait = self._send_delay - ( + now - self._send_state.last_sent_at + ) + if wait > 0: + await asyncio.sleep(wait) + + await self.hass.services.async_call( + 'mqtt', 'publish', service_data) + self._send_state.last_payload = command + self._send_state.last_sent_at = loop.time() + + +class MQTTTopicSendState: + """Shared send state for controllers publishing to the same topic.""" + + def __init__(self): + self.lock = asyncio.Lock() + self.last_payload = None + self.last_sent_at = None class LookinController(AbstractController): diff --git a/docs/CLIMATE.md b/docs/CLIMATE.md index b01df3d..fbd9522 100644 --- a/docs/CLIMATE.md +++ b/docs/CLIMATE.md @@ -13,7 +13,7 @@ _Please note that the device_code field only accepts positive numbers. The .json | `unique_id` | string | optional | An ID that uniquely identifies this device. If two devices have the same unique ID, Home Assistant will raise an exception. | | `device_code` | number | required | (Accepts only positive numbers) | | `controller_data` | string | required | The data required for the controller to function. Enter the entity_id of the Broadlink remote **(must be an already configured device)**, or the entity id of the Xiaomi IR controller, or the MQTT topic on which to send commands. | -| `delay` | number | optional | Adjusts the delay in seconds between multiple commands. The default is 0.5 | +| `delay` | number | optional | Adjusts the delay in seconds between multiple commands. MQTT controllers also pace commands by topic and suppress identical payloads sent during this interval. Increase it for IR blasters with long Zigbee transmissions. The default is 0.5. | | `temperature_sensor` | string | optional | *entity_id* for a temperature sensor | | `humidity_sensor` | string | optional | *entity_id* for a humidity sensor | | `power_sensor` | string | optional | *entity_id* for a sensor that monitors whether your device is actually `on` or `off`. This may be a power monitor sensor. (Accepts only on/off states) | diff --git a/docs/FAN.md b/docs/FAN.md index e10007a..6390df2 100644 --- a/docs/FAN.md +++ b/docs/FAN.md @@ -11,7 +11,7 @@ Find your device's brand code [here](FAN.md#available-codes-for-fan-devices) and **unique_id** (Optional): An ID that uniquely identifies this device. If two devices have the same unique ID, Home Assistant will raise an exception.
**device_code** (Required): ...... (Accepts only positive numbers)
**controller_data** (Required): The data required for the controller to function. Enter the entity_id of the Broadlink remote (must be an already configured device), or the entity id of the Xiaomi IR controller, or the MQTT topic on which to send commands.
-**delay** (Optional): Adjusts the delay in seconds between multiple commands. The default is 0.5
+**delay** (Optional): Adjusts the delay in seconds between multiple commands. MQTT controllers also pace commands by topic and suppress identical payloads sent during this interval. Increase it for long Zigbee IR transmissions. The default is 0.5
**power_sensor** (Optional): *entity_id* for a sensor that monitors whether your device is actually On or Off. This may be a power monitor sensor. (Accepts only on/off states)
## Example (using broadlink controller) diff --git a/docs/LIGHT.md b/docs/LIGHT.md index 59a298d..bfb9204 100644 --- a/docs/LIGHT.md +++ b/docs/LIGHT.md @@ -11,7 +11,7 @@ Find your device's brand code [here](LIGHT.md#available-codes-for-light-devices) **unique_id** (Optional): An ID that uniquely identified this device. If two devices have the same unique ID, Home Assistant will raise an exception.
**device_code** (Required): ...... (Accepts only positive numbers)
**controller_data** (Required): The data required for the controller to function. Enter the entity_id of the Broadlink or Xiaomi IR controller, or the MQTT topic on which to send commands.
-**delay** (Optional): Adjusts the delay in seconds between multiple commands. The default is 0.5
+**delay** (Optional): Adjusts the delay in seconds between multiple commands. MQTT controllers also pace commands by topic and suppress identical payloads sent during this interval. Increase it for long Zigbee IR transmissions. The default is 0.5
**power_sensor** (Optional): *entity_id* for a sensor that monitors whether your device is actually On or Off. This may be a power monitor sensor. (Accepts only on/off states)
## Example (using broadlink controller) diff --git a/docs/MEDIA_PLAYER.md b/docs/MEDIA_PLAYER.md index 001360a..451235f 100644 --- a/docs/MEDIA_PLAYER.md +++ b/docs/MEDIA_PLAYER.md @@ -10,7 +10,7 @@ Find your device's brand code [here](MEDIA_PLAYER.md#available-codes-for-tv-devi **unique_id** (Optional): An ID that uniquely identifies this device. If two devices have the same unique ID, Home Assistant will raise an exception.
**device_code** (Required): ...... (Accepts only positive numbers)
**controller_data** (Required): The data required for the controller to function. Enter the IP address of the Broadlink device **(must be an already configured device)**, or the entity id of the Xiaomi IR controller, or the MQTT topic on which to send commands.
-**delay** (Optional): Adjusts the delay in seconds between multiple commands. The default is 0.5
+**delay** (Optional): Adjusts the delay in seconds between multiple commands. MQTT controllers also pace commands by topic and suppress identical payloads sent during this interval. Increase it for long Zigbee IR transmissions. The default is 0.5
**power_sensor** (Optional): *entity_id* for a sensor that monitors whether your device is actually On or Off. This may be a power monitor sensor. (Accepts only on/off states)
**source_names** (Optional): Override the names of sources as displayed in HomeAssistant (see below)