Bravia.Discovery (Bravia v0.1.0)

Copy Markdown View Source

Finds Sony televisions on the local network over SSDP.

{:ok, devices} = Bravia.Discovery.discover()
{:ok, tv} = Bravia.from_device(hd(devices), psk: "0000")

Discovery is two steps. An M-SEARCH datagram goes out to the SSDP multicast group, and each television answers with a LOCATION pointing at its device description. That description is then fetched and parsed for the set's identity and its control URL.

The LOCATION port is assigned dynamically and is not the port the control API listens on, so the advertised URL has to be followed rather than reconstructed.

parse_search_response/1 and parse_device_description/1 are public and pure, which is where the real logic lives; the socket work around them is deliberately thin.

Summary

Functions

Searches for televisions and returns what answered.

Parses a UPnP device description into a Bravia.Device.

Parses an SSDP search reply into its headers.

Functions

discover(options \\ [])

@spec discover(keyword()) :: {:ok, [Bravia.Device.t()]} | {:error, term()}

Searches for televisions and returns what answered.

Options

  • :mx — seconds advertised for televisions to spread their replies over, and how long to listen. Defaults to 3.
  • :timeout — total listen time in milliseconds. Defaults to mx * 1000 + 500.
  • :ip — local interface address to bind, as a tuple, when the host is multi-homed.

Televisions that answer but whose description cannot be fetched or parsed are omitted rather than failing the whole search.

parse_device_description(xml)

@spec parse_device_description(binary()) ::
  {:ok, Bravia.Device.t()} | {:error, term()}

Parses a UPnP device description into a Bravia.Device.

Fails when the XML is malformed, or when it describes a device that advertises no Sony ScalarWebAPI service — that is, something on the network that is not a controllable BRAVIA.

parse_search_response(response)

@spec parse_search_response(binary()) :: {:ok, map()} | {:error, :missing_location}

Parses an SSDP search reply into its headers.

Returns the advertised :location and, when present, :usn, :server, and :st. Header names are matched case-insensitively.