California narrowly averts electricity crisis as it sets records for energy use amid scorching heat.