public async IAsyncEnumerable <EntityState> GetStatesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) { var response = await HttpClient.GetAsync("states", HttpCompletionOption.ResponseHeadersRead, cancellationToken); response.EnsureSuccessStatusCode(); var jsonSerializer = JsonSerializer.Create(JsonSerializerSettings); using (var responseStream = await response.Content.ReadAsStreamAsync()) using (var streamReader = new StreamReader(responseStream)) using (var reader = new JsonTextReader(streamReader)) { while (await reader.ReadAsync(cancellationToken)) { if (reader.TokenType == JsonToken.StartObject) { var jObject = await JObject.LoadAsync(reader, cancellationToken); var entityId = jObject["entity_id"]?.Value <string>() ?? throw new InvalidOperationException(); var state = jObject["state"]?.ToString(); var attributes = jObject["attributes"]?.ToObject <Dictionary <string, object> >() ?? throw new InvalidOperationException(); StateCodeGenerator.Observe(entityId, state, attributes); var entityStateType = StateProvider.GetEntityStateType(entityId, attributes); var entityState = (EntityState)jObject.ToObject(entityStateType, jsonSerializer); yield return(entityState); } } } }