From 9cf372f69643e8245113f43697bd781a80dbc01f Mon Sep 17 00:00:00 2001 From: Angel Marin Date: Fri, 22 Dec 2023 10:16:26 +0100 Subject: [PATCH] add support for ZHA zigbee IR remotes as controller --- custom_components/smartir/controller.py | 30 +++++++++++++++++++++++-- docs/CLIMATE.md | 24 +++++++++++++++++++- docs/FAN.md | 23 ++++++++++++++++++- docs/MEDIA_PLAYER.md | 22 +++++++++++++++++- docs/README.md | 1 + info.md | 1 + 6 files changed, 96 insertions(+), 5 deletions(-) diff --git a/custom_components/smartir/controller.py b/custom_components/smartir/controller.py index 30758f62b..ede33b793 100644 --- a/custom_components/smartir/controller.py +++ b/custom_components/smartir/controller.py @@ -15,6 +15,7 @@ MQTT_CONTROLLER = 'MQTT' LOOKIN_CONTROLLER = 'LOOKin' ESPHOME_CONTROLLER = 'ESPHome' +ZHA_CONTROLLER = 'zha' ENC_BASE64 = 'Base64' ENC_HEX = 'Hex' @@ -35,7 +36,8 @@ def get_controller(hass, controller, encoding, controller_data, delay): XIAOMI_CONTROLLER: XiaomiController, MQTT_CONTROLLER: MQTTController, LOOKIN_CONTROLLER: LookinController, - ESPHOME_CONTROLLER: ESPHomeController + ESPHOME_CONTROLLER: ESPHomeController, + ZHA_CONTROLLER: ZhaController } try: return controllers[controller](hass, controller, encoding, controller_data, delay) @@ -183,4 +185,28 @@ async def send(self, command): service_data = {'command': json.loads(command)} await self.hass.services.async_call( - 'esphome', self._controller_data, service_data) \ No newline at end of file + 'esphome', self._controller_data, service_data) + + +class ZhaController(AbstractController): + """Controls a ZHA device.""" + + def check_encoding(self, encoding): + """Check if the encoding is supported by the controller.""" + if encoding not in ESPHOME_COMMANDS_ENCODING: + raise Exception("The encoding is not supported " + "by the ESPHome controller.") + + async def send(self, command): + """Send a command.""" + service_data = json.loads(self._controller_data) + if not isinstance(service_data, dict): + raise Exception("Wrong json config for ZHA controller") + for k in ['ieee', 'endpoint_id', 'cluster_id', 'cluster_type', 'command']: + if not service_data.get(k): + raise Exception(f"Missing {k} parameter in config for ZHA controller") + service_data['params'] = { + 'code': command, + } + await self.hass.services.async_call( + 'zha', 'issue_zigbee_cluster_command', service_data) diff --git a/docs/CLIMATE.md b/docs/CLIMATE.md index 352f495e8..fa39b4cf7 100644 --- a/docs/CLIMATE.md +++ b/docs/CLIMATE.md @@ -12,7 +12,7 @@ _Please note that the device_code field only accepts positive numbers. The .json | `name` | string | optional | The name of the device | | `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. | +| `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 or the ZHA zigbee cluster to send commands to. | | `delay` | number | optional | Adjusts the delay in seconds between multiple commands. 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 | @@ -123,6 +123,28 @@ climate: power_sensor: binary_sensor.ac_power ``` +## Example (using ZHA controller and a TuYa ZS06): +```yaml +smartir: + +climate: + - platform: smartir + name: Office AC + unique_id: office_ac + device_code: 9000 + controller_data: '{ + "ieee":"XX:XX:XX:XX:XX:XX:XX:XX", + "endpoint_id": 1, + "cluster_id": 57348, + "cluster_type": "in", + "command": 2, + "command_type": "server" + }' + temperature_sensor: sensor.temperature + humidity_sensor: sensor.humidity + power_sensor: binary_sensor.ac_power +``` + ## Available codes for climate devices: The following are the code files created by the amazing people in the community. Before you start creating your own code file, try if one of them works for your device. **Please open an issue if your device is working and not included in the supported models.** Contributing to your own code files is welcome. However, we do not accept incomplete files as well as files related to MQTT controllers. diff --git a/docs/FAN.md b/docs/FAN.md index a836a30c9..bbfb7ff3b 100644 --- a/docs/FAN.md +++ b/docs/FAN.md @@ -10,7 +10,7 @@ Find your device's brand code [here](FAN.md#available-codes-for-fan-devices) and **name** (Optional): The name of the device
**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.
+**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 or the ZHA zigbee cluster to send commands to.
**delay** (Optional): Adjusts the delay in seconds between multiple commands. 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)
@@ -115,6 +115,27 @@ fan: power_sensor: binary_sensor.fan_power ``` +## Example (using ZHA controller and a TuYa ZS06): + +```yaml +smartir: + +fan: + - platform: smartir + name: Bedroom fan + unique_id: bedroom_fan + device_code: 5000 + controller_data: '{ + "ieee":"XX:XX:XX:XX:XX:XX:XX:XX", + "endpoint_id": 1, + "cluster_id": 57348, + "cluster_type": "in", + "command": 2, + "command_type": "server" + }' + power_sensor: binary_sensor.fan_power +``` + ## Available codes for Fan devices The following are the code files created by the amazing people in the community. Before you start creating your own code file, try if one of them works for your device. **Please open an issue if your device is working and not included in the supported models.** diff --git a/docs/MEDIA_PLAYER.md b/docs/MEDIA_PLAYER.md index 8da2b3ba3..8e9f94c97 100644 --- a/docs/MEDIA_PLAYER.md +++ b/docs/MEDIA_PLAYER.md @@ -9,7 +9,7 @@ Find your device's brand code [here](MEDIA_PLAYER.md#available-codes-for-tv-devi **name** (Optional): The name of the device
**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.
+**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 or the ZHA zigbee cluster to send commands to.
**delay** (Optional): Adjusts the delay in seconds between multiple commands. 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)
@@ -107,6 +107,26 @@ media_player: power_sensor: binary_sensor.tv_power ``` +## Example (using ZHA controller and a TuYa ZS06): +```yaml +smartir: + +media_player: + - platform: smartir + name: Living room TV + unique_id: living_room_tv + device_code: 5000 + controller_data: '{ + "ieee":"XX:XX:XX:XX:XX:XX:XX:XX", + "endpoint_id": 1, + "cluster_id": 57348, + "cluster_type": "in", + "command": 2, + "command_type": "server" + }' + power_sensor: binary_sensor.tv_power +``` + ### Overriding Source Names Source names in device files are usually set to the name that the media player uses. These often aren't very descriptive, so you can override these names in the configuration file. You can also remove a source by setting its name to `null`. diff --git a/docs/README.md b/docs/README.md index de2a4ac46..63a944122 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,6 +8,7 @@ SmartIR currently supports the following controllers: * [LOOK.in Remote](http://look-in.club/devices/remote) * [ESPHome User-defined service for remote transmitter](https://esphome.io/components/api.html#user-defined-services) * [MQTT Publish service](https://www.home-assistant.io/docs/mqtt/service/) +* [ZHA Zigbee IR remote](https://www.home-assistant.io/integrations/zha/) More than 120 climate devices are currently supported out-of-the-box, mainly for the Broadlink controller, thanks to our awesome community.

Don't forget to **star** the repository if you had fun!

diff --git a/info.md b/info.md index f7e9ef3e0..4dc759761 100644 --- a/info.md +++ b/info.md @@ -5,6 +5,7 @@ SmartIR currently supports the following controllers: * [LOOK.in Remote](http://look-in.club/devices/remote) * [ESPHome User-defined service for remote transmitter](https://esphome.io/components/api.html#user-defined-services) * [MQTT Publish service](https://www.home-assistant.io/docs/mqtt/service/) +* [ZHA Zigbee IR remote](https://www.home-assistant.io/integrations/zha/) More than 120 climate devices are currently supported out-of-the-box, mainly for the Broadlink controller, thanks to our awesome community.
Please don't forget to [**star**](https://github.com/smartHomeHub/SmartIR/) the repository if you had fun! [**"Buy Me A Coffee**"](https://www.buymeacoffee.com/vassilis) is also welcome. It will help in further development.