From 2bbe8e0e6ba363b8055796c8099e7ee8957964aa Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 11 Dec 2020 09:31:54 +1100 Subject: [PATCH] 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. --- homeassistant/components/moon/sensor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/moon/sensor.py b/homeassistant/components/moon/sensor.py index f7914177f8a..9e0f8ef51d6 100644 --- a/homeassistant/components/moon/sensor.py +++ b/homeassistant/components/moon/sensor.py @@ -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)