Convert Channels platform services to use platform register (#38827)

This commit is contained in:
Paulus Schoutsen 2020-08-13 11:08:59 +02:00 committed by GitHub
parent b3571602bb
commit 7343649c54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,7 +20,6 @@ from homeassistant.components.media_player.const import (
SUPPORT_VOLUME_MUTE,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_HOST,
CONF_NAME,
CONF_PORT,
@ -28,9 +27,9 @@ from homeassistant.const import (
STATE_PAUSED,
STATE_PLAYING,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import config_validation as cv, entity_platform
from .const import DOMAIN, SERVICE_SEEK_BACKWARD, SERVICE_SEEK_BY, SERVICE_SEEK_FORWARD
from .const import SERVICE_SEEK_BACKWARD, SERVICE_SEEK_BY, SERVICE_SEEK_FORWARD
_LOGGER = logging.getLogger(__name__)
@ -61,58 +60,22 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
# 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):
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)
if DATA_CHANNELS not in hass.data:
hass.data[DATA_CHANNELS] = []
platform = entity_platform.current_platform.get()
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
platform.async_register_entity_service(
SERVICE_SEEK_FORWARD, {}, "seek_forward",
)
hass.services.register(
DOMAIN, SERVICE_SEEK_BACKWARD, service_handler, schema=CHANNELS_SCHEMA
platform.async_register_entity_service(
SERVICE_SEEK_BACKWARD, {}, "seek_backward",
)
hass.services.register(
DOMAIN, SERVICE_SEEK_BY, service_handler, schema=CHANNELS_SEEK_BY_SCHEMA
platform.async_register_entity_service(
SERVICE_SEEK_BY, {vol.Required(ATTR_SECONDS): vol.Coerce(int)}, "seek_by",
)