Creating a usable switch via the API

Is it possible to create a usable switch just via the api? I’m trying to create a switch using

hass.states.set('switch.test_switch', 'off')

but, while it creates it, it’s not usable. If I try to toggle the switch it gives me

Unable to find referenced entities switch

I’m creating the switch from a pyscript script, but I suspect it would be the same from a python_script script.

Is what I’m trying to do even possible?

I’d bet that the switch platform isn’t loaded, so while you’ve created the entity there’s nothing attached to it.

If I’ve got other switch entities already present shouldn’t it be loaded? Or do I need to load the platform manually before I create the switch?

If you have other entities associated with a platform then it should be fine

Should I have to tell it that it’s a template switch? Or is just that it’s a switch enough?