<commit_before>"""Support for interfacing with an instance of getchannels.com."""
import logging

from pychannels import Channels
import voluptuous as vol

from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
from homeassistant.components.media_player.const import (
    MEDIA_TYPE_CHANNEL,
    MEDIA_TYPE_EPISODE,
    MEDIA_TYPE_MOVIE,
    MEDIA_TYPE_TVSHOW,
    SUPPORT_NEXT_TRACK,
    SUPPORT_PAUSE,
    SUPPORT_PLAY,
    SUPPORT_PLAY_MEDIA,
    SUPPORT_PREVIOUS_TRACK,
    SUPPORT_SELECT_SOURCE,
    SUPPORT_STOP,
    SUPPORT_VOLUME_MUTE,
)
from homeassistant.const import (
    ATTR_ENTITY_ID,
    CONF_HOST,
    CONF_NAME,
    CONF_PORT,
    STATE_IDLE,
    STATE_PAUSED,
    STATE_PLAYING,
)
import homeassistant.helpers.config_validation as cv

from .const import DOMAIN, SERVICE_SEEK_BACKWARD, SERVICE_SEEK_BY, SERVICE_SEEK_FORWARD

_LOGGER = logging.getLogger(__name__)

DATA_CHANNELS = "channels"
DEFAULT_NAME = "Channels"
DEFAULT_PORT = 57000

FEATURE_SUPPORT = (
    SUPPORT_PLAY
    | SUPPORT_PAUSE
    | SUPPORT_STOP
    | SUPPORT_VOLUME_MUTE
    | SUPPORT_NEXT_TRACK
    | SUPPORT_PREVIOUS_TRACK
    | SUPPORT_PLAY_MEDIA
    | SUPPORT_SELECT_SOURCE
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_HOST): cv.string,
        vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
        vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
    }
)


# Service call validation schemas
ATTR_SECONDS = "seconds"

CHANNELS_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_id})

CHANNELS_SEEK_BY_SCHEMA = CHANNELS_SCHEMA.extend(
    {vol.Required(ATTR_SECONDS): vol.Coerce(int)}
)


def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Channels platform."""
    device = ChannelsPlayer(config[CONF_NAME], config[CONF_HOST], config[CONF_PORT])

    if DATA_CHANNELS not in hass.data:
        hass.data[DATA_CHANNELS] = []

    add_entities([device], True)
    hass.data[DATA_CHANNELS].append(device)

    def service_handler(service):
        """Handle service."""
        entity_id = service.data.get(ATTR_ENTITY_ID)

        device = next(
            (
                device
                for device in hass.data[DATA_CHANNELS]
                if device.entity_id == entity_id
            ),
            None,
        )

        if device is None:
            _LOGGER.warning("Unable to find Channels with entity_id: %s", entity_id)
            return

        if service.service == SERVICE_SEEK_FORWARD:
            device.seek_forward()
        elif service.service == SERVICE_SEEK_BACKWARD:
            device.seek_backward()
        elif service.service == SERVICE_SEEK_BY:
            seconds = service.data.get("seconds")
            device.seek_by(seconds)

    hass.services.register(
        DOMAIN, SERVICE_SEEK_FORWARD, service_handler, schema=CHANNELS_SCHEMA
    )

    hass.services.register(
        DOMAIN, SERVICE_SEEK_BACKWARD, service_handler, schema=CHANNELS_SCHEMA
    )

    hass.services.register(
        DOMAIN, SERVICE_SEEK_BY, service_handler, schema=CHANNELS_SEEK_BY_SCHEMA
    )


class ChannelsPlayer(MediaPlayerEntity):
    """Representation of a Channels instance."""

    def __init__(self, name, host, port):
        """Initialize the Channels app."""<commit_msg>Convert Channels platform services to use platform register (#38827)<commit_after>"""Support for interfacing with an instance of getchannels.com."""
import logging

from pychannels import Channels
import voluptuous as vol

from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
from homeassistant.components.media_player.const import (
    MEDIA_TYPE_CHANNEL,
    MEDIA_TYPE_EPISODE,
    MEDIA_TYPE_MOVIE,
    MEDIA_TYPE_TVSHOW,
    SUPPORT_NEXT_TRACK,
    SUPPORT_PAUSE,
    SUPPORT_PLAY,
    SUPPORT_PLAY_MEDIA,
    SUPPORT_PREVIOUS_TRACK,
    SUPPORT_SELECT_SOURCE,
    SUPPORT_STOP,
    SUPPORT_VOLUME_MUTE,
)
from homeassistant.const import (
    CONF_HOST,
    CONF_NAME,
    CONF_PORT,
    STATE_IDLE,
    STATE_PAUSED,
    STATE_PLAYING,
)
from homeassistant.helpers import config_validation as cv, entity_platform

from .const import SERVICE_SEEK_BACKWARD, SERVICE_SEEK_BY, SERVICE_SEEK_FORWARD

_LOGGER = logging.getLogger(__name__)

DATA_CHANNELS = "channels"
DEFAULT_NAME = "Channels"
DEFAULT_PORT = 57000

FEATURE_SUPPORT = (
    SUPPORT_PLAY
    | SUPPORT_PAUSE
    | SUPPORT_STOP
    | SUPPORT_VOLUME_MUTE
    | SUPPORT_NEXT_TRACK
    | SUPPORT_PREVIOUS_TRACK
    | SUPPORT_PLAY_MEDIA
    | SUPPORT_SELECT_SOURCE
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_HOST): cv.string,
        vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
        vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
    }
)


# Service call validation schemas
ATTR_SECONDS = "seconds"


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Set up the Channels platform."""
    device = ChannelsPlayer(config[CONF_NAME], config[CONF_HOST], config[CONF_PORT])
    async_add_entities([device], True)

    platform = entity_platform.current_platform.get()

    platform.async_register_entity_service(
        SERVICE_SEEK_FORWARD, {}, "seek_forward",
    )
    platform.async_register_entity_service(
        SERVICE_SEEK_BACKWARD, {}, "seek_backward",
    )
    platform.async_register_entity_service(
        SERVICE_SEEK_BY, {vol.Required(ATTR_SECONDS): vol.Coerce(int)}, "seek_by",
    )


class ChannelsPlayer(MediaPlayerEntity):
    """Representation of a Channels instance."""

    def __init__(self, name, host, port):
        """Initialize the Channels app."""
{'fraction_non_alphanumeric': 0.08355274705985606, 'fraction_numerical': 0.0026329647182727752, 'mean_word_length': 3.6935749588138385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4548484', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1850, 'n_words': 343}