/// <summary>
        /// Creates a <see cref="BaseOutgoingMessage"/> used in specific operations for certain collection registry items.
        /// </summary>
        /// <param name="customOpName">The custom operation name.</param>
        /// <param name="modelId">The unique identifier of the collection registry item.</param>
        /// <param name="model">The object model involved in the operation, if any.</param>
        /// <param name="selectedProperties">White-list containing the name of the properties to extract from the <paramref name="model"/> object.
        /// When <see langword="null"/>, no filter will be applied.</param>
        /// <returns>A <see cref="BaseOutgoingMessage"/> used in specific operations for certain collection registry items.</returns>
        protected BaseOutgoingMessage CreateCustomOperationMessage(string customOpName, string modelId, TModel model = null, IEnumerable <string> selectedProperties = null)
        {
            var mergedObject = model != null?HassSerializer.CreateJObject(model, selectedProperties) : new JObject();

            this.AddModelIdProperty(mergedObject, modelId);
            return(new RawCommandMessage($"{this.apiPrefix}/{customOpName}", mergedObject));
        }
        /// <summary>
        /// Creates a <see cref="BaseOutgoingMessage"/> used to update an existing item from the collection registry.
        /// </summary>
        /// <param name="modelId">The unique identifier of the collection registry item to update.</param>
        /// <param name="model">The object model to be updated.</param>
        /// <param name="selectedProperties">White-list containing the name of the properties to extract from the <paramref name="model"/> object.
        /// When <see langword="null"/>, no filter will be applied.</param>
        /// <returns>
        /// A <see cref="BaseOutgoingMessage"/> used to update an existing item from the collection registry.
        /// </returns>
        protected BaseOutgoingMessage CreateUpdateMessage(string modelId, object model, IEnumerable <string> selectedProperties = null)
        {
            var mergedObject = HassSerializer.CreateJObject(model, selectedProperties);

            this.AddModelIdProperty(mergedObject, modelId);
            return(new RawCommandMessage($"{this.apiPrefix}/update", mergedObject));
        }
示例#3
0
        public void JObjectWithSelectedProperties()
        {
            var selectedProperties = new[] { nameof(TestClass.TestProperty) };
            var result             = HassSerializer.CreateJObject(new TestClass(), selectedProperties);

            Assert.NotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.ContainsKey(expectedTestPropertyResult));
            Assert.IsFalse(result.ContainsKey(expectedTestFieldResult));
        }
示例#4
0
        public void JObjectFieldsAreConvertedToSnakeCase()
        {
            var value = new TestClass()
            {
                TestField = nameof(TestClass.TestField)
            };
            var result = HassSerializer.CreateJObject(value);

            Assert.NotNull(result);
            Assert.AreEqual(value.TestField, result.GetValue(expectedTestFieldResult).ToString());
        }
        public BaseOutgoingMessage CreateUpdateMessage(Device device, bool?disable, bool forceUpdate)
        {
            var model = this.CreateDefaultUpdateObject(device, forceUpdate);

            if (disable.HasValue)
            {
                var merged = HassSerializer.CreateJObject(new { DisabledBy = disable.Value ? DisabledByEnum.User : (DisabledByEnum?)null });
                model.Merge(merged);
            }

            return(this.CreateUpdateMessage(device.Id, model));
        }
        public BaseOutgoingMessage CreateUpdateMessage(EntityRegistryEntry entity, string newEntityId, bool?disable, bool forceUpdate)
        {
            var model = this.CreateDefaultUpdateObject(entity, forceUpdate);

            if (newEntityId != null)
            {
                var merged = HassSerializer.CreateJObject(new { NewEntityId = newEntityId });
                model.Merge(merged);
            }

            if (disable.HasValue)
            {
                var merged = HassSerializer.CreateJObject(new { DisabledBy = disable.Value ? DisabledByEnum.User : (DisabledByEnum?)null });
                model.Merge(merged);
            }

            return(this.CreateUpdateMessage(entity.EntityId, model));
        }
示例#7
0
 public void JObjectFromNullThrows()
 {
     Assert.Throws <ArgumentNullException>(() => HassSerializer.CreateJObject(null));
 }
        /// <summary>
        /// Creates the default update object filtering modified property names only.
        /// </summary>
        /// <param name="model">The object model to be updated.</param>
        /// <param name="forceUpdate">
        /// Indicates if the update message force the update of every modifiable property.
        /// </param>
        /// <returns>The default update object filtering modified property names only.</returns>
        protected JObject CreateDefaultUpdateObject(TModel model, bool forceUpdate)
        {
            var selectedProperties = forceUpdate ? model.GetModifiablePropertyNames() : model.GetModifiedPropertyNames();

            return(HassSerializer.CreateJObject(model, selectedProperties));
        }
 /// <summary>
 /// Creates the default create object filtering modifiable property names only.
 /// </summary>
 /// <param name="model">The object model to be updated.</param>
 /// <returns>The default create object filtering modifiable property names only.</returns>
 protected JObject CreateDefaultCreateObject(TModel model)
 {
     return(HassSerializer.CreateJObject(model, model.GetModifiablePropertyNames()));
 }