From 96c6e4c2f434f371d90f39e326ae9b7a8b54131d Mon Sep 17 00:00:00 2001 From: lawtancool <26829131+lawtancool@users.noreply.github.com> Date: Tue, 4 Aug 2020 12:35:28 -0700 Subject: [PATCH] Fix Control4 token refresh (#38302) --- homeassistant/components/control4/__init__.py | 4 +--- homeassistant/components/control4/light.py | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/control4/__init__.py b/homeassistant/components/control4/__init__.py index 43af82678f4..0f27c678e59 100644 --- a/homeassistant/components/control4/__init__.py +++ b/homeassistant/components/control4/__init__.py @@ -171,9 +171,7 @@ class Control4Entity(entity.Entity): ): """Initialize a Control4 entity.""" self.entry = entry - self.account = entry_data[CONF_ACCOUNT] - self.director = entry_data[CONF_DIRECTOR] - self.director_token_expiry = entry_data[CONF_DIRECTOR_TOKEN_EXPIRATION] + self.entry_data = entry_data self._name = name self._idx = idx self._coordinator = coordinator diff --git a/homeassistant/components/control4/light.py b/homeassistant/components/control4/light.py index f121219fd36..d5a681eac09 100644 --- a/homeassistant/components/control4/light.py +++ b/homeassistant/components/control4/light.py @@ -19,7 +19,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from . import Control4Entity, get_items_of_category -from .const import CONTROL4_ENTITY_TYPE, DOMAIN +from .const import CONF_DIRECTOR, CONTROL4_ENTITY_TYPE, DOMAIN from .director_utils import director_update_data _LOGGER = logging.getLogger(__name__) @@ -138,12 +138,13 @@ class Control4Light(Control4Entity, LightEntity): device_id, ) self._is_dimmer = is_dimmer - self._c4_light = None - async def async_added_to_hass(self): - """When entity is added to hass.""" - await super().async_added_to_hass() - self._c4_light = C4Light(self.director, self._idx) + def create_api_object(self): + """Create a pyControl4 device object. + + This exists so the director token used is always the latest one, without needing to re-init the entire entity. + """ + return C4Light(self.entry_data[CONF_DIRECTOR], self._idx) @property def is_on(self): @@ -167,6 +168,7 @@ class Control4Light(Control4Entity, LightEntity): async def async_turn_on(self, **kwargs) -> None: """Turn the entity on.""" + c4_light = self.create_api_object() if self._is_dimmer: if ATTR_TRANSITION in kwargs: transition_length = kwargs[ATTR_TRANSITION] * 1000 @@ -176,10 +178,10 @@ class Control4Light(Control4Entity, LightEntity): brightness = (kwargs[ATTR_BRIGHTNESS] / 255) * 100 else: brightness = 100 - await self._c4_light.rampToLevel(brightness, transition_length) + await c4_light.rampToLevel(brightness, transition_length) else: transition_length = 0 - await self._c4_light.setLevel(100) + await c4_light.setLevel(100) if transition_length == 0: transition_length = 1000 delay_time = (transition_length / 1000) + 0.7 @@ -189,15 +191,16 @@ class Control4Light(Control4Entity, LightEntity): async def async_turn_off(self, **kwargs) -> None: """Turn the entity off.""" + c4_light = self.create_api_object() if self._is_dimmer: if ATTR_TRANSITION in kwargs: transition_length = kwargs[ATTR_TRANSITION] * 1000 else: transition_length = 0 - await self._c4_light.rampToLevel(0, transition_length) + await c4_light.rampToLevel(0, transition_length) else: transition_length = 0 - await self._c4_light.setLevel(0) + await c4_light.setLevel(0) if transition_length == 0: transition_length = 1500 delay_time = (transition_length / 1000) + 0.7