Unverified Commit 4fb66619 authored by Diogo Gomes's avatar Diogo Gomes Committed by GitHub
Browse files

Add play_media service (#503)

* add play_media service

* advertise support

* add documentation
parent 444c9f9d
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ from homeassistant.components.media_player import (
from homeassistant.components.media_player.const import (
    SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_PREVIOUS_TRACK,
    SUPPORT_NEXT_TRACK, SUPPORT_VOLUME_STEP, SUPPORT_VOLUME_MUTE, 
    SUPPORT_SELECT_SOURCE, MEDIA_TYPE_CHANNEL)
    SUPPORT_PLAY_MEDIA, SUPPORT_SELECT_SOURCE, MEDIA_TYPE_CHANNEL)
from homeassistant.const import (
    CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN)
import homeassistant.helpers.config_validation as cv
@@ -127,7 +127,7 @@ class SmartIRMediaPlayer(MediaPlayerEntity, RestoreEntity):
            self._support_flags = self._support_flags | SUPPORT_VOLUME_MUTE

        if 'sources' in self._commands and self._commands['sources'] is not None:
            self._support_flags = self._support_flags | SUPPORT_SELECT_SOURCE
            self._support_flags = self._support_flags | SUPPORT_SELECT_SOURCE | SUPPORT_PLAY_MEDIA

            for source, new_name in config.get(CONF_SOURCE_NAMES, {}).items():
                if source in self._commands['sources']:
@@ -266,6 +266,23 @@ class SmartIRMediaPlayer(MediaPlayerEntity, RestoreEntity):
        await self.send_command(self._commands['sources'][source])
        await self.async_update_ha_state()

    async def async_play_media(self, media_type, media_id, **kwargs):
        """Support channel change through play_media service."""
        if self._state == STATE_OFF:
            await self.async_turn_on()

        if media_type != MEDIA_TYPE_CHANNEL:
            _LOGGER.error("invalid media type")
            return
        if not media_id.isdigit():
            _LOGGER.error("media_id must be a channel number")
            return

        self._source = "Channel {}".format(media_id)
        for digit in media_id:
            await self.send_command(self._commands['sources']["Channel {}".format(digit)])
        await self.async_update_ha_state()

    async def send_command(self, command):
        async with self._temp_lock:
            try:
+12 −0
Original line number Diff line number Diff line
@@ -123,6 +123,18 @@ media_player:
      VGA: null
```

### Changing channels
Most IR remotes can only send one key at a time (0 to 9) to change your TV channel, changing to other channels requires pressing 2 consecutive keys. SmartIR handles any channel for you with the standard Home Assistant service interface. Here is an example that changes your Kitchen TV to channel 51:

```yaml
service: media_player.play_media
target:
  entity_id: media_player.kitchen_tv
data:
  media_content_id: 51
  media_content_type: "channel"
```

## Available codes for TV 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.