Samsung Smart TV - No control?

I don’t know if anyone is watching this thread but I came across it last night and it motivated me to dig into the home assistant code to write a custom component on top of samsungtv to give me access to the “send_key” functionality that I think @thmry was alluding to.

The service is called media_player.send_key and the service data looks like this:

{
  "entity_id": "media_player.samsung_tv",
  "key_code": "KEY_HOME"
}

You need to add the custom component to configuration.yaml the same way you would with a samsungtv.
BE WARNED!
This component essentially replaces samsungtv. I wouldn’t recommend having both in your configuration.

media_player:
  - platform: samsungtv_custom

Finally, put the following code in custom_components/media_player/samsungtv_custom.py. I didn’t name it samsungtv.py because I wanted to re-use as much code as possible and I’m not familiar enough with python to know how to do that properly.

"""
Custom component to allow overriding or adding features to the samsungtv media_player component.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.samsungtv/
"""

import logging

import voluptuous as vol

import homeassistant.components.media_player.samsungtv as stv
from homeassistant.components.media_player import (DOMAIN)
from homeassistant.const import (ATTR_ENTITY_ID)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

SAMSUNG_TV_CUSTOM_DATA = 'samsungtv_custom'

SERVICE_KEY = 'send_key'

# Service call validation schemas
ATTR_KEY = 'key_code'

SAMSUNG_TV_CUSTOM_KEY_SCHEMA = vol.Schema({
    vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
    vol.Required(ATTR_KEY): cv.string,
})

def setup_platform(hass, config, add_devices, discovery_info=None):
    if SAMSUNG_TV_CUSTOM_DATA not in hass.data:
        hass.data[SAMSUNG_TV_CUSTOM_DATA] = []

    # Use this to get my hands on the SamsungTVDevices that get added
    def add_devices_custom(devices):
        add_devices(devices)
        for device in devices:
            hass.data[SAMSUNG_TV_CUSTOM_DATA].append(device)
    
    # pass in my add_devices_custom function
    stv.setup_platform(hass, config, add_devices_custom, discovery_info)    
    _LOGGER.debug("hass.data[SAMSUNG_TV_CUSTOM_DATA] = %s.", hass.data[SAMSUNG_TV_CUSTOM_DATA])
    
    def service_handle(service):
        _LOGGER.debug("service_handle called for %s with %s", service.service, service.data)
        entity_ids = service.data.get('entity_id')
        devices = hass.data[SAMSUNG_TV_CUSTOM_DATA]
        
        for device in devices:
            if device.entity_id in entity_ids:
                if service.service == SERVICE_KEY:
                    device.send_key(service.data.get(ATTR_KEY))
                    
                    device.schedule_update_ha_state(True)

    hass.services.register(
        DOMAIN, SERVICE_KEY, service_handle,
        schema=SAMSUNG_TV_CUSTOM_KEY_SCHEMA)
6 Likes