Custom device mounted in electrical receptacle box?
That would not be code-compliant. The NEC requires that all installed equipment be listed by a recognized testing laboratory (i.e. Underwriters' Laboratories, &quot;UL&quot;, in the US) who has performed safety testing.
One way to deal with this is to outsource the switching job to a listed relay that mounts in or to a junction box, so that the mains voltage is completely contained in the junction box. Outside, the relay gives you terminals (for either closing a circuit or providing a voltage) to control the relay. Your custom IoT gadget can control the mains circuit without needing to be listed itself, because it is isolated from the mains power.
Random example relay