Today's electrical grid is undergoing deep changes, resulting from the large integration of distributed Renewable Energy Sources (RES) in an effort to decarbonize the generation of electrical energy. In addition to the emergence of this volatile electricity production, the worldwide demand for electricity increases due to a growing population and the intensified electrification of buildings. Smart-buildings represent promising assets for supporting the electrical grid in balancing demand with a supply based on non-dispatchable RES. A smart-building denotes a building equipped with sensor/actuator hardware connected to a federating Building Data Management System (BDMS) which enables high-level applications and services. Tapping into the flexibility inherent to its various entities (load, storage, and generation), a smart-building can provide Demand Response (DR) functionality through the optimization of its energy profile in response to varying electricity prices or commands from the grid.This PhD thesis provides a set of tools, algorithms, and frameworks, revolving around the notion of smart-buildings that foster an enhanced Building-to-Grid (BtG) integration. The tools developed here aim to fill the gap encountered in the literature created by the recent rollout of BDMSs and the ubiquitous Internet of Things (IoT). Furthermore, the mismatch between current DR and the future RES-based smart-grid opens the way to the development of innovative algorithms and frameworks to manage the flexibility offered by smart-buildings for grid-side agents. Built upon BDMSs, two open-source tools have been developed. Firstly, an integrated high-speed emulation and simulation software, dubbed Virtualization Engine (vEngine), allows the simulation of non-existing components of a building directly on-site. The multi-threaded, light architecture of vEngine permits efficient simulations, in a modular environment conceived for developers. Secondly, we describe Open Energy Management System (OpenEMS), a platform that seamlessly connects to any existing BDMS and provides its users with an environment to create their own energy management algorithms, with a focus on Model Predictive Control (MPC). Simulations using a realistic Swiss residential building model demonstrate the effectiveness and modularity of both tools. Additionally, we propose a multi-state load profile identification algorithm tailored to Non-Intrusive Load Monitoring (NILM). Applied to energy disaggregation, it shows promising results for enhanced energy feedbacks to the occupants. To attain daily energy balance within the smart-grid, we propose several algorithms and energy management frameworks, using smart-buildings. An incremental MPC formulation is derived to better balance monthly costs associated to energy and peak demand of large commercial buildings. Simulations data show substantial benefits, for both the building's owner and the grid. Furthermore, we present a decentralized framework for autonomously managing the energy in a community of smart-buildings, with RES. Based on blockchain technology and smart-contracts, the framework optimizes an objective common to the whole community without the need for a central agent. Finally, we suggest a unified BtG model that could benefit grid-side aggregators in both microgrids and electricity markets.