def _get_state_if_valid(hass: HomeAssistant, entity_id: str) -> (TemplateState | None):
    state = hass.states.get(entity_id)
    if ((state is None) and (not valid_entity_id(entity_id))):
        raise TemplateError(f"Invalid entity ID '{entity_id}'")
    return _get_template_state_from_state(hass, entity_id, state)
def _get_state_if_valid(hass: HomeAssistant, entity_id: str) -> (TemplateState | None):
    state = hass.states.get(entity_id)
    if ((state is None) and (not valid_entity_id(entity_id))):
        raise TemplateError(f"Invalid entity ID '{entity_id}'")
    return _get_template_state_from_state(hass, entity_id, state)