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

Viewed 18

I have created a post API, In this code I am trying to read a file in which JSON content is present. In request of the API, device_id will come. Based on that device_id, I have found the entities related to the 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:-

    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?

1 Answers

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:-

AssertionError: Result should be None, string, bytes or StreamResponse.

So, we just have to change the return statement as:-

return str(device_entities)
Related