Using "he" or "she" to refer to an animal of known gender, like a pet, is easy to understand, as you say.
Using a gendered personal pronoun to refer to an indefinite animal, or an animal of unknown gender, is just another kind of personification. There are no absolute rules about it. In my experience, people tend to use "he" more often than "she," similarly to how people often use masculine pronouns rather than feminine ones for indefinite human beings unless thinking specifically about a role associated with women. But there is nothing wrong with using "she" either.
The actual physical sex of the animal is only important if it is salient to the speaker. Otherwise, it might be ignored. For example, even someone who knows intellectually that only female mosquitoes suck blood might say something like "got him!" after swatting an annoying mosquito, and people don't always use feminine pronouns for personified worker bees even though in real life, worker bees are all female (example: Bee Movie).
Obviously, words that by definition can only refer to animals of one specific gender have to take the appropriately gendered pronoun. A speaker will not use the word "she" to refer to an animal that has been called a "bull", or the word "he" to refer to an animal that has been called a "hen". But words like this are not as common as gender-neutral species words.
I've seen some statements suggesting that some animals are more often associated with certain genders—I think the example was dogs as masculine, cats as feminine—but I think this is a simplified description that you should be careful not to take too dogmatically. (Oh, I just noticed John Lawler's comment descring this idea of "covert gender" as something that was discussed by Whorf.)
The thing is, I wouldn't be surprised at all if someone said something like "If I had a cat, I wouldn't let him wander outside." There is nothing wrong grammatically with this sentence either. So any kind of tendencies that exist for gendering entire species are very weak and can be overridden by all sorts of contextual details or just habits of the speaker.
And there are many animal species with no clear gender associations, like frogs and toads.
I would say at their strongest, the gender associations of certain animal species would be no stronger than the gender associations of the sun and the moon, and these certainly aren't entirely fixed in English usage. Nowadays the sun is usually personified as masculine, and the moon as feminine, in part likely due to influence from the grammatical genders of the associated words in Latin and the Romance languages, but the opposite associations are still possible:
These examples are maybe a bit unexpected for an English speaker, but at least to me, they don't seem at all like mistakes or even particularly glaring pronoun choices. In fact, I think it's a bit less jolting to me than the use of the feminine for a generic indefinite singular animate (as in something like "A good student pursues her studies diligently, while a bad student procrastinates"), which is pretty familiar as a minority usage pattern.
My point here is that the sun and the moon are personified relatively frequently, and even with all of that opportunity for a clear gendering tendency to develop, the tendency is still not overwhelmingly strong. For other things that we talk about less often, you can imagine the tendencies will be even weaker.
Here is a paper I found about the topic of this kind of gender in English:A Diachronic-Synchronic Review of Gender in English, Jesús Fernández-Domínguez
It has a list on page 57 and I decided to look to see if there were any classifications that seem too simplistic to me. There were some: I think sun (as I mentioned earlier), river, summer, winter, love, time, war and maybe death can be personified as feminine (death certainly can in more extended metaphors or fiction; it's harder with short references because people are very familiar with the masculine personification of death and are likely to think of it by default), despite Fernández-Domínguez classifying them as neuter or masculine, and I think moon (as I mentioned earlier), earth, spring, world, virtue, peace, liberty can be personified as masculine, despite Fernández-Domínguez classifying them as neuter or feminine. People would probably expect one gender more than the other, and might be surprised at the use of another, but it is quite possible grammatically, and I don't think it would even necessarily be surprising depending on the context.
Now, certainly some people might disagree with me about some or all of the above, but I imagine some people would agree to at least some extent. Overall, the use of gender in English personifications is quite flexible compared to an average European language with grammatical gender.