Exemplo n.º 1
0
        public void Contact_sensor_config_is_generated_with_device_class()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test door",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "contact", DeviceClass = "door"
                                   } },
            };
            var expectedConfig = @"
# Test door contact, from hubitat via MQTT
- platform: mqtt
  name: Test door contact
  device_class: door
  state_topic: hubitat/Test door/contact
  payload_on: open
  payload_off: closed
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
        }
Exemplo n.º 2
0
        public void Threshold_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test meter",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "power-threshold", OnCondition = "< 5"
                                   } },
            };
            var expectedConfig = @"
# Test meter, from hubitat via MQTT
- platform: mqtt
  name: Test meter
  state_topic: hubitat/Test meter/power
  value_template: '{%if (value | float) < 5-%}on{%-else-%}off{%-endif%}'
  payload_on: 'on'
  payload_off: 'off'
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.IsEmpty(config.Customization, "Customization declared as expected");
        }
Exemplo n.º 3
0
        public void Configured_platform_prefix_is_used()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test sensor",
                Platform = "smartthings",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "temperature"
                                   } },
            };
            var expectedPartialConfig = @"
  state_topic: this/is/a/test/Test sensor/temperature
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["sensor"].Count, "Only one entity returned");

            var config = result["sensor"].First();

            Assert.IsTrue(config.Entity.Contains(expectedPartialConfig), config.Entity, "Config declared as expected");
            Assert.IsEmpty(config.Customization, "Customization declared as expected");
        }
Exemplo n.º 4
0
        public void Customizations_as_expected()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test device",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type      = "battery",
                                       Customize = new Dictionary <string, string> {
                                           { "my_custom_attr", "The value" }
                                       },
                                   } },
            };
            var expectedCustomization = $@"
sensor.test_device_battery:
  my_custom_attr: The value
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["sensor"].Count, "Only one entity returned");

            var config = result["sensor"].First();

            //Assert.IsTrue(config.Entity.Contains(expectedPartialConfig), config.Entity, "Config declared as expected");

            Assert.AreEqual(expectedCustomization, config.Customization, "Customization declared as expected");
        }
Exemplo n.º 5
0
        public void Hold_release_button_sensor_config_is_generated(string sensorType)
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test button",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = sensorType
                                   } },
            };
            var expectedConfig = @"
# Test button hold, from hubitat via MQTT
- platform: mqtt
  name: Test button hold
  state_topic: hubitat/Test button/1/hold
  payload_on: held
  payload_off: released
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.IsEmpty(config.Customization, "Customization declared as expected");
        }
Exemplo n.º 6
0
        public void Scene_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test device",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "scene"
                                   } },
            };
            var expectedConfig = @"
# Test device scene, from hubitat via MQTT
- platform: mqtt
  name: Test device scene
  state_topic: hubitat/Test device/scene
  force_update: true
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["sensor"].Count, "Only one entity returned");

            var config = result["sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.IsEmpty(config.Customization, "Customization declared as expected");
        }
Exemplo n.º 7
0
        public void Generated_name_with_apostrophe_is_as_expected()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test user's button",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "button"
                                   } },
            };
            var expectedPartialConfig = $@"
  name: Test users button
".Trim();
            var expectedCustomization = $@"
binary_sensor.test_users_button:
  friendly_name: Test user's button
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.IsTrue(config.Entity.Contains(expectedPartialConfig), config.Entity, "Config declared as expected");

            Assert.AreEqual(expectedCustomization, config.Customization, "Customization declared as expected");
        }
Exemplo n.º 8
0
        public void Multiple_entities_are_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test multisensor",
                Platform = "hubitat",
                Name     = "Test device",
                Sensors  = new[] {
                    new SensorDefinition {
                        Type = "battery"
                    },
                    new SensorDefinition {
                        Type = "motion"
                    },
                    new SensorDefinition {
                        Type = "temperature"
                    },
                },
            };

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(2, result.Keys.Count, "Multiple entity types returned");
            Assert.Contains("sensor", result.Keys, "Expected entity types returned");
            Assert.Contains("binary_sensor", result.Keys, "Expected entity types returned");
            Assert.AreEqual(2, result["sensor"].Count, "Expected number of entities returned");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Expected number of entities returned");
        }
Exemplo n.º 9
0
        public void Unsupported_platform_throws()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test device",
                Platform = "unsupported",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "contact"
                                   } },
            };

            // Assert
            Assert.Throws <UnsupportedPlatformException>(() => transformer.TransformConfig(definition));
        }
Exemplo n.º 10
0
        public void Missing_deviceId_throws()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                Name     = "Test device",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "contact"
                                   } },
            };

            // Assert
            Assert.Throws <ValidationException>(() => transformer.TransformConfig(definition));
        }
Exemplo n.º 11
0
        public void Missing_threshold_condition_throws()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test meter",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "power-threshold"
                                   } },
            };

            // Action
            Assert.Throws <ValidationException>(() => transformer.TransformConfig(definition));
        }
Exemplo n.º 12
0
        public void Missing_power_cycle_sensor_throws()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test meter",
                Platform = "hubitat",
                Name     = "Test device",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "power-cycle-onoff"
                                   } },
            };

            // Assert
            Assert.Throws <ValidationException>(() => transformer.TransformConfig(definition));
        }
