Read a map list

Hi, I managed to obtain, thanks to chatGpt, the list of errors of my inverter with this code.
Unfortunately the list is long and contains 240 IDs and I don’t want to make the main yaml unreadable. Is there a way to write the list into another external yaml file or text file or whatever and read it with the same code?

text_sensor:
 - platform: modbus_controller
   modbus_controller_id: zcs
   name: Messaggio Errore
   id: fault
   register_type: holding
   address: 0x0405
   register_count: 18
   response_size: 36
   raw_encode: HEXBYTES
   lambda: |-
    std::string z;    
    // Mappa degli ID degli errori ai loro significati
    std::map<int, std::string> errorMap = {
      {1, "GridOVP"},
      {2, "GridUVP"},
      {3, "GridOFP"},
      {4, "GridUFP"},
      {5, "GFCI"},
      {6, "OVRT"},
      {7, "LVRT"},
      {8, "IslandFault"},
      {10, ""},
      {11, ""},
      {12, ""},
      {13, ""},
      {14, ""},
      {15, ""},
      {16, ""},
      // Aggiungi altri ID e significati qui.....
      {240, ""},
    };   
    for (int i = 0; i < 18; i++) {
      int idx = item->offset + (i * 2);
      int value = (data[idx] << 8) | data[idx + 1];
      for (int bit = 0; bit < 16; bit++) {
        if (value & (1 << bit)) {
          int errorID = i * 16 + bit + 1;          
          // Cerca il significato nell'amppa
          auto it = errorMap.find(errorID);
          if (it != errorMap.end()) {
            z += "ID" + std::to_string(errorID) + " (" + it->second + "), ";
          } else {
            z += "ID" + std::to_string(errorID) + " (Significato sconosciuto), ";
          }
        }
      }
    }    
    if (!z.empty()) {
      z.pop_back(); // Rimuove l'ultima virgola
      z.pop_back(); // Rimuove lo spazio finale
    }    
    return {z};

Here’s some thoughts. I don’t know exact solutions or even if they are good approaches.

Maybe you can store the map in a global? And use packages/include so it doesn’t clutter your main file?

Or maybe there is a way you can store them in a local json file and read that in? I know ESPHome has some json parsing functions but not sure about reading local files.

The other thing which could help is just figure out how to define it as a global (or similar) but place that at the end of your yaml file so that the top part of your code is still short and easy to navigate.