private async Task UpdateButtonsConfigAsync(string configDirectory, IEnumerable <DeviceDefinition> devices)
        {
            var file = Path.Combine(configDirectory, $"button-entities.yaml");

            // Generate the list of entities required by the list of devices.
            var model = _transformer.GenerateButtonList(devices);

            // Serialize the list of entities to yaml.
            var serializer = new SerializerBuilder()
                             .WithNamingConvention(new UnderscoredNamingConvention())
                             .Build();
            var output = serializer.Serialize(model);

            await Filesystem.WriteFileAsync(file, output);
        }
        private async Task UpdateEntitiesConfigAsync(string sensorType, string configDirectory, IEnumerable <DeviceDefinition> definitions)
        {
            var file = Path.Combine(configDirectory, $"{sensorType}-entities.yaml");

            // Deserialize the existing Lovelace config file.
            LovelaceEntity[] existingConfig = null;
            if (Filesystem.FileExists(file))
            {
                var fileContent = await Filesystem.ReadFileAsync(file);

                var deserializer = new DeserializerBuilder()
                                   .WithNamingConvention(new UnderscoredNamingConvention())
                                   .Build();
                existingConfig = deserializer.Deserialize <LovelaceEntity[]>(fileContent);
            }

            // Generate the list of entities required by the list of devices.
            var entities = _transformer.GenerateSensorEntityList(sensorType, definitions);

            // Generate the list of entites to be included in the Lovelace config,
            // by adding in any which don't already exist.
            var config = existingConfig?.ToList() ?? new List <LovelaceEntity>();

            foreach (var entity in entities)
            {
                // If entity exists in the list already, leave it unchanged.
                // If it doesn't, add it in.
                if (!config.Any(i => i.Entity == entity.Entity))
                {
                    config.Add(entity);
                }
            }

            // Serialize the list of entities to yaml.
            var serializer = new SerializerBuilder()
                             .WithNamingConvention(new UnderscoredNamingConvention())
                             .Build();
            var output = serializer.Serialize(config);

            await Filesystem.WriteFileAsync(file, output);
        }
Exemple #3
0
        private async Task WriteToConfigFileAsync(string type, string[] entries, string filePath)
        {
            if (!entries.Any())
            {
                return;
            }

            var sectionStart = string.Format(SectionStartFormat, type);
            var sectionEnd   = string.Format(SectionEndFormat, type);

            var fileContent = (Filesystem.FileExists(filePath) ? await Filesystem.ReadFileAsync(filePath) : string.Empty).Trim();
            var startIndex  = fileContent.IndexOf(sectionStart);
            var endIndex    = fileContent.IndexOf(sectionEnd);

            if (startIndex > -1 && endIndex > startIndex)
            {
                // Remove existing section from file
                fileContent = fileContent.Remove(startIndex, endIndex + sectionEnd.Length - startIndex);
            }

            var newContent = string.Join(Environment.NewLine + Environment.NewLine,
                                         sectionStart,
                                         string.Join(Environment.NewLine + Environment.NewLine, entries),
                                         sectionEnd);

            if (startIndex > -1)
            {
                fileContent = fileContent.Insert(startIndex, newContent);
            }
            else
            {
                fileContent = fileContent + Environment.NewLine + Environment.NewLine + newContent;
            }

            await Filesystem.WriteFileAsync(filePath, fileContent + Environment.NewLine);
        }