Exemplo n.º 13
0
        public void Multiple_hold_release_button_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test button",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "2-hold-release-button"
                                   } },
            };
            var expectedConfig = new[] { @"
# Test button 1 hold, from hubitat via MQTT
- platform: mqtt
  name: Test button 1 hold
  state_topic: hubitat/Test button/1/hold
  payload_on: held
  payload_off: released
".Trim(), @"
# Test button 2 hold, from hubitat via MQTT
- platform: mqtt
  name: Test button 2 hold
  state_topic: hubitat/Test button/2/hold
  payload_on: held
  payload_off: released
".Trim() };

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(expectedConfig.Length, result["binary_sensor"].Count, "Correct number of entities returned");

            var configs = result["binary_sensor"].Select(i => i.Entity).ToArray();

            foreach (var expected in expectedConfig)
            {
                Assert.Contains(expected, configs, "Config declared as expected");
            }

            Assert.IsTrue(result["binary_sensor"].All(i => string.IsNullOrEmpty(i.Customization)), "Customization declared as expected");
        }
Exemplo n.º 14
0
        public void Power_cycle_onoff_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test meter",
                Platform = "hubitat",
                Name     = "Test device",
                Sensors  = new[] {
                    new SensorDefinition {
                        Type = "power-cycle"
                    },
                    new SensorDefinition {
                        Type = "power-cycle-onoff"
                    },
                },
            };
            var expectedConfig        = @"
# Test device
- platform: template
  sensors:
    test_device:
      value_template: >
        {{states.sensor.test_device_cycle.state not in ['unknown','off']}}
".Trim();
            var expectedCustomization = @"
binary_sensor.test_device:
  friendly_name: Test device
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            // We don't care about the sensor here - that's tested elsewhere. Just look at the binary_sensor.
            Assert.IsTrue(result.ContainsKey("binary_sensor"), "Expected entity type is returned");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.AreEqual(expectedCustomization, config.Customization, "Customization declared as expected");
        }
Exemplo n.º 15
0
        public void Moisture_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test flood",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "moisture"
                                   } },
            };
            var expectedConfig        = @"
# Test flood moisture, from hubitat via MQTT
- platform: mqtt
  name: Test flood moisture
  device_class: moisture
  state_topic: hubitat/Test flood/water
  payload_on: wet
  payload_off: dry
".Trim();
            var expectedCustomization = @"
binary_sensor.test_flood_moisture:
  friendly_name: Test flood
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.AreEqual(expectedCustomization, config.Customization, "Customization declared as expected");
        }
Exemplo n.º 16
0
        public void Smoke_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test smoke",
                Platform = "hubitat",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "smoke"
                                   } },
            };
            var expectedConfig = @"
# Test smoke, from hubitat via MQTT
- platform: mqtt
  name: Test smoke
  device_class: smoke
  state_topic: hubitat/Test smoke/smoke
  value_template: >
    {%if value == 'clear'-%}clear{%-else-%}smoke{%-endif%}
  payload_on: smoke
  payload_off: clear
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("binary_sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["binary_sensor"].Count, "Only one entity returned");

            var config = result["binary_sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.IsEmpty(config.Customization, "Customization declared as expected");
        }
Exemplo n.º 17
0
        public void Battery_sensor_config_is_generated()
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test multisensor",
                Platform = "hubitat",
                Name     = "Test device",
                Sensors  = new[] { new SensorDefinition {
                                       Type = "battery"
                                   } },
            };
            var expectedConfig = @"
# Test multisensor battery, from hubitat via MQTT
- platform: mqtt
  name: Test multisensor battery
  device_class: battery
  state_topic: hubitat/Test multisensor/battery
  value_template: >
    {{ value | int }}
  unit_of_measurement: '%'
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual("sensor", result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result["sensor"].Count, "Only one entity returned");

            var config = result["sensor"].First();

            Assert.AreEqual(expectedConfig, config.Entity, "Config declared as expected");
            Assert.IsEmpty(config.Customization, "Customization declared as expected");
        }
Exemplo n.º 18
0
        public void Generated_name_is_as_expected(string sensorType, string entityType, string name, string friendlyName)
        {
            // Arrange
            var transformer = new MqttSensorConfigTransformer(new DummyConfiguration(), new DeviceTranslator());
            var definition  = new DeviceDefinition
            {
                DeviceId = "Test multisensor",
                Platform = "hubitat",
                Name     = "Test device",
                Sensors  = new[] { new SensorDefinition {
                                       Type = sensorType
                                   } },
            };
            var expectedPartialConfig = $@"
  name: {name}
".Trim();

            // Action
            var result = transformer.TransformConfig(definition);

            // Assert
            Assert.AreEqual(1, result.Keys.Count, "One entity type returned");
            Assert.AreEqual(entityType, result.Keys.First(), "The type of the entity returned is correct");
            Assert.AreEqual(1, result[entityType].Count, "Only one entity returned");

            var config = result[entityType].First();

            Assert.IsTrue(config.Entity.Contains(expectedPartialConfig), "Config declared as expected");

            if (friendlyName != null)
            {
                var expectedPartialCustomization = $@"
  friendly_name: {friendlyName}
".Trim();
                Assert.IsTrue(config.Customization.Contains(expectedPartialCustomization), "Customization declared as expected");
            }
        }