public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { var objs = MockHassModelFactory.PanelInfoFaker.Generate(10).ToDistinctDictionary(x => x.ComponentName); var resultObject = new JRaw(HassSerializer.SerializeObject(objs)); return(this.CreateResultMessageWithResult(resultObject)); }
public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { var configuration = MockHassModelFactory.ConfigurationFaker.Generate(); var resultObject = new JRaw(HassSerializer.SerializeObject(configuration)); return(this.CreateResultMessageWithResult(resultObject)); }
protected override object ProccessUpdateCommand(MockHassServerRequestContext context, JToken merged) { var newEntityIdProperty = merged.FirstOrDefault(x => (x is JProperty property) && property.Name == "new_entity_id"); var newEntityId = (string)newEntityIdProperty; newEntityIdProperty?.Remove(); var entityIdProperty = merged.FirstOrDefault(x => (x is JProperty property) && property.Name == "entity_id"); var entityId = (string)entityIdProperty; var result = this.FindRegistryEntry(context, entityId, createIfNotFound: true); if (result != null) { if (newEntityId != null) { context.HassDB.DeleteObject(result.Entry); ((JProperty)entityIdProperty).Value = newEntityId; } this.PopulateModel(merged, result); if (newEntityId != null) { context.HassDB.CreateObject(result.Entry); } } return(new EntityEntryResponse() { EntityEntryRaw = new JRaw(HassSerializer.SerializeObject(result)) }); }
public void EnumValuesAreConvertedToSnakeCase() { var value = TestEnum.TestValue; var result = HassSerializer.SerializeObject(value); Assert.NotNull(result); Assert.AreEqual($"\"{expectedTestValueEnumResult}\"", result); }
public async Task SendMessageAsync(BaseMessage message, CancellationToken cancellationToken) { var sendMsg = HassSerializer.SerializeObject(message); var sendBytes = Encoding.UTF8.GetBytes(sendMsg); var sendBuffer = new ArraySegment <byte>(sendBytes); await this.WebSocket.SendAsync(sendBuffer, WebSocketMessageType.Text, endOfMessage : true, cancellationToken); }
protected override object ProccessUpdateCommand(MockHassServerRequestContext context, JToken merged) { var user = base.ProccessUpdateCommand(context, merged); return(new UserResponse() { UserRaw = new JRaw(HassSerializer.SerializeObject(user)) }); }
public void EnumValuesAreConvertedToSnakeCasePriorizingEnumMemberAttribute() { var value = TestEnum.EnumMemberTestValue; var memInfo = typeof(TestEnum).GetMember(value.ToString()); var attribValue = memInfo[0].GetCustomAttribute <EnumMemberAttribute>().Value; var result = HassSerializer.SerializeObject(value); Assert.NotNull(result); Assert.AreEqual($"\"{attribValue}\"", result); }
public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { var callServiceMsg = receivedCommand as CallServiceMessage; var state = new StateModel() { Context = MockHassModelFactory.ContextFaker.Generate() }; var resultObject = new JRaw(HassSerializer.SerializeObject(state)); return(this.CreateResultMessageWithResult(resultObject)); }
public void FieldsAreConvertedToSnakeCase() { var value = new TestClass() { TestField = nameof(TestClass.TestField) }; var result = HassSerializer.SerializeObject(value); Assert.NotNull(result); Assert.IsTrue(result.Contains($"\"{expectedTestFieldResult}\":\"{value.TestField}\"")); }
public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { try { if (!this.isContextReady) { this.isContextReady = true; this.PrepareHassContext(context); } var merged = (receivedCommand as RawCommandMessage).MergedObject as JToken; var commandType = receivedCommand.Type; object result = null; if (commandType.EndsWith("list")) { result = this.ProccessListCommand(context, merged); } else if (commandType.EndsWith("create")) { result = this.ProccessCreateCommand(context, merged); } else if (commandType.EndsWith("delete")) { result = this.ProccessDeleteCommand(context, merged); } else if (commandType.EndsWith("update")) { result = (object)this.ProccessUpdateCommand(context, merged) ?? ErrorCodes.NotFound; } else { result = this.ProccessUnknownCommand(commandType, context, merged); } if (result is ErrorCodes errorCode) { return(this.CreateResultMessageWithError(new ErrorInfo(errorCode))); } else { var resultObject = new JRaw(HassSerializer.SerializeObject(result)); return(this.CreateResultMessageWithResult(resultObject)); } } catch (Exception ex) { return(this.CreateResultMessageWithError(new ErrorInfo(ErrorCodes.UnknownError) { Message = ex.Message })); } }
private string GetModelSerialized(JToken merged) { string modelSerialized = HassSerializer.SerializeObject(merged); var idPropertyName = HassSerializer.GetSerializedPropertyName(idPropertyInfo); if (this.modelIdPropertyName != idPropertyName) { modelSerialized = modelSerialized.Replace(this.modelIdPropertyName, idPropertyName); } return(modelSerialized); }
public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { var commandEntitySource = receivedCommand as EntitySourceMessage; IEnumerable <EntitySource> objs; if (commandEntitySource.EntityIds?.Count() > 0) { objs = MockHassModelFactory.EntitySourceFaker.GenerateWithEntityIds(commandEntitySource.EntityIds); } else { objs = MockHassModelFactory.EntitySourceFaker.Generate(10); } var resultObject = new JRaw(HassSerializer.SerializeObject(objs.ToDistinctDictionary(x => x.EntityId))); return(this.CreateResultMessageWithResult(resultObject)); }
public Task <bool> RaiseStateChangedEventAsync(string entityId) { var data = MockHassModelFactory.StateChangedEventFaker .GenerateWithEntityId(entityId); var eventResult = new EventResultInfo() { EventType = KnownEventTypes.StateChanged.ToEventTypeString(), Origin = "mock_server", TimeFired = DateTimeOffset.Now, Data = new JRaw(HassSerializer.SerializeObject(data)), Context = data.OldState.Context }; var eventResultObject = new JRaw(HassSerializer.SerializeObject(eventResult)); return(this.RaiseEventAsync(KnownEventTypes.StateChanged, eventResultObject)); }
public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { var searchMessage = receivedCommand as SearchRelatedMessage; var resultResponse = new SearchRelatedResponse(); if (searchMessage.ItemType == ItemTypes.Entity && searchMessage.ItemId == "light.bed_light") { var faker = new Faker(); resultResponse.AreaIds = new[] { faker.RandomUUID() }; resultResponse.AutomationIds = new[] { faker.RandomUUID() }; resultResponse.ConfigEntryIds = new[] { faker.RandomUUID() }; resultResponse.DeviceIds = new[] { faker.RandomUUID() }; resultResponse.EntityIds = new[] { faker.RandomEntityId() }; } var resultObject = new JRaw(HassSerializer.SerializeObject(resultResponse)); return(this.CreateResultMessageWithResult(resultObject)); }
public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand) { var commandRenderTemplate = receivedCommand as RenderTemplateMessage; this.entityIds = new HashSet <string>(); var result = Regex.Replace(commandRenderTemplate.Template, @"{{ (.*) }}", this.RenderTemplateValue); var listeners = new ListenersTemplateInfo() { All = false, Time = false, }; listeners.Entities = this.entityIds.ToArray(); listeners.Domains = listeners.Entities.Select(x => x.GetDomain()).ToArray(); this.entityIds = null; var renderTemplateEvent = new TemplateEventInfo() { Result = result, Listeners = listeners, }; var eventMsg = new EventResultMessage() { Id = commandRenderTemplate.Id, Event = new JRaw(HassSerializer.SerializeObject(renderTemplateEvent)) }; Task.Factory.StartNew(async() => { await Task.Delay(40); await context.SendMessageAsync(eventMsg, CancellationToken.None); }); return(this.CreateResultMessageWithResult(null)); }