mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 02:37:50 +00:00
Raise HomeAssistantError if event is triggered with invalid event_type (#106357)
This commit is contained in:
parent
99734a76aa
commit
65e8bbacc9
@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Any, Self, final
|
|||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.config_validation import ( # noqa: F401
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
PLATFORM_SCHEMA_BASE,
|
PLATFORM_SCHEMA_BASE,
|
||||||
@ -154,7 +155,17 @@ class EventEntity(RestoreEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_)
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Process a new event."""
|
"""Process a new event."""
|
||||||
if event_type not in self.event_types:
|
if event_type not in self.event_types:
|
||||||
raise ValueError(f"Invalid event type {event_type} for {self.entity_id}")
|
event_types: str = ", ".join(self.event_types)
|
||||||
|
raise HomeAssistantError(
|
||||||
|
f"Invalid event type {event_type} for {self.entity_id}",
|
||||||
|
translation_key="invalid_event_type",
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_placeholders={
|
||||||
|
"event_type": event_type,
|
||||||
|
"event_types": event_types,
|
||||||
|
"entity_id": self.entity_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
self.__last_event_triggered = dt_util.utcnow()
|
self.__last_event_triggered = dt_util.utcnow()
|
||||||
self.__last_event_type = event_type
|
self.__last_event_type = event_type
|
||||||
self.__last_event_attributes = event_attributes
|
self.__last_event_attributes = event_attributes
|
||||||
|
@ -21,5 +21,10 @@
|
|||||||
"motion": {
|
"motion": {
|
||||||
"name": "Motion"
|
"name": "Motion"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"exceptions": {
|
||||||
|
"invalid_event_type": {
|
||||||
|
"message": "Invalid event type {event_type} for {entity_id}, valid types are: {event_types}."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ from homeassistant.components.event import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME, CONF_VALUE_TEMPLATE
|
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME, CONF_VALUE_TEMPLATE
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
@ -174,7 +175,7 @@ class MqttEvent(MqttEntity, EventEntity):
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self._trigger_event(event_type, event_attributes)
|
self._trigger_event(event_type, event_attributes)
|
||||||
except ValueError:
|
except HomeAssistantError:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Invalid event type %s for %s received on topic %s, payload %s",
|
"Invalid event type %s for %s received on topic %s, payload %s",
|
||||||
event_type,
|
event_type,
|
||||||
|
@ -16,6 +16,7 @@ from homeassistant.components.event import (
|
|||||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
||||||
from homeassistant.const import CONF_PLATFORM, STATE_UNKNOWN
|
from homeassistant.const import CONF_PLATFORM, STATE_UNKNOWN
|
||||||
from homeassistant.core import HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY
|
from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@ -90,7 +91,8 @@ async def test_event() -> None:
|
|||||||
|
|
||||||
# Test triggering an unknown event
|
# Test triggering an unknown event
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
ValueError, match="^Invalid event type unknown_event for event.doorbell$"
|
HomeAssistantError,
|
||||||
|
match="^Invalid event type unknown_event for event.doorbell$",
|
||||||
):
|
):
|
||||||
event._trigger_event("unknown_event")
|
event._trigger_event("unknown_event")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user