Example #1
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Yamaha MusicCast platform."""
    import pymusiccast

    name = config.get(CONF_NAME)
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)

    receiver = pymusiccast.McDevice(host, udp_port=port)
    _LOGGER.debug("receiver: %s / Port: %d", receiver, port)

    add_devices([YamahaDevice(receiver, name)], True)
Example #2
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Yamaha MusicCast platform."""
    import socket
    import pymusiccast

    known_hosts = hass.data.get(KNOWN_HOSTS_KEY)
    if known_hosts is None:
        known_hosts = hass.data[KNOWN_HOSTS_KEY] = []
    _LOGGER.debug("known_hosts: %s", known_hosts)

    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    interval = config.get(INTERVAL_SECONDS)

    # Get IP of host to prevent duplicates
    try:
        ipaddr = socket.gethostbyname(host)
    except (OSError) as error:
        _LOGGER.error(
            "Could not communicate with %s:%d: %s", host, port, error)
        return

    if [item for item in known_hosts if item[0] == ipaddr]:
        _LOGGER.warning("Host %s:%d already registered.", host, port)
        return

    if [item for item in known_hosts if item[1] == port]:
        _LOGGER.warning("Port %s:%d already registered.", host, port)
        return

    reg_host = (ipaddr, port)
    known_hosts.append(reg_host)

    try:
        receiver = pymusiccast.McDevice(
            ipaddr, udp_port=port, mc_interval=interval)
    except pymusiccast.exceptions.YMCInitError as err:
        _LOGGER.error(err)
        receiver = None

    if receiver:
        for zone in receiver.zones:
            _LOGGER.debug(
                "receiver: %s / Port: %d / Zone: %s",
                receiver, port, zone)
            add_devices(
                [YamahaDevice(receiver, receiver.zones[zone])],
                True)
    else:
        known_hosts.remove(reg_host)