Improve logging elgato (#47681)

This commit is contained in:
Paulus Schoutsen 2021-03-09 10:52:53 -08:00 committed by GitHub
parent fd1add8f15
commit ed679b263b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 23 deletions

View File

@ -1,4 +1,6 @@
"""Support for Elgato Key Lights.""" """Support for Elgato Key Lights."""
import logging
from elgato import Elgato, ElgatoConnectionError from elgato import Elgato, ElgatoConnectionError
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
@ -30,6 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try: try:
await elgato.info() await elgato.info()
except ElgatoConnectionError as exception: except ElgatoConnectionError as exception:
logging.getLogger(__name__).debug("Unable to connect: %s", exception)
raise ConfigEntryNotReady from exception raise ConfigEntryNotReady from exception
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})

View File

@ -55,25 +55,20 @@ class ElgatoLight(LightEntity):
info: Info, info: Info,
): ):
"""Initialize Elgato Key Light.""" """Initialize Elgato Key Light."""
self._brightness: int | None = None
self._info: Info = info self._info: Info = info
self._state: bool | None = None self._state: State | None = None
self._temperature: int | None = None
self._available = True
self.elgato = elgato self.elgato = elgato
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the entity.""" """Return the name of the entity."""
# Return the product name, if display name is not set # Return the product name, if display name is not set
if not self._info.display_name: return self._info.display_name or self._info.product_name
return self._info.product_name
return self._info.display_name
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
return self._available return self._state is not None
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
@ -83,12 +78,14 @@ class ElgatoLight(LightEntity):
@property @property
def brightness(self) -> int | None: def brightness(self) -> int | None:
"""Return the brightness of this light between 1..255.""" """Return the brightness of this light between 1..255."""
return self._brightness assert self._state is not None
return round((self._state.brightness * 255) / 100)
@property @property
def color_temp(self) -> int | None: def color_temp(self) -> int | None:
"""Return the CT color value in mireds.""" """Return the CT color value in mireds."""
return self._temperature assert self._state is not None
return self._state.temperature
@property @property
def min_mireds(self) -> int: def min_mireds(self) -> int:
@ -108,7 +105,8 @@ class ElgatoLight(LightEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return the state of the light.""" """Return the state of the light."""
return bool(self._state) assert self._state is not None
return self._state.on
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light.""" """Turn off the light."""
@ -131,22 +129,19 @@ class ElgatoLight(LightEntity):
await self.elgato.light(**data) await self.elgato.light(**data)
except ElgatoError: except ElgatoError:
_LOGGER.error("An error occurred while updating the Elgato Key Light") _LOGGER.error("An error occurred while updating the Elgato Key Light")
self._available = False self._state = None
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update Elgato entity.""" """Update Elgato entity."""
restoring = self._state is None
try: try:
state: State = await self.elgato.state() self._state: State = await self.elgato.state()
except ElgatoError: if restoring:
if self._available: _LOGGER.info("Connection restored")
_LOGGER.error("An error occurred while updating the Elgato Key Light") except ElgatoError as err:
self._available = False meth = _LOGGER.error if self._state else _LOGGER.debug
return meth("An error occurred while updating the Elgato Key Light: %s", err)
self._state = None
self._available = True
self._brightness = round((state.brightness * 255) / 100)
self._state = state.on
self._temperature = state.temperature
@property @property
def device_info(self) -> dict[str, Any]: def device_info(self) -> dict[str, Any]: