Skip to content

Commit

Permalink
[Chromecast] Added support for Audio groups fixes openhab#1853 (openh…
Browse files Browse the repository at this point in the history
…ab#1882)

Creates additional THING_TYPE for Audio Groups. Adds discovered network
port number to device configuration rather than relying on assumed port
number of 8009.

Signed-off-by: Sean McGuire [email protected] (github: mcguiresean)
  • Loading branch information
mcguiresean authored and Markinus committed Sep 8, 2017
1 parent bf73c77 commit 98819cb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@
xmlns:thing="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0"
xsi:schemaLocation="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0 http://eclipse.org/smarthome/schemas/thing-description-1.0.0.xsd">

<!-- Chromecast Audio Group Thing Type -->
<thing-type id="audiogroup">
<label>Chromecast Audio Group</label>
<description>A Google Chromecast Audio Group device</description>

<channels>
<channel id="control" typeId="control"/>
<channel id="volume" typeId="volume"/>
<channel id="mute" typeId="mute"/>
<channel id="playuri" typeId="playuri"/>
</channels>

<config-description>
<parameter name="ipAddress" type="text">
<context>network-address</context>
<label>Network Address</label>
<description>Network address of the Chromecast device.</description>
<required>true</required>
</parameter>
<parameter name="port" type="integer">
<label>Network Port</label>
<description>Network port of the Chromecast device.</description>
<advanced>true</advanced>
<default>8009</default>
</parameter>
</config-description>
</thing-type>

<!-- Chromecast Audio Thing Type -->
<thing-type id="audio">
<label>Chromecast Audio</label>
Expand All @@ -23,9 +51,14 @@
<description>Network address of the Chromecast device.</description>
<required>true</required>
</parameter>
<parameter name="port" type="integer">
<label>Network Port</label>
<description>Network port of the Chromecast device.</description>
<advanced>true</advanced>
<default>8009</default>
</parameter>
</config-description>
</thing-type>

<!-- Chromecast HDMI dongle Thing Type -->
<thing-type id="chromecast">
<label>Chromecast</label>
Expand All @@ -45,6 +78,12 @@
<description>Network address of the Chromecast device.</description>
<required>true</required>
</parameter>
<parameter name="port" type="integer">
<label>Network Port</label>
<description>Network port of the Chromecast device.</description>
<advanced>true</advanced>
<default>8009</default>
</parameter>
</config-description>
</thing-type>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class ChromecastBindingConstants {

public static final ThingTypeUID THING_TYPE_CHROMECAST = new ThingTypeUID(BINDING_ID, "chromecast");
public static final ThingTypeUID THING_TYPE_AUDIO = new ThingTypeUID(BINDING_ID, "audio");
public static final ThingTypeUID THING_TYPE_AUDIOGROUP = new ThingTypeUID(BINDING_ID, "audiogroup");

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>();

static {
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_AUDIO);
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_AUDIOGROUP);
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_CHROMECAST);
}

Expand All @@ -41,6 +43,7 @@ public class ChromecastBindingConstants {

// config parameters
public static final String HOST = "ipAddress";
public static final String PORT = "port";

// properties
public static final String SERIAL_NUMBER = "serialNumber";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.openhab.binding.chromecast.handler;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -95,9 +96,10 @@ public ChromecastHandler(final Thing thing, AudioHTTPServer audioHTTPServer, Str
this.callbackUrl = callbackUrl;
}

private void createChromecast(final String address) {
private void createChromecast(final String address, final int port) {
try {
chromecast = new ChromeCast(address);
logger.debug("Connecting to Chromecast: {} {}", address, port);
chromecast = new ChromeCast(address, port);
chromecast.registerListener(this);
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -141,24 +143,36 @@ public void dispose() {

@Override
public void initialize() {
final Object obj = getConfig().get(ChromecastBindingConstants.HOST);
if (!(obj instanceof String)) {
final Object ipAddress = getConfig().get(ChromecastBindingConstants.HOST);
if (!(ipAddress instanceof String)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
"Cannot connect to Chromecast. IP address is invalid.");
return;
}
final String host = (String) obj;

final String host = (String) ipAddress;
if (StringUtils.isBlank(host)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
"Cannot connect to Chromecast. IP address is not set.");
return;
}

if (chromecast != null && !chromecast.getAddress().equals(host)) {
final Object portnumber = getConfig().get(ChromecastBindingConstants.PORT);
logger.debug("Variable Type is {}", portnumber.getClass().getTypeName());
final int port;
if (portnumber instanceof BigDecimal) {
port = ((BigDecimal) portnumber).intValue();
} else if (portnumber instanceof Integer) {
port = (Integer) portnumber;
} else {
port = 8009;
}

if (chromecast != null && (!chromecast.getAddress().equals(host) || (chromecast.getPort() != port))) {
destroyChromecast();
}
if (chromecast == null) {
createChromecast(host);
createChromecast(host, port);
}

scheduleConnect(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.io.transport.mdns.discovery.MDNSDiscoveryParticipant;
import org.openhab.binding.chromecast.ChromecastBindingConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link ChromecastDiscoveryParticipant} is responsible for discovering Chromecast devices through UPnP.
Expand All @@ -31,6 +33,7 @@
public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant {

private static final String SERVICE_TYPE = "_googlecast._tcp.local.";
private final Logger logger = LoggerFactory.getLogger(getClass());

@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
Expand All @@ -53,7 +56,9 @@ public DiscoveryResult createResult(ServiceInfo service) {
final Map<String, Object> properties = new HashMap<>(2);
String host = service.getHostAddresses()[0];
properties.put(ChromecastBindingConstants.HOST, host);

int port = service.getPort();
properties.put(ChromecastBindingConstants.PORT, port);
logger.debug("Chromecast Found: {} {}", host, port);
String friendlyName = service.getPropertyString("fn"); // friendly name;

final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
Expand All @@ -64,11 +69,14 @@ public DiscoveryResult createResult(ServiceInfo service) {

private ThingTypeUID getThingType(final ServiceInfo service) {
String model = service.getPropertyString("md"); // model
logger.debug("Chromecast Type: {}", model);
if (model == null) {
return null;
}
if (model.equals("Chromecast Audio")) {
return ChromecastBindingConstants.THING_TYPE_AUDIO;
} else if (model.equals("Google Cast Group")) {
return ChromecastBindingConstants.THING_TYPE_AUDIOGROUP;
} else {
return ChromecastBindingConstants.THING_TYPE_CHROMECAST;
}
Expand Down

0 comments on commit 98819cb

Please sign in to comment.