Cache Astral object in moon integration, to use less CPU (#44012)

Creating an Astral() instance seems to be surprisingly expensive. This
was previously being done in every update, taking a non-trivial
proportion of the (actual) CPU used by HA, that is, ignoring
sleep/wait/idle states like being blocked on epoll.

This patch caches the Astral instance to avoid recomputing it
regularly.
This commit is contained in:
Huon Wilson 2020-12-11 09:31:54 +11:00 committed by GitHub
parent b4afef1395
commit 2bbe8e0e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,6 +49,7 @@ class MoonSensor(Entity):
"""Initialize the moon sensor."""
self._name = name
self._state = None
self._astral = Astral()
@property
def name(self):
@ -87,4 +88,4 @@ class MoonSensor(Entity):
async def async_update(self):
"""Get the time and updates the states."""
today = dt_util.as_local(dt_util.utcnow()).date()
self._state = Astral().moon_phase(today)
self._state = self._astral.moon_phase(today)