smartHomeHub / SmartIR

Integration for Home Assistant to control climate, TV and fan devices via IR/RF controllers (Broadlink, Xiaomi, MQTT, LOOKin, ESPHome)
MIT License
2.02k stars 996 forks source link

Hex codes for Esphome #958

Open vashilov opened 1 year ago

vashilov commented 1 year ago

Sorry for my bad english.

I try add my stupid tv Samsung into smartir. I have only HEX codes for my TV.

Home Assistant version 2022.10.4

SmartIR version 1.17.6

1) create new service send_samsung_command in my esp_home SmartIR configuration

esphome:
  name: esp_03
  platform: ESP8266
  board: nodemcuv2
api:
  password: !secret esp_password
  services:
    - service: send_raw_command
      variables:
        command: int[]
      then:
        - remote_transmitter.transmit_raw:
            code: !lambda 'return command;'
    - service: send_samsung_command
      variables:
        command: string
      then:
        - remote_transmitter.transmit_samsung:
            data: !lambda 'return strtoll(command.c_str(), NULL, 16);'

From HA this service work, and switсh on my TV. (code 0xE0E040BF)

2) in HA

smartir:

media_player:
  - platform: smartir
    name: Living room TV
    unique_id: living_room_tv
    device_code: 7061
    controller_data: esp_03_send_samsung_command
    power_sensor: input_boolean.led_40

3) cretae codes/media_player/7061.json (Raw, but need Hex)

{
    "manufacturer": "Samsung",
    "supportedModels": [
      "UA42J5200"
    ],
    "supportedController": "ESPHome",
    "commandsEncoding": "Raw",
    "commands": {
        "off": "0xE0E040BF", 
        "on": "0xE0E040BF",
        "previousChannel": "0xE0E008F7",
        "nextChannel": "0xE0E048B7",
        "volumeDown": "E0E0D02F",
        "volumeUp": "E0E0E01F",
        "mute": "0xE0E0F00F",
        "sources": {
            "Channel 0": "0xE0E0807F",
            "Channel 1": "0xE0E0807F",
            "Channel 2": "0xE0E0807F",
            "Channel 3": "0xE0E0807F",
            "Channel 4": "0xE0E0807F",
            "Channel 5": "0xE0E0807F",
            "Channel 6": "0xE0E0807F",
            "Channel 7": "0xE0E0807F",
            "Channel 8": "0xE0E0807F",
            "Channel 9": "0xE0E0807F",
            "Input": "0xE0E0807F"
        }
    }
}

Then I try push button on media_player I have error

Debug log


Extra data: line 1 column 2 (char 1)

Traceback (most recent call last):
  File "/config/custom_components/smartir/media_player.py", line 289, in send_command
    await self._controller.send(command)
  File "/config/custom_components/smartir/controller.py", line 183, in send
    service_data = {'command':  json.loads(command)}
  File "/usr/local/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.10/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2 (char 1)

Please help me, i do not have raw codes from my TV. What i do wrong? I need example json file for Hex codes.

vashilov commented 1 year ago

7061.json

{
    "manufacturer": "Samsung",
    "supportedModels": [
      "UA42J5200"
    ],
    "supportedController": "ESPHome",
    "commandsEncoding": "Hex",
    "commands": {
        "off": "0xE0E040BF", 
        "on": "0xE0E040BF",
        "previousChannel": "0xE0E008F7",
        "nextChannel": "0xE0E048B7",
        "volumeDown": "E0E0D02F",
        "volumeUp": "E0E0E01F",
        "mute": "0xE0E0F00F",
        "sources": {
            "Channel 0": "0xE0E020DF",
            "Channel 1": "0xE0E0A05F",
            "Channel 2": "0xE0E0609F",
            "Channel 3": "0xE0E010EF",
            "Channel 4": "0xE0E0906F",
            "Channel 5": "0xE0E050AF",
            "Channel 6": "0xE0E0B04F",
            "Channel 7": "0xE0E0708F",
            "Channel 8": "0xE0E0807F",
            "Channel 9": "0xE0E08877",
            "Input": "0xE0E0807F"
        }
    }
}

HA

media_player:
  - platform: smartir
    name: Living room TV
    unique_id: living_room_tv
    device_code: 7061
    delay: 1
    controller_data: esp_03_send_samsung_command

EspHome

substitutions:
  device_name: Esp_03
  name: esp_03

esphome:
  name: ${name}
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: !secret wifi_name_home
  password: !secret wifi_password_home

  ap:
    ssid: "wifi_name Fallback Hotspot"
    password: !secret esp_password_wifi

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret esp_password
  services:
    - service: send_raw_command
      variables:
        command: int[]
      then:
        - remote_transmitter.transmit_raw:
            code: !lambda 'return command;'
            carrier_frequency: 37000

    - service: send_samsung_command
      variables:
        command: string
      then:
        - remote_transmitter.transmit_samsung:
            data: !lambda 'return strtoll(command.c_str(), NULL, 16);'
ota:
  password: !secret esp_password

remote_transmitter:
  pin: 
    number: D3
    inverted: false
  id: trs
  carrier_duty_percent: 50%

controller.py

ESPHOME_COMMANDS_ENCODING = [ENC_RAW, ENC_HEX]
...
...
...
class ESPHomeController(AbstractController):
    """Controls a ESPHome 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."""
        if self._encoding == ENC_RAW:
            try:
                service_data = {'command':  json.loads(command)}
            except:
                raise Exception("Error while send raw")

        if self._encoding == ENC_HEX:
            try:
                service_data = {'command':  command}
            except:
                raise Exception("Error while send hex")

        await self.hass.services.async_call(
            'esphome', self._controller_data, service_data)
vashilov commented 1 year ago

It work! for me))

ZerefDeLaValier commented 1 year ago

Thank you very much! It helps to me too!