# Path: pulseaudio/const.py
# DOMAIN = "pulseaudio"
# 
# CONF_SINK = 'sink'


import logging
import asyncio
import shlex
import voluptuous as vol
from subprocess import PIPE, Popen
from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_NAME
from .const import DOMAIN, CONF_SINK   # pylint:disable=unused-import
"""Config flow for Pulse Audio integration."""



_LOGGER = logging.getLogger(__name__)

async def get_sinks():
    cmd = "pactl list short sinks"
    proc = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE)
    stdout, _ = await proc.communicate()
    return  {shlex.split(d)[1]:shlex.split(d)[0]+":"+shlex.split(d)[1] for d in stdout.decode().splitlines()}

async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """

    sinks = await get_sinks()

    if data[CONF_SINK] not in sinks:
        raise InvalidSinkID

    # Return info that you want to store in the config entry.
    return {"title": "PulseAudio: "+data[CONF_SINK]}



# Path: pulseaudio/const.py
# DOMAIN = "pulseaudio"
# 
# CONF_SINK = 'sink'


# Path: pulseaudio/config_flow.py
import logging
import asyncio
import shlex
import voluptuous as vol
from subprocess import PIPE, Popen
from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_NAME
from .const import DOMAIN, CONF_SINK   # pylint:disable=unused-import

"""Config flow for Pulse Audio integration."""



_LOGGER = logging.getLogger(__name__)

async def get_sinks():
    cmd = "pactl list short sinks"
    proc = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE)
    stdout, _ = await proc.communicate()
    return  {shlex.split(d)[1]:shlex.split(d)[0]+":"+shlex.split(d)[1] for d in stdout.decode().splitlines()}

async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """

    sinks = await get_sinks()

    if data[CONF_SINK] not in sinks:
        raise InvalidSinkID

    # Return info that you want to store in the config entry.
    return {"title": "PulseAudio: "+data[CONF_SINK]}



class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):