/// <inheritdoc /> public async Task Initialized(CancellationToken ct, Automation automation) { if (!_enableHomeAssistantDiscovery) { return; } var id = "automation_" + GetId(automation); var config = new HomeAssistantConfig { Id = id, Name = id, // cf. comment below StateTopic = GetStateTopic(automation), StateOn = "enabled", StateOff = "disabled", IsOptimistic = true, // TODO: Add ability for automation to confirm state, then add control channel and set it pessimistic CommandTopic = GetStateTopic(automation), PayloadOn = "enabled", PayloadOff = "disabled", IsRetained = true, AvailabilityTopic = _mqtt.AvailabilityTopic, Icon = "mdi:clipboard-text-play" // "mdi:script-text-outline" }; // With first publish the automation using the "id" as "name" so Home assistant // will use it as "entity_id" (which cannot be configured from discovery component) // Then we publish it a second time using the right name. // Note: HA will not generate 2 different devices as we are providing de vice "unique_id" which stays the same. // Note: This is a patch which works only if HA is up when this config is published, if not, you can still change the entity_id from the UI await _mqtt.Publish(ct, $"homeassistant/switch/{id}/config", JsonConvert.SerializeObject(config), retain : !_mqtt.IsTestEnvironment); config.Name = automation.Name; await _mqtt.Publish(ct, $"homeassistant/switch/{id}/config", JsonConvert.SerializeObject(config), retain : !_mqtt.IsTestEnvironment); }
/// <inheritdoc /> public async Task Initialized(CancellationToken ct, Scene scene) { if (!_enableHomeAssistantDiscovery) { return; } var id = "scene_" + GetId(scene); var config = new HomeAssistantConfig { Id = id, Name = id, // cf. comment below StateTopic = GetStateTopic(scene), StateOn = "running", StateOff = "idle", IsOptimistic = false, CommandTopic = GetControlTopic(scene), PayloadOn = "start", PayloadOff = "stop", IsRetained = false, AvailabilityTopic = _mqtt.AvailabilityTopic, Icon = "mdi:script-text-outline" }; // With first publish the scene using the "id" as "name" so Home assistant // will use it as "entity_id" (which cannot be configured from discovery component) // Then we publish it a second time using the right name. // Note: HA will not generate 2 different devices as we are providing device "unique_id" which stays the same. // Note: This is a patch which works only if HA is up when this config is published, if not, you can still change the entity_id from the UI await _mqtt.Publish(ct, $"homeassistant/switch/{id}/config", JsonConvert.SerializeObject(config), retain : !_mqtt.IsTestEnvironment); config.Name = scene.Name; await _mqtt.Publish(ct, $"homeassistant/switch/{id}/config", JsonConvert.SerializeObject(config), retain : !_mqtt.IsTestEnvironment); }