Home Energy Management Systems (HEMS) have become a pivotal component of the smart grid infrastructure as they facilitate energy exchange and trading. With the increasing use of micropower renewables generation from wind and solar within residential houses and buildings, HEMS now plays a critical role in not just managing energy usage but assisting the smart grid with schedule optimisation. However, HEMS are typically installed with limited security measures, which makes them vulnerable to cyber-attacks. A coordinated attack on multiple HEMS has the potential to compromise the entire power grid. In this article, we aim to address this gap by proposing a robust security framework for HEMS. The framework centralises security in the design of HEMS and leverages Artificial Intelligence for the security requirements. The proposed framework consists of five layers with built-in security measures that use AI to mitigate cyber threats. This is a key contribution that has been neglected in related work in optimizing HEMS operations.