TV control with alexa skill

Este artículo se puede leer en castellano aqui

4 Years ago I was given a Telefunken branded “smart” TV like this

Doing a little of “hacking” over it, I discovered that this is a “remarked” Vestel TV which uses a Linux OS and can be controlled using this Android APP:

This year, I was given a Alexa Echo Dot device as a present for my birthday and I tried to control every electronic device in my home using this element.

Using Fiddler as proxy for the Vestel app, I made “reverse engineering” over the commands sent by the app and I replicate them by use of python scripts.

Finnally using Flask-Ask library for Python, which I forked here I generate a private Alexa Skill that allows me to control my TV using voice.

Python code

Code for this skill is public and accessible through this repository . As can be seen it is launched using a systemd service.

The actions and the channel names have been inserted into lists. There are 3 special channels, TDT that sets TV to receive DVB-T channels, Kodi that changes the input of the TV to the HDMI that is connected to a raspberry running Kodi, and Satelite which changes the input of the TV to a DVB-S2 receiver HDMI.

The remote keys and the channel codes are inserted into a dictionary.

The TV commands and channels setting execution, first involve a discover step , in which the skill makes use of SSDP protocol to get the IP and Port of the TV.

As can be seen, there is also another handler for “intentluz” , this is because I run this script on a Raspberry Pi that with use of Hyperion, generates an ambilight effect with every media played with the Raspberry or the Satellite receiver.

The last setting about this backend code is that there is an nginx service running on raspberry that handles with HTTPS TLS layer and redirects requests to internal port 5000 where the flask framework waits for connections.

  server { 
  listen 443 ssl; 
  server_name <DDNS host>; 
  ssl_certificate <Generated public key> ;
  ssl_certificate_key <Generated private key> ;
  ssl_protocols TLSv1.2; ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4; 
  ssl_prefer_server_ciphers on; 
  location / { 
	  proxy_pass http://127.0.0.1:5000/; 
	  proxy_http_version 1.1; 
	  proxy_set_header Upgrade $http_upgrade; 
	  proxy_set_header Connection ‘upgrade’; 
	  proxy_set_header Host $host; 
	  proxy_cache_bypass $http_upgrade; 
	  proxy_read_timeout 120s; 
	} 
  }

Alexa Skill

I used “la tele” as skill invocation name so it becomes a very natural way to ask alexa to do something over my TV:

“Alexa dile a la tele que suba el volumen”

I create 2 intents, one for hyperion ON/OFF handling and the other for TV commands and channel changing.

I created 4 sample utterances and 2 intent slots (with 1 slot type each) for TV Intent. One of the slots is for actions and the other is for channels. Note that the slot type values must match the elements in python lists. Synonims can be used to match Alexa’s voice service resolved strings to our elements.

Once this is configured, a critical item is that the Endpoint must be configured to point to our DDNS host. A pair of public and private keys must be generated and the public certificate must be uploaded. Since we are not going to publish this alexa skill, self generated keys and certificates are valid.

Regarding privacy and protection, if we do not publish our skill only our Alexa devices will be able to access it.

  • Type: Alexa Skill
  • Where: Raspberry pi 4 B
  • Languages and technologies used: Python, flask, Alexa
  • Github repo: yes

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *