import org.openhab.binding.mqtt.homeassistant.generic.internal.MqttBindingConstants;
import org.openhab.binding.mqtt.homeassistant.internal.HaID;
import org.openhab.binding.mqtt.homeassistant.internal.HandlerConfiguration;
+import org.openhab.binding.mqtt.homeassistant.internal.HomeAssistantConfiguration;
import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
+import org.openhab.core.config.core.ConfigurableService;
+import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.type.ThingType;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*
* @author David Graeff - Initial contribution
*/
-@Component(service = DiscoveryService.class, configurationPid = "discovery.mqttha")
+@Component(service = DiscoveryService.class, configurationPid = "discovery.mqttha", property = Constants.SERVICE_PID
+ + "=discovery.mqttha")
+@ConfigurableService(category = "system", label = "Home Assistant Discovery", description_uri = "binding:mqtt.homeassistant")
@NonNullByDefault
public class HomeAssistantDiscovery extends AbstractMQTTDiscovery {
private final Logger logger = LoggerFactory.getLogger(HomeAssistantDiscovery.class);
+ private HomeAssistantConfiguration configuration;
protected final Map<String, Set<HaID>> componentsPerThingID = new TreeMap<>();
protected final Map<String, ThingUID> thingIDPerTopic = new TreeMap<>();
protected final Map<String, DiscoveryResult> results = new ConcurrentHashMap<>();
}
static final String BASE_TOPIC = "homeassistant";
+ static final String BIRTH_TOPIC = "homeassistant/status";
+ static final String ONLINE_STATUS = "online";
@NonNullByDefault({})
protected MqttChannelTypeProvider typeProvider;
@NonNullByDefault({})
protected MQTTTopicDiscoveryService mqttTopicDiscovery;
- public HomeAssistantDiscovery() {
+ @Activate
+ public HomeAssistantDiscovery(@Nullable Map<String, Object> properties) {
super(null, 3, true, BASE_TOPIC + "/#");
this.gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory()).create();
+ configuration = (new Configuration(properties)).as(HomeAssistantConfiguration.class);
}
@Reference
this.mqttTopicDiscovery = null;
}
+ @Modified
+ protected void modified(@Nullable Map<String, Object> properties) {
+ configuration = (new Configuration(properties)).as(HomeAssistantConfiguration.class);
+ }
+
@Override
protected MQTTTopicDiscoveryService getDiscoveryService() {
return mqttTopicDiscovery;
}
}
+ @Override
+ protected void startScan() {
+ super.startScan();
+ triggerDeviceDiscovery();
+ }
+
+ @Override
+ protected void startBackgroundDiscovery() {
+ super.startBackgroundDiscovery();
+ triggerDeviceDiscovery();
+ }
+
+ private void triggerDeviceDiscovery() {
+ if (!configuration.status) {
+ return;
+ }
+ // https://www.home-assistant.io/integrations/mqtt/#use-the-birth-and-will-messages-to-trigger-discovery
+ getDiscoveryService().publish(BIRTH_TOPIC, ONLINE_STATUS.getBytes(), 1, false);
+ }
+
protected void publishResults() {
Collection<DiscoveryResult> localResults;
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<config-description:config-descriptions
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:config-description="https://openhab.org/schemas/config-description/v1.0.0"
+ xsi:schemaLocation="https://openhab.org/schemas/config-description/v1.0.0
+ https://openhab.org/schemas/config-description-1.0.0.xsd">
+ <config-description uri="binding:mqtt.homeassistant">
+
+ <parameter name="status" type="boolean" required="false">
+ <label>Publish Online Status</label>
+ <default>true</default>
+ <description><![CDATA[
+ Publish <tt>online</tt> to <tt>homeassistant/status</tt> when discovering Home Assistant
+ things in order to trigger devices to publish up-to-date discovery information.
+ If you also run Home Assistant <i>and</i> other services that depend on knowing if Home
+ Assistant is not running, then it's possible for those services to be out-of-sync with
+ the actual status of Home Assistant, and you may want to disable this.
+ ]]></description>
+ </parameter>
+
+ </config-description>
+</config-description:config-descriptions>
thing-type.config.mqtt.homeassistant-updatable.doUpdate.label = Update
thing-type.config.mqtt.homeassistant-updatable.doUpdate.description = Request the device do an OTA update
-# channel types config
+# binding config
-channel-type.config.mqtt.ha-channel.component.label = Component
-channel-type.config.mqtt.ha-channel.component.description = HomeAssistant component type (e.g. binary_sensor, switch, light)
-channel-type.config.mqtt.ha-channel.config.label = Json Configuration
-channel-type.config.mqtt.ha-channel.config.description = The json configuration string received by the component via MQTT.
-channel-type.config.mqtt.ha-channel.nodeid.label = Node ID
-channel-type.config.mqtt.ha-channel.nodeid.description = Optional node name of the component
-channel-type.config.mqtt.ha-channel.objectid.label = Object ID
-channel-type.config.mqtt.ha-channel.objectid.description = Object id of the component
+binding.config.mqtt.homeassistant-status.label = Publish Online Status
+binding.config.mqtt.homeassistant-status.description = Publish <tt>online</tt> to <tt>homeassistant/status</tt> when discovering Home Assistant things in order to trigger devices to publish up-to-date discovery information. If you also run Home Assistant <i>and</i> other services that depend on knowing if Home Assistant is not running, then it's possible for those services to be out-of-sync with the actual status of Home Assistant, and you may want to disable this.