Address late review of ViCare (#79458)

Runn blocking I/O of button entity creation in async_add_executor_job
This commit is contained in:
Hans Oischinger 2022-10-03 11:10:25 +02:00 committed by GitHub
parent c05d3c10db
commit 125c037def
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
"""Viessmann ViCare sensor device.""" """Viessmann ViCare button device."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress from contextlib import suppress
@ -30,7 +30,7 @@ BUTTON_DHW_ACTIVATE_ONETIME_CHARGE = "activate_onetimecharge"
class ViCareButtonEntityDescription( class ViCareButtonEntityDescription(
ButtonEntityDescription, ViCareRequiredKeysMixinWithSet ButtonEntityDescription, ViCareRequiredKeysMixinWithSet
): ):
"""Describes ViCare button sensor entity.""" """Describes ViCare button entity."""
BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = ( BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = (
@ -45,28 +45,41 @@ BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = (
) )
def _build_entity(name, vicare_api, device_config, description):
"""Create a ViCare button entity."""
_LOGGER.debug("Found device %s", name)
try:
description.value_getter(vicare_api)
_LOGGER.debug("Found entity %s", name)
except PyViCareNotSupportedFeatureError:
_LOGGER.info("Feature not supported %s", name)
return None
except AttributeError:
_LOGGER.debug("Attribute Error %s", name)
return None
return ViCareButton(
name,
vicare_api,
device_config,
description,
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Create the ViCare binary sensor devices.""" """Create the ViCare button entities."""
name = VICARE_NAME name = VICARE_NAME
api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API] api = hass.data[DOMAIN][config_entry.entry_id][VICARE_API]
entities = [] entities = []
for description in BUTTON_DESCRIPTIONS: for description in BUTTON_DESCRIPTIONS:
try: entity = await hass.async_add_executor_job(
description.value_getter(api) _build_entity,
_LOGGER.debug("Found entity %s", description.name)
except PyViCareNotSupportedFeatureError:
_LOGGER.info("Feature not supported %s", description.name)
continue
except AttributeError:
_LOGGER.debug("Attribute Error %s", name)
continue
entity = ViCareButton(
f"{name} {description.name}", f"{name} {description.name}",
api, api,
hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG], hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG],
@ -86,7 +99,7 @@ class ViCareButton(ButtonEntity):
def __init__( def __init__(
self, name, api, device_config, description: ViCareButtonEntityDescription self, name, api, device_config, description: ViCareButtonEntityDescription
): ):
"""Initialize the sensor.""" """Initialize the button."""
self.entity_description = description self.entity_description = description
self._device_config = device_config self._device_config = device_config
self._api = api self._api = api