Example #1
0
def get_dtv_data(
    hass: HomeAssistant, host: str, port: int = DEFAULT_PORT, client_addr: str = "0"
) -> dict:
    """Retrieve a DIRECTV instance, locations list, and version info for the receiver device."""
    dtv = DIRECTV(host, port, client_addr, determine_state=False)
    locations = dtv.get_locations()
    version_info = dtv.get_version()

    return {
        DATA_CLIENT: dtv,
        DATA_LOCATIONS: locations,
        DATA_VERSION_INFO: version_info,
    }
Example #2
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the DirecTV platform."""
    known_devices = hass.data.get(DATA_DIRECTV, set())
    hosts = []

    if CONF_HOST in config:
        _LOGGER.debug("Adding configured device %s with client address %s ",
                      config.get(CONF_NAME), config.get(CONF_DEVICE))
        hosts.append([
            config.get(CONF_NAME), config.get(CONF_HOST),
            config.get(CONF_PORT), config.get(CONF_DEVICE)
        ])

    elif discovery_info:
        host = discovery_info.get('host')
        name = 'DirecTV_{}'.format(discovery_info.get('serial', ''))

        # Attempt to discover additional RVU units
        _LOGGER.debug("Doing discovery of DirecTV devices on %s", host)

        from DirectPy import DIRECTV
        dtv = DIRECTV(host, DEFAULT_PORT)
        try:
            resp = dtv.get_locations()
        except requests.exceptions.RequestException as ex:
            # Bail out and just go forward with uPnP data
            # Make sure that this device is not already configured
            # Comparing based on host (IP) and clientAddr.
            _LOGGER.debug("Request exception %s trying to get locations", ex)
            resp = {
                'locations': [{
                    'locationName': name,
                    'clientAddr': DEFAULT_DEVICE
                }]
            }

        _LOGGER.debug("Known devices: %s", known_devices)
        for loc in resp.get("locations") or []:
            if "locationName" not in loc or "clientAddr" not in loc:
                continue

            # Make sure that this device is not already configured
            # Comparing based on host (IP) and clientAddr.
            if (host, loc["clientAddr"]) in known_devices:
                _LOGGER.debug("Discovered device %s on host %s with "
                              "client address %s is already "
                              "configured",
                              str.title(loc["locationName"]),
                              host, loc["clientAddr"])
            else:
                _LOGGER.debug("Adding discovered device %s with"
                              " client address %s",
                              str.title(loc["locationName"]),
                              loc["clientAddr"])
                hosts.append([str.title(loc["locationName"]), host,
                              DEFAULT_PORT, loc["clientAddr"]])

    dtvs = []

    for host in hosts:
        dtvs.append(DirecTvDevice(*host))
        hass.data.setdefault(DATA_DIRECTV, set()).add((host[1], host[3]))

    add_entities(dtvs)
Example #3
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the DirecTV platform."""
    known_devices = hass.data.get(DATA_DIRECTV, set())
    hosts = []

    if CONF_HOST in config:
        _LOGGER.debug(
            "Adding configured device %s with client address %s ",
            config.get(CONF_NAME),
            config.get(CONF_DEVICE),
        )
        hosts.append([
            config.get(CONF_NAME),
            config.get(CONF_HOST),
            config.get(CONF_PORT),
            config.get(CONF_DEVICE),
        ])

    elif discovery_info:
        host = discovery_info.get("host")
        name = "DirecTV_{}".format(discovery_info.get("serial", ""))

        # Attempt to discover additional RVU units
        _LOGGER.debug("Doing discovery of DirecTV devices on %s", host)

        from DirectPy import DIRECTV

        dtv = DIRECTV(host, DEFAULT_PORT)
        try:
            resp = dtv.get_locations()
        except requests.exceptions.RequestException as ex:
            # Bail out and just go forward with uPnP data
            # Make sure that this device is not already configured
            # Comparing based on host (IP) and clientAddr.
            _LOGGER.debug("Request exception %s trying to get locations", ex)
            resp = {
                "locations": [{
                    "locationName": name,
                    "clientAddr": DEFAULT_DEVICE
                }]
            }

        _LOGGER.debug("Known devices: %s", known_devices)
        for loc in resp.get("locations") or []:
            if "locationName" not in loc or "clientAddr" not in loc:
                continue

            # Make sure that this device is not already configured
            # Comparing based on host (IP) and clientAddr.
            if (host, loc["clientAddr"]) in known_devices:
                _LOGGER.debug(
                    "Discovered device %s on host %s with "
                    "client address %s is already "
                    "configured",
                    str.title(loc["locationName"]),
                    host,
                    loc["clientAddr"],
                )
            else:
                _LOGGER.debug(
                    "Adding discovered device %s with"
                    " client address %s",
                    str.title(loc["locationName"]),
                    loc["clientAddr"],
                )
                hosts.append([
                    str.title(loc["locationName"]),
                    host,
                    DEFAULT_PORT,
                    loc["clientAddr"],
                ])

    dtvs = []

    for host in hosts:
        dtvs.append(DirecTvDevice(*host))
        hass.data.setdefault(DATA_DIRECTV, set()).add((host[1], host[3]))

    add_entities(dtvs)