You can define an MQTT Sensor (or MQTT Switch, Light, Lock, cover, etc) in YAML or you can create a script to define it via MQTT Discovery.
I provided someone else with an example here:
The script can also be used to override a discovered device. However, you must override all of the discovered device’s configuration and not just one part of it (i.e. you can’t just override its device_class alone).
MQTT Discovery is nothing more than Home Assistant subscribing to all topics beginning with homeassistant. A new entity will be created if the topic is in the correct format, namely something like this:
homeassistant/sensor/hallway_motion/config
and if it contains a payload that is also in the correct format. The format of the topic and payload are described here: