"Ok Google, Channel 154" with Broadlink

I’m a HA noob and this is my first little project. And I’m french so sorry for my english level…

To change to any channel I needed to simulate multiple button press with a 1sec delay between each.
Here how I achieved this (I think there is lots of improvements to make, do not hesitate to comment :slight_smile:

1 - I made a script for each number of my tv’s remote (0 to 9):

tv_0:
  alias: TV - 0
  sequence:
    - service: broadlink.send_packet_xxx_xxx_xxx_xxx
      data:
        packet: 
          - "JgBQAAABJZISOBITExITEhMSEhMSExITEzcSExI4EjgSOBM3EzcTNxMSEzcTEhMSEzcTEhMSEzcTNxMSEzcUNRUREzcTNxQRFAAFKgABJUkSAA0FAAAAAAAAAAA="
[......]
tv_9:
  alias: TV - 9
  sequence:
    - service: broadlink.send_packet_xxx_xxx_xxx_xxx

2 - I made an IFTTT recipe : Google assistant : “Channel #” that calls a script.change_channel service with the number as a “channel” variable:

change_channel:
  alias: "Changement de chaine"
  sequence:
    - service: python_script.set_channel
      data_template:
        channel: "{{channel}}"

3 - The Python script component set_channel.py is just here to fire an event (this is the worst part of my workflow…) :

channel = data.get('channel')
hass.bus.fire('change_channel', {'channel': channel})

4 - The event change_channel is catched by an appdaemon script set_channel.py (yes, same name… :blush: ):

import appdaemon.appapi as appapi

#
# Change Channel on TV
# To call it, fire a change_channel event with a "channel" json param
#
# Args :
# prefix: remote's button scripts format (exemple : "prefix: lg_channel_" will call script.lg_channel_0 to script.lg_channel_9)
# 

class SetChannel(appapi.AppDaemon):
    def initialize(self):
        self.log("SetChannel loaded")
        self.listen_event(self.do_change, 'change_channel')

    def do_change(self, event_name, data, kwargs): 
        self.log("Changing channel for {}".format(data["channel"]))
        delay = 0
        for c in str(data["channel"]):
            if delay == 0:
                self.do_channel({"channel": c}) # Got a bug with run_in if delay = 0...
            else:
                self.run_in(self.do_channel, delay, channel = c)
            delay += 1

    def do_channel(self, kwargs):
        target = "script.{}{}".format(self.args["prefix"], kwargs["channel"])
        self.log("Pressing remote button {} - call script {}".format(kwargs["channel"], target))
        self.turn_on(target)

My module config:

set channel:
  module: set_channel
  class: SetChannel
  prefix: tv_ 

And yeaaaah !
It’s working for any channel :hugs:
Not much, but proud of it :smiley:

cheers

12 Likes

Good job bro

Awesome! Markdown!

Thanks for the share and welcome here (French too myself :wink:

Thx guys !
My stepmother is in love with Google Home just because of this :slight_smile:

@vincen : Yeah ! :sunglasses:

Hi, does this mean I need to create applet for each of my favourite tv channels?

Could you give an example on how I should generate the applet?
Particularly what I need to add on the ‘body’ section

Could you also specify where I need to place the script in /.homeassistant folder?

@manthoR What does your webhook look like in IFTTT?