A door-to-door salesman who barged into a woman's bedroom and exposed himself has delusional beliefs about people being sexually attracted to him, a Melbourne court has heard.

Troy Mark Sheppard, 28, was contracted to knock on people's doors in a bid to persuade them to switch their electricity accounts to Origin Energy.

The Glenroy man knocked on the door of a Richmond home in March last year and spoke with the resident before she briefly stepped out to put her cat outside, prosecutor Roy Brandi told the Melbourne Magistrates Court.

When she returned, the woman found Sheppard masturbating in her bedroom.

She repeatedly asked him to leave, but Sheppard told the woman he thought that was what she wanted.