vicare: Don't create unsupportedd button entites (#79425)

Button entities should only be offered when the datapoint exists on
the API.
This commit is contained in:
Hans Oischinger 2022-10-01 21:17:25 +02:00 committed by GitHub
parent 94c825cf4f
commit 35fa73eee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -34,6 +34,14 @@ class ViCareRequiredKeysMixin:
value_getter: Callable[[Device], bool]
@dataclass()
class ViCareRequiredKeysMixinWithSet:
"""Mixin for required keys with setter."""
value_getter: Callable[[Device], bool]
value_setter: Callable[[Device], bool]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from config entry."""
_LOGGER.debug("Setting up ViCare component")

View File

@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ViCareRequiredKeysMixin
from . import ViCareRequiredKeysMixinWithSet
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG, VICARE_NAME
_LOGGER = logging.getLogger(__name__)
@ -27,7 +27,9 @@ BUTTON_DHW_ACTIVATE_ONETIME_CHARGE = "activate_onetimecharge"
@dataclass
class ViCareButtonEntityDescription(ButtonEntityDescription, ViCareRequiredKeysMixin):
class ViCareButtonEntityDescription(
ButtonEntityDescription, ViCareRequiredKeysMixinWithSet
):
"""Describes ViCare button sensor entity."""
@ -37,7 +39,8 @@ BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = (
name="Activate one-time charge",
icon="mdi:shower-head",
entity_category=EntityCategory.CONFIG,
value_getter=lambda api: api.activateOneTimeCharge(),
value_getter=lambda api: api.getOneTimeCharge(),
value_setter=lambda api: api.activateOneTimeCharge(),
),
)
@ -54,6 +57,15 @@ async def async_setup_entry(
entities = []
for description in BUTTON_DESCRIPTIONS:
try:
description.value_getter(api)
_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}",
api,
@ -83,7 +95,7 @@ class ViCareButton(ButtonEntity):
"""Handle the button press."""
try:
with suppress(PyViCareNotSupportedFeatureError):
self.entity_description.value_getter(self._api)
self.entity_description.value_setter(self._api)
except requests.exceptions.ConnectionError:
_LOGGER.error("Unable to retrieve data from ViCare server")
except ValueError: