Characterizing the energy flexibility of buildings and districts