def get_state(api, entity_id):
    try:
        req = api(METHOD_GET, URL_API_STATES_ENTITY.format(entity_id))

        # req.status_code == 422 if entity does not exist

        return ha.State.from_dict(req.json()) \
            if req.status_code == 200 else None

    except (HomeAssistantError, ValueError):
        # ValueError if req.json() can't parse the json
        _LOGGER.exception("Error fetching state")

        return None
Query given API for state of entity_id.
dataset/ETHPy150Open home-assistant/home-assistant/homeassistant/remote.py/get_state