Bravia (Bravia v0.1.0)

Copy Markdown View Source

A client for the local REST API on Sony BRAVIA televisions.

{:ok, tv} = Bravia.new("192.168.1.20", psk: "0000")
{:ok, 15} = Bravia.Video.get_brightness(tv)
:ok = Bravia.Video.set_brightness(tv, 8)

Enabling control on the television

Reading requires no authentication. Changing anything requires a Pre-Shared Key, set on the TV under Settings → Network → Home Network → IP Control → Authentication by choosing "Normal and Pre-Shared Key". Pass the same value as :psk. Without it, writes fail with %Bravia.Error{reason: :unauthorized}.

Finding a television

Bravia.Discovery.discover/1 locates BRAVIA sets over SSDP:

{:ok, [device | _]} = Bravia.Discovery.discover()
{:ok, tv} = Bravia.from_device(device, psk: "0000")

Scope

This library is a stateless protocol client. It starts no processes and holds no state between calls — polling, caching, and supervision belong to the application using it.

Summary

Functions

Lists the methods and versions the television supports, keyed by service.

Builds a client from a device found by Bravia.Discovery.discover/1.

Builds a client for the television at host.

Functions

capabilities(client, services \\ ~w(system video audio avContent))

@spec capabilities(Bravia.Client.t(), [String.t()]) ::
  {:ok, %{required(String.t()) => %{required(String.t()) => [String.t()]}}}
  | {:error, Bravia.Error.t()}

Lists the methods and versions the television supports, keyed by service.

Method versions differ between models — the same set may offer setAudioVolume at both "1.0" and "1.2" — so check here rather than assuming a version is available.

{:ok, capabilities} = Bravia.capabilities(tv)
capabilities["audio"]["setAudioVolume"]
#=> ["1.0", "1.2"]

from_device(device, options \\ [])

@spec from_device(
  Bravia.Device.t(),
  keyword()
) :: {:ok, Bravia.Client.t()} | {:error, term()}

Builds a client from a device found by Bravia.Discovery.discover/1.

Uses the control URL the television advertised rather than assuming one, and accepts the same options as new/2.

new(host, options \\ [])

@spec new(
  String.t(),
  keyword()
) :: {:ok, Bravia.Client.t()} | {:error, term()}

Builds a client for the television at host.

Options

  • :psk — the Pre-Shared Key configured on the television. Required for writes.
  • :port — defaults to 80.
  • :scheme — defaults to "http". BRAVIA sets do not serve the control API over TLS.

Any other option is passed straight to Req.new/1, which is how you set timeouts, retry behaviour, or a test stub:

Bravia.new("192.168.1.20", psk: "0000", receive_timeout: 2_000)