The short answer to the thrust of your question about whether HOA deed restrictions may supersede municipal codes is: "Yes."
Here is why. The covenants, conditions and restrictions imposed in a planned community with a homeowner's association were available for inspection prior to purchase. In other words, when you agreed to purchase the home in that community, you agreed to be bound by the CCR's. So, you voluntarily agreed to the restrictions, and likely signed paperwork to that effect.
The purpose of deed restrictions is to maintain value for the entire community. To maintain value, the HOA can develop rules and requirements. Maintaining an owner's yard is a reasonable requirement. Would you prefer that the HOA dispatch yard police with rulers to enforce a 2 inch decree or a 3 inch decree? Requiring that a yard be "maintained" essentially requires that the yard be cared for, without having to get caught up with measuring blades of grass or to determine why or how the grass grew beyond a specified length or having to have the owner explain that the owner was on vacation or had back trouble that week. In other words, it gives the HOA some flexibility.
As to trespassing, read your CCR's and the HOA rules. What do they say about site visits?
Bottom line, your question scenario needs many more facts for an attorney to be able to evaluate whether or not the HOA stepped beyond its typical powers. Your question seems to be in the neighborhood of life is too short to fight over lawn maintenance.