private ConfigEntry FormatSensorDefinition(string entityType, SensorConfig sensor) { void addConfig(List <string> entity) { var prefix = _configuration.GetPlatformPrefix(sensor.Platform); switch (sensor.Type) { case SensorType.Battery: entity.Add($" device_class: {sensor.DeviceClass ?? "battery"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/battery"); entity.Add($" value_template: >{Environment.NewLine} {{{{ value | int }}}}"); entity.Add(@" unit_of_measurement: '%'"); break; case SensorType.Contact: if (sensor.DeviceClass != null) { entity.Add($" device_class: {sensor.DeviceClass}"); } entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/contact"); entity.Add(" payload_on: open"); entity.Add(" payload_off: closed"); break; case SensorType.Illuminance: entity.Add($" device_class: {sensor.DeviceClass ?? "illuminance"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/illuminance"); entity.Add($" value_template: >{Environment.NewLine} {{{{ value | int }}}}"); entity.Add(" unit_of_measurement: lux"); entity.Add(" force_update: true"); break; case SensorType.Moisture: entity.Add($" device_class: {sensor.DeviceClass ?? "moisture"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/water"); entity.Add(" payload_on: wet"); entity.Add(" payload_off: dry"); break; case SensorType.Motion: entity.Add($" device_class: {sensor.DeviceClass ?? "motion"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/motion"); entity.Add(" payload_on: active"); entity.Add(" payload_off: inactive"); break; case SensorType.Offline: entity.Add($" device_class: {sensor.DeviceClass ?? "problem"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/activity"); entity.Add(" payload_on: inactive"); entity.Add(" payload_off: active"); break; case SensorType.Power: if (sensor.DeviceClass != null) { entity.Add($" device_class: {sensor.DeviceClass}"); } entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/power"); entity.Add($" value_template: >{Environment.NewLine} {{{{ value | float }}}}"); entity.Add(" unit_of_measurement: W"); entity.Add(" force_update: true"); break; case SensorType.PowerCycle: entity.Add($" state_topic: {prefix}/{sensor.DeviceName}/cycle"); break; case SensorType.Presence: entity.Add($" device_class: {sensor.DeviceClass ?? "presence"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/presence"); entity.Add(" payload_on: present"); entity.Add(" payload_off: not present"); break; case SensorType.Scene: entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/scene"); entity.Add(" force_update: true"); break; case SensorType.Smoke: entity.Add($" device_class: {sensor.DeviceClass ?? "smoke"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/smoke"); entity.Add(" value_template: >"); entity.Add(" {%if value == 'clear'-%}clear{%-else-%}smoke{%-endif%}"); entity.Add(" payload_on: smoke"); entity.Add(" payload_off: clear"); break; case SensorType.Temperature: entity.Add($" device_class: {sensor.DeviceClass ?? "temperature"}"); entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/temperature"); entity.Add($" value_template: >{Environment.NewLine} {{{{ value | float }}}}"); entity.Add(" unit_of_measurement: °C"); entity.Add(" force_update: true"); break; case SensorType.Threshold: if (sensor.DeviceClass != null) { entity.Add($" device_class: {sensor.DeviceClass}"); } entity.Add($" state_topic: {prefix}/{sensor.DeviceId}/{sensor.ThresholdAttribute}"); entity.Add($" value_template: '{{%if (value | float) {sensor.ThresholdOnCondition}-%}}on{{%-else-%}}off{{%-endif%}}'"); entity.Add($" payload_on: 'on'"); entity.Add($" payload_off: 'off'"); break; default: throw new UnrecognizedTypeException(sensor.Type); } } return(FormatDefinition(addConfig, entityType, sensor)); }
private ConfigEntry FormatDefinition(Action <List <string> > addConfig, string entityType, SensorConfig sensor) { // TODO: Accept Genius sensors if (!new[] { Platform.HomeAssistant, Platform.Hubitat, Platform.SmartThings }.Contains(sensor.Platform)) { throw new UnsupportedPlatformException(sensor.Platform, $"sensor:{sensor.Type}"); } var entity = new List <string>(); var customization = new List <string>(); entity.Add($"# {sensor.Name}, from {sensor.Platform} via MQTT"); entity.Add("- platform: mqtt"); if (sensor.Name.Contains("'") && (sensor.Customize == null || !sensor.Customize.ContainsKey("friendly_name"))) { entity.Add($" name: {sensor.Name.Replace("'", string.Empty)}"); customization.Add($" friendly_name: {sensor.Name}"); } else { entity.Add($" name: {sensor.Name}"); } if (sensor.Icon != null) { customization.Add($" icon: {sensor.Icon}"); } if (sensor.Customize != null) { foreach (var key in sensor.Customize.Keys) { customization.Add($" {key}: {sensor.Customize[key]}"); } } addConfig(entity); if (customization.Any()) { customization.Insert(0, $@"{entityType}.{FormatAsId(sensor.Name)}:"); } return(new ConfigEntry { Entity = string.Join(Environment.NewLine, entity), Customization = string.Join(Environment.NewLine, customization), }); }