Under California law, every landlord has a duty to provide a habitable premises. The tenancy must have hot and cold running water, be free of vermin, there cannot be holes in the wall, the roof or the windows.
The standard for habitability is not high. You cannot refuse to pay rent because the place has not been painted in a while. That doesn’t mean that you can have carpeting that is three hundred years old in the tenancy. That may be a health hazard.
Usually, habitability becomes an issue when rent is late. Suddenly the tenant finds mold, the heat stops working, the electrical system is not up to par. Some tenants will even sabotage the tenancy.
The problem for most tenant is that they raise the issue too late. If there is a problem, you report it immediately to the landlord and, if you really think it is bad, the code enforcement agency of your county.
If the landlord fails to fix it, and then evicts you, you then have a cause of action for retaliatory eviction. The presumption under California law is that an eviction is retaliatory if it happens within six months after you assert your rights. You cannot, however, stay in the tenancy and fail to pay rent and then claim habitability as an issue. You must move out and THEN sue. Many tenants claim their tenancy has problems, and then refuse to allow the landlord to enter the premises to fix it, and then refuse to move out, hoping to pressure the landlord into a settlement.
Because habitability is not raised before the eviction, Sonoma County judges just like other judges in California are often not sympathetic. If, however, it is an issue before, and it has not been repaired, then you have a valid defense to the eviction if you are a tenant, and a real problem if you are a landlord.