Well, I can tell you what the wires are doing. Black and white (lower) are AC supply (phase) and neutral (which is NOT ground) from the supply/switch.
The upper wires are
Safety ground is also wired, but bypasses this module entirely, so no wire for it. ETL approved this and gave it the equivalent of a "UL Listed" stamp, based on the guidance in the UL White Book.
Honestly, given the "playing with mains power" aspect of this, and the fact that 1000 different smart fan+light controllers already exist so you're reinventing the wheel, I don't feel like this is a good thing to do except as a "prove you can do it" experiment. But this is not a starter project.
As an experiment, feel free to use ЯU-Recognized relays. As a "I plan to use this indefinitely" thing, you must use UL-Listed relays, i.e. they have a UL in circle mark with a C or US sidemark, which will be weirdly more expensive and provide appropriate wiring methods for integrating into mains wiring.
There is nothing wrong with improvising low voltage control of mains appliances, as long as you use UL-Listed (not ЯU) relays, and install the mains side using NEC wiring methods. Mains is a whole different world that must be respected. We talk a lot about it over on diy.se.