简体   繁体   English

获取 AssertionError:结果应该是 None、string、bytes 或 StreamResponse。 找不到解决方法?

[英]Getting AssertionError: Result should be None, string, bytes or StreamResponse. Can't find a solution to resolve it?

I have created a post API, In this code I am trying to read a file in which JSON content is present.我创建了一个帖子 API,在此代码中,我试图读取一个包含 JSON 内容的文件。 In request of the API, device_id will come.应 API 的请求,将提供 device_id。 Based on that device_id, I have found the entities related to the device_id.根据该 device_id,我找到了与 device_id 相关的实体。 The code for which is as follows:-代码如下:-

class EntityBasedOnDeviceId(HomeAssistantView):
"""API that will read the entity registry file and read the entities corresponding to a device ID"""
url = URL_API_ENTITY_BASED_ON_DEVICEID
name = "api: entity_based_on_deviceId"

async def post(self, request):
    """Returning the content of the file core.entity_registry in response"""
    json_data = await request.json()
    device_id = json_data["device_id"]
    file_object = open("/home/vvdn/core/config/.storage/core.entity_registry", "r")
    content = file_object.read()
    content = json.loads(content)

    device_entities = {"entities": []}

    for entity in content["data"]["entities"]:
        print("\n hello \n", entity['device_id'])
        if entity["device_id"] is None:
            print("Yes working is None")
        if entity["device_id"] is not None:
            print("yes device_id is not None")
            if entity["device_id"] == device_id:
                device_entities["entities"].append(entity)

    return device_entities

This code is reading a file, for which the content is as follows:-此代码正在读取一个文件,其内容如下:-

{
        "version": 1,
        "minor_version": 8,
        "key": "core.entity_registry",
        "data": {
        "entities": [
          {
            "area_id": null,
            "capabilities": {},
            "config_entry_id": "b9ea169ebb460d3365262535b9550e5c",
            "device_class": null,
            "device_id": "b7d5c082a6b80d874653fae7d6d7ca10",
            "disabled_by": null,
            "entity_category": null,
            "entity_id": "media_player.tv",
            "hidden_by": null,
            "icon": null,
            "id": "ac35c115166f6a2409dbc87c2f6fa508",
            "has_entity_name": true,
            "name": null,
            "options": {},
            "original_device_class": null,
            "original_icon": null,
            "original_name": null,
            "platform": "cast",
            "supported_features": 131968,
            "unique_id": "bf2da007-c30c-45a7-ef5a-7951d86cfb59",
            "unit_of_measurement": null
          },
          {
            "area_id": null,
            "capabilities": null,
            "config_entry_id": null,
            "device_class": null,
            "device_id": null,
            "disabled_by": null,
            "entity_category": null,
            "entity_id": "person.kapil",
            "hidden_by": null,
            "icon": null,
            "id": "eb5382f464532d63ae88d80b3d865f8f",
            "has_entity_name": false,
            "name": null,
            "options": {},
            "original_device_class": null,
            "original_icon": null,
            "original_name": "Kapil",
            "platform": "person",
            "supported_features": 0,
            "unique_id": "kapil",
            "unit_of_measurement": null
          }
         ]
       }
     }

The error I am getting after hitting this API is as follows:-击中此 API 后出现的错误如下:-

    2022-09-20 16:01:23.855 ERROR (MainThread) [aiohttp.server] Error handling request
    Traceback (most recent call last):
    File "/home/vvdn/core/venv/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
    resp = await request_handler(request)
  File "/home/vvdn/core/venv/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/home/vvdn/core/venv/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
    File "/home/vvdn/core/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/home/vvdn/core/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
  File "/home/vvdn/core/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
  File "/home/vvdn/core/homeassistant/components/http/ban.py", line 82, in ban_middleware
    return await handler(request)
  File "/home/vvdn/core/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
  File "/home/vvdn/core/homeassistant/components/http/view.py", line 160, in handle
    assert (
AssertionError: Result should be None, string, bytes or StreamResponse. Got: {'entities': [{'area_id': None, 'capabilities': {}, 'config_entry_id': 'b9ea169ebb460d3365262535b9550e5c', 'device_class': None, 'device_id': 'b7d5c082a6b80d874653fae7d6d7ca10', 'disabled_by': None, 'entity_category': None, 'entity_id': 'media_player.tv', 'hidden_by': None, 'icon': None, 'id': 'ac35c115166f6a2409dbc87c2f6fa508', 'has_entity_name': True, 'name': None, 'options': {}, 'original_device_class': None, 'original_icon': None, 'original_name': None, 'platform': 'cast', 'supported_features': 131968, 'unique_id': 'bf2da007-c30c-45a7-ef5a-7951d86cfb59', 'unit_of_measurement': None}]}

Where am I doing wrong in this code?我在这段代码中哪里做错了?

I got it why I was getting the error.我明白了为什么我会收到错误。 I was getting the error because I was returning a dictionary from the function while using return statement in aiohttp , I can only return None, String, Bytes or StreamResponse as mentioned in the error as:-我收到错误是因为我在 aiohttp 中使用 return 语句时从function返回字典,我只能返回 None、String、Bytes 或 StreamResponse,如错误中所述:-

AssertionError: Result should be None, string, bytes or StreamResponse. AssertionError:结果应该是 None、string、bytes 或 StreamResponse。

So, we just have to change the return statement as:-因此,我们只需将 return 语句更改为:-

return str(device_entities)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM