mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
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:
parent
94c825cf4f
commit
35fa73eee9
@ -34,6 +34,14 @@ class ViCareRequiredKeysMixin:
|
|||||||
value_getter: Callable[[Device], bool]
|
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:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up from config entry."""
|
"""Set up from config entry."""
|
||||||
_LOGGER.debug("Setting up ViCare component")
|
_LOGGER.debug("Setting up ViCare component")
|
||||||
|
@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import ViCareRequiredKeysMixin
|
from . import ViCareRequiredKeysMixinWithSet
|
||||||
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG, VICARE_NAME
|
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG, VICARE_NAME
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -27,7 +27,9 @@ BUTTON_DHW_ACTIVATE_ONETIME_CHARGE = "activate_onetimecharge"
|
|||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ViCareButtonEntityDescription(ButtonEntityDescription, ViCareRequiredKeysMixin):
|
class ViCareButtonEntityDescription(
|
||||||
|
ButtonEntityDescription, ViCareRequiredKeysMixinWithSet
|
||||||
|
):
|
||||||
"""Describes ViCare button sensor entity."""
|
"""Describes ViCare button sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +39,8 @@ BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = (
|
|||||||
name="Activate one-time charge",
|
name="Activate one-time charge",
|
||||||
icon="mdi:shower-head",
|
icon="mdi:shower-head",
|
||||||
entity_category=EntityCategory.CONFIG,
|
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 = []
|
entities = []
|
||||||
|
|
||||||
for description in BUTTON_DESCRIPTIONS:
|
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(
|
entity = ViCareButton(
|
||||||
f"{name} {description.name}",
|
f"{name} {description.name}",
|
||||||
api,
|
api,
|
||||||
@ -83,7 +95,7 @@ class ViCareButton(ButtonEntity):
|
|||||||
"""Handle the button press."""
|
"""Handle the button press."""
|
||||||
try:
|
try:
|
||||||
with suppress(PyViCareNotSupportedFeatureError):
|
with suppress(PyViCareNotSupportedFeatureError):
|
||||||
self.entity_description.value_getter(self._api)
|
self.entity_description.value_setter(self._api)
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
_LOGGER.error("Unable to retrieve data from ViCare server")
|
_LOGGER.error("Unable to retrieve data from ViCare server")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